Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat attachment file picker search path is broken when using Workspace #155

Open
DBNDeveloper opened this issue Feb 15, 2025 · 2 comments
Open

Comments

@DBNDeveloper
Copy link

When trying to add files to Chat via the attachment picker, files that should be available aren't and files that should not be available are. It seems that when in an Xcode workspace, the Chat file picker simply scans directories that are adjacent to the workspace and makes all files available even if the adjacent directories and subdirectories are not in the workspace.

Versions

  • Copilot for Xcode: 0.31.0
  • Xcode: 16
  • macOS: 5.2

Steps to reproduce
Bug 1. Too Greedy

  1. Create a Demo directory in file system
  2. Create a Projects directory inside Demo Demo/Projects
  3. Create ProjectA in Projects Demo/Projects/ProjectA
  4. Create ProjectB in Projects Demo/Projects/ProjectB
  5. Save ProjectA as a Workspace in the Projects directory Demo/Projects/ProjectA.xcworkspace
  6. Open ProjectA.xcworkspace
  7. Open Chat Window Attach Context Picker

Results: ProjectB files are available to attach to Chat, they shouldn't be.

Bug 2. Too limited

  1. Create a Packages directory in demo Demo/Packages
  2. Create a PackageA in the Packages Demo/Packages/PackageA
  3. Edit the package as a local dependecy within the workspace
  4. Open Chat Window Attach Context Picker

Results: All project files are available, but only the focused package file is available. We cant add other files from the package.

The issue seems be be in ContextUtils.getFilesInActiveWorkspace() which makes no use of the workspaceURL :

    public static func getFilesInActiveWorkspace() -> [FileReference] {
        guard let workspaceURL = XcodeInspector.shared.realtimeActiveWorkspaceURL,
              let projectURL = XcodeInspector.shared.realtimeActiveProjectURL else {
            return []
        }

        do {
            let fileManager = FileManager.default
            let enumerator = fileManager.enumerator(
                at: projectURL,
                includingPropertiesForKeys: [.isRegularFileKey, .isDirectoryKey],
                options: [.skipsHiddenFiles]
            )

            var files: [FileReference] = []
            while let fileURL = enumerator?.nextObject() as? URL {
                // Skip items matching the specified pattern
                if matchesPatterns(fileURL, patterns: skipPatterns) {
                    enumerator?.skipDescendants()
                    continue
                }

                let resourceValues = try fileURL.resourceValues(forKeys: [.isRegularFileKey, .isDirectoryKey])
                // Handle directories if needed
                if resourceValues.isDirectory == true {
                    continue
                }

                guard resourceValues.isRegularFile == true else { continue }
                if supportedFileExtensions.contains(fileURL.pathExtension.lowercased()) == false {
                    continue
                }

                let relativePath = fileURL.path.replacingOccurrences(of: projectURL.path, with: "")
                let fileName = fileURL.lastPathComponent

                let file = FileReference(url: fileURL,
                                           relativePath: relativePath,
                                           fileName: fileName)
                files.append(file)
            }

            return files
        } catch {
            Logger.client.error("Failed to get files in workspace: \(error)")
            return []
        }
    }
@testforstephen
Copy link

@DBNDeveloper thanks for the feedback. We will take a look to see how to improve it.

@testforstephen
Copy link

@DBNDeveloper The latest pre-release 0.31.104 (https://github.com/github/CopilotForXcode/releases/tag/0.31.104) fixed the file picker scope issue when using Workspace. Feel free to give a try and let us know if it works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants