How do I read from a folder in the user's home directory from an Xcode Source Editor Extension?

314 views Asked by At

I created an Xcode Source Editor Extension. I'd like to build a linting command, for example. The user's existing lint file is located in a location like /Users/Tim/CalculatorApp/swiftlint.yml.

If I try to use try! String(contentsOfFile: "/Users/Tim/CalculatorApp/swiftlint.yml"), I get the error:

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 "The file “.swiftlint.yml” couldn’t be opened because you don’t have permission to view it."

This is most likely because the extension is running in a sandbox and doesn't have permission to read files under the user's home directory. I tried removing the sandbox from the Extension's target, but then the extension doesn't load at all.

I get the error:

Could not attach to pid : “3623”

attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries when the attached failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.)

If I re-add the App Sandbox, clean the project, then run again, I no longer get this error. This leads me to believe that Xcode Extensions might need to be run in App Sandbox mode.

Is there a way to run an Xcode Extension without App Sandbox mode?

Are there any other workarounds for how to allow the Xcode extension to read the file in question? I was thinking about using the "User Selected File" permission, but I don't see how that would work with an Extension which has no UI.

Any other thoughts for how to get something like this to work?

0

There are 0 answers