SPM plugin launching Process doesn't seems to have access to internet connection

I've created an executable in Swift.
This executable downloads some files precompiled binaries from a repository. The executable uses URLConnection.
Now I'm trying to wrap this executable into a swift package commend plugin.

.plugin(
            name: "DepPackagePlugin",
            capability: .command(
                intent: .custom(
                    verb: "download-dependencies",
                    description: "Download dependencies"
                ),
                permissions: [
                    .writeToPackageDirectory(reason: "to download dependencies")
                ]
            )
        )

The plugin is visible in the list of available plugins.

@main
struct DepPackagePlugin: CommandPlugin {
    func performCommand(context: PluginContext, arguments: [String]) async throws {
        let directoryPlugin = context.package.directory
        let commandPath =  directoryPlugin.appending("DepDownloader").string
        print(commandPath)
        let destinationPath = directoryPlugin.string
        print(destinationPath)
        let process = Process()
        process.executableURL = URL(fileURLWithPath: commandPath)
        process.arguments = [
            destinationPath
        ]
        print(process.arguments)
        print(process.executableURL)
        try process.run()
        process.waitUntilExit()
        
        if process.terminationReason == .exit && process.terminationStatus == 0 {
            print("Dowloaded dependencies to  \(destinationPath).")
        }
        else {
            let problem = "\(process.terminationReason):\(process.terminationStatus)"
            Diagnostics.error("Invocation failed: \(problem)")
        }
    }
}

The implementation creates a process with the required arguments.
The executable, launched directly using bash works properly, but if I try to call it using SPM, it exits before even trying to connect to the repository. Doing a MiM I see that the url is not even called.
The error I get is this:

2023-02-03 11:00:25.487 EmPackage[9270:10276199] Error: stepSQLStatement:toCompletionWithRetry - stepping returned unhandled result=8, DB=/Users/XYZ/Library/Caches/DepDownloader/Cache.db
Error: The operation couldn’t be completed. Operation not permitted
error: Invocation failed: NSTaskTerminationReason(rawValue: 1):1

Does command plugin supports Internet connection? it doesn't' seems to exist such a permission.
Or it is due to a wrong configuration in the URLConnection used inside the executable?

I've read that plugins are run in a sandbox where network access is not available :frowning:

Have you tried --disable-sandbox?

mikolasstuchlik@Mikolas-MacBook-Pro SST_iOS % swift build --help | grep sandbox
  --disable-sandbox       Disable using the sandbox when executing subprocesses

Unfortunately, is not present in the package subcommand that is needed to launch the plugin

Can you share the exact command that you're trying to use? Something like swift package --disable-sandbox download-dependencies should work.

1 Like

Damn, invertire the order of --allow-writing-to-package-directory and --disable-sandbox it worked!!!
Amazing! thank you so much!

1 Like