Conversation
|
Do you think it's intentional that the XPC capability is disabled in the sandbox? Doesn't seem to be mentioned. Can we tell we're in an SPM-plugin / sandboxed environment and automatically use the in-proc version? Something like Should we just always use the in-proc version if it's available? I guess that would be a bold move... Need to document the setting in the README. |
|
When you build a Swift package that uses SPM plugins, you can find the helper script that Xcode uses to execute custom build commands in the build log. The script uses sandbox-exec with some rules that specify which directories the command may write files to. It doesn't explicitly specify any XPC rules, so I guess that's just a side effect of using a sandbox. I looked at detecting if we're in an SPM plugin, the only thing that I found was this environment variable set by Xcode: So it would be possible to parse it at runtime, and if We could also try testing if we have write access to some directories to test if we're in a sandbox, but that also feels like a workaround. The solution in this PR isn't ideal either, because setting environment variables in a Regarding always using the in-proc version, that might be not great for people who use SourceKittenFramework in a GUI app, because then if sourcekit crashes, so will the GUI app. So there are advantages in using the XPC version. So, which solution is the best? I feel that using an environment variable to give the users a choice is the most universal way. |
|
An environment variable for this is fine, but I think we’d additionally want a programmatic option for use in SwiftLint. |
fd2e3c1 to
3c27f6c
Compare
|
Alright, I've added the description of the new environment variable in the README. For a programmatic option, we could make this a -let useInProcSourceKit = envBool("USE_INPROC_SOURCEKIT")
+public var useInProcSourceKit = envBool("USE_INPROC_SOURCEKIT") |
So we can set it programmatically regardless of the user's environment.
|
Thanks @juozasvalancius! I made a few small changes on your branch, I hope you don't mind:
I'll be merging this once CI passes. Thanks for working on this, and please keep me up to date as you make progress on your SwiftPM build tool integration! |
|
I tested with SwiftLint to see if there was any performance impact and it looks like the answer is no. I confirmed via |
As of jpsim/SourceKitten#728 there's now a way to run SourceKitten while avoiding XPC communication with SourceKit. This should prevent the sandbox violations we were seeing before.
* sourcekitten 0.32.0 * Requires Xcode 12.0 to build * Improve SourceKitten test As of jpsim/SourceKitten#728 there's now a way to run SourceKitten while avoiding XPC communication with SourceKit. This should prevent the sandbox violations we were seeing before. * Fix style * Use ENV instead * Try with Xcode 13+ * Only run syntax test when Xcode 13 is selected * Use MacOs::Xcode.version Closes #96932. Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Signed-off-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Signed-off-by: BrewTestBot <1589480+BrewTestBot@users.noreply.github.com>
|
@jpsim thanks for the improvements! And I'll be sure to update you if I have more code to share about SPM plugins. |
There are two versions of sourcekitd that come with Xcode:
sourcekitd.framework, which uses XPC to connect to the sourcekit daemon and parse swift files out-of-process.sourcekitdInProc.framework, which does the parsing in-process.SourceKitten uses the XPC version on macOS. However, there may be cases, when the in-process version might be preferred. So this PR adds the possibility to configure this using the following environment variable:
Specifically, this will be needed to support running SourceKitten in Swift Package Manager build tool plugins. It is a new feature (SE-0303) in Swift 5.6, that allows Swift packages to execute custom build commands when building. So for example, you could have swiftlint run during a build of your Swift package.
As of this writing, Swift 5.6 is available in the Release Candidate of Xcode 13.3. I have experimented with it and found that SPM build commands run in a sandbox that prevent the use of XPC and crash when using the regular version of sourcekitd, but it works normally with sourcekitdInProc.