url startAccessingSecurityScopedResource on directory with Package.swift fails

One of the functions in an app I made is to add bookmarks to directories that a plugin can access (read-only).

For the most part it works fine, however, I have noticed that granting access to a directory containing a Package.swift will cause the startAccessingSecurityScopedResource method to fail.

I am not currently using the .withSecurityScope option when retrieving the bookmark data:

let data = try url.bookmarkData(options: [], includingResourceValuesForKeys: nil, relativeTo: nil)

however it is worth noting that when I do include the .withSecurityScope option this method also fails with an error 256: Failed to open/cannot open

I have read through the documentation and I can't see anything specific around the contents of a directory. Was wondering if anyone else has hit this one or has any links to specific documentation around the issue/directory content limitations?

I’m presuming that this is on the Mac but please let me know otherwise.

Is your app sandboxed?

You wrote:

One of the functions in an app I made is to add bookmarks to directories that a plugin can access (read-only).

Please elaborate on how this works. It seems you have an app and a plug-in. Is the plug-in running in the app’s process? Or a separate process?

And what does “add bookmarks to directories” mean? I can read it two ways:

  • You’re actually writing a bookmark to a directory.

  • You have to a directory and you’re saving a bookmark to it.

Is it either of those? Or something else.

And how does your app get its initial access to this directory? Using an open panel? Or something else?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Definitely should have put more details sorry 😅

So yes it is a Mac app and it is Sandboxed.

Specifically, the Mac application is the companion app housing an XcodeSourceEditorExtension. In order for the extension to read source contents and do it's thing, it requires read access to the project directory.

The user adds these permissions via the Mac application, which uses an open panel so the user can select an Xcode project file, workspace, or Package.swift file. Once the user submits this, the url to the directory containing the project file is used to save a bookmark to the directory.

When the user launches Xcode and invokes the extension, it resolves the active project to find the matching bookmark based on the directory the active project is in. Once resolved it calls the startAccessingSecurityScopedResource method, does its thing, then calls the stopAccessingSecurityScopedResource once it has finished.

I first noticed the issue when trying to use the extension in a standalone swift package project. Debugging led me to find the problem described in the original post.

Once the user submits this, the url to the directory containing the project file is used to save a bookmark to the directory.

Juts to clarify, that last part should be “used to save a bookmark pointing to the directory”, right?

Where do you save the contents of that bookmark?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yes - used to save a bookmark pointing to the directory is correct

sorry missed the last bit - I am storing them in UserDefaults at the moment

Presumably in user defaults that you share between your app and your appex using an app group?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yes - currently the UserDefaults are shared using an app group

And this works in general? But fails when the directory contains a Package.swift file?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yeah works in general and only seems to fail when the directory contains a Package.swift file.

Well, that’s weird.

One thing to note here is that, on the Mac, SwiftPM generally runs in a sandbox, where the goal is to prevent a rogue build from eating your Mac. This isn’t a standard App Sandbox-style sandbox, but something custom. It’s possible that this is dropping something on the disk that’s confusing bookmarks.

However, I’ve never dug into this aspect of SwiftPM so that’s all the insight I have into this issue. If you want further help with this I recommend that you open a DTS tech support incident so that I, or one of my colleagues, can carve out the time to investigate it properly.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yeah a bit of an odd one - am putting together a sample project to raise a tech support ticket. Hopefully can be resolved, will post back with any future updates 🤞

url startAccessingSecurityScopedResource on directory with Package.swift fails
 
 
Q