Skip to content

[5.7] The shouldDisableSandbox setting wasn't being passed along to build tool plugin commands (#4283) #4308

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

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// swift-tools-version: 5.6
import PackageDescription

let package = Package(
name: "MyLibrary",
dependencies: [
.package(path: "../MyPlugin")
],
targets: [
.target(
name: "MyLibrary",
plugins: [
.plugin(name: "PackageScribblerPlugin", package: "MyPlugin")
])
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public func MyLibraryStruct() -> String {
return "This is \(foo)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version: 5.6
import PackageDescription

let package = Package(
name: "MyPlugin",
products: [
.plugin(
name: "PackageScribblerPlugin",
targets: ["PackageScribblerPlugin"]
),
],
targets: [
.plugin(
name: "PackageScribblerPlugin",
capability: .buildTool()
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import PackagePlugin
import Foundation

@main
struct MyPlugin: BuildToolPlugin {

func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
let outputDir = target.directory.appending("generated")
try FileManager.default.createDirectory(atPath: outputDir.string, withIntermediateDirectories: true)
return [
.prebuildCommand(
displayName: "Creating Foo.swift in the target directory…",
executable: Path("/bin/bash"),
arguments: [ "-c", "echo 'let foo = \"\(target.name)\"' > '\(outputDir)/foo.swift'" ],
outputFilesDirectory: outputDir)
]
}
}
1 change: 1 addition & 0 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ public class SwiftTool {
packageGraphLoader: customPackageGraphLoader ?? graphLoader,
pluginScriptRunner: self.getPluginScriptRunner(),
pluginWorkDirectory: try self.getActiveWorkspace().location.pluginWorkingDirectory,
disableSandboxForPluginCommands: self.options.security.shouldDisableSandbox,
outputStream: customOutputStream ?? self.outputStream,
logLevel: customLogLevel ?? self.logLevel,
fileSystem: self.fileSystem,
Expand Down
23 changes: 23 additions & 0 deletions Tests/FunctionalTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,27 @@ class PluginTests: XCTestCase {
XCTAssert(stdout.contains("type of snippet target: snippet"), "output:\n\(stderr)\n\(stdout)")
}
}

func testSandboxViolatingBuildToolPluginCommands() throws {
#if !os(macOS)
try XCTSkipIf(true, "sandboxing tests are only supported on macOS")
#endif

// Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require).
try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency")

// Check that the build fails with a sandbox violation by default.
try fixture(name: "Miscellaneous/Plugins/SandboxViolatingBuildToolPluginCommands") { path in
XCTAssertThrowsError(try executeSwiftBuild(path.appending(component: "MyLibrary"), configuration: .Debug)) { error in
XCTAssertMatch("\(error)", .contains("You don’t have permission to save the file “generated” in the folder “MyLibrary”."))
}
}

// Check that the build succeeds if we disable the sandbox.
try fixture(name: "Miscellaneous/Plugins/SandboxViolatingBuildToolPluginCommands") { path in
let (stdout, stderr) = try executeSwiftBuild(path.appending(component: "MyLibrary"), configuration: .Debug, extraArgs: ["--disable-sandbox"])
XCTAssert(stdout.contains("Compiling MyLibrary foo.swift"), "[STDOUT]\n\(stdout)\n[STDERR]\n\(stderr)\n")
}

}
}