Skip to content

Commit 2a12ea4

Browse files
committed
The shouldDisableSandbox setting wasn't being passed along to build tool plugin commands
Not exactly sure when this failed, but the setting ended up not getting passed through in one of the refactorings a couple of weeks ago. There wasn't a unit test covering it, so this commit adds one. rdar://90955872
1 parent f372135 commit 2a12ea4

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// swift-tools-version: 5.6
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "MyLibrary",
6+
dependencies: [
7+
.package(path: "../MyPlugin")
8+
],
9+
targets: [
10+
.target(
11+
name: "MyLibrary",
12+
plugins: [
13+
.plugin(name: "PackageScribblerPlugin", package: "MyPlugin")
14+
])
15+
]
16+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public func MyLibraryStruct() -> String {
2+
return "This is \(foo)"
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// swift-tools-version: 5.6
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "MyPlugin",
6+
products: [
7+
.plugin(
8+
name: "PackageScribblerPlugin",
9+
targets: ["PackageScribblerPlugin"]
10+
),
11+
],
12+
targets: [
13+
.plugin(
14+
name: "PackageScribblerPlugin",
15+
capability: .buildTool()
16+
)
17+
]
18+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import PackagePlugin
2+
import Foundation
3+
4+
@main
5+
struct MyPlugin: BuildToolPlugin {
6+
7+
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
8+
let outputDir = target.directory.appending("generated")
9+
try FileManager.default.createDirectory(atPath: outputDir.string, withIntermediateDirectories: true)
10+
return [
11+
.prebuildCommand(
12+
displayName: "Creating Foo.swift in the target directory…",
13+
executable: Path("/bin/bash"),
14+
arguments: [ "-c", "echo 'let foo = \"\(target.name)\"' > '\(outputDir)/foo.swift'" ],
15+
outputFilesDirectory: outputDir)
16+
]
17+
}
18+
}

Sources/Commands/SwiftTool.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ public class SwiftTool {
754754
packageGraphLoader: customPackageGraphLoader ?? graphLoader,
755755
pluginScriptRunner: self.getPluginScriptRunner(),
756756
pluginWorkDirectory: try self.getActiveWorkspace().location.pluginWorkingDirectory,
757+
disableSandboxForPluginCommands: self.options.security.shouldDisableSandbox,
757758
outputStream: customOutputStream ?? self.outputStream,
758759
logLevel: customLogLevel ?? self.logLevel,
759760
fileSystem: self.fileSystem,

Tests/FunctionalTests/PluginTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,4 +855,23 @@ class PluginTests: XCTestCase {
855855
XCTAssert(stdout.contains("type of snippet target: snippet"), "output:\n\(stderr)\n\(stdout)")
856856
}
857857
}
858+
859+
func testSandboxViolatingBuildToolPluginCommands() throws {
860+
// Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require).
861+
try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency")
862+
863+
// Check that the build fails with a sandbox violation by default.
864+
try fixture(name: "Miscellaneous/Plugins/SandboxViolatingBuildToolPluginCommands") { path in
865+
XCTAssertThrowsError(try executeSwiftBuild(path.appending(component: "MyLibrary"), configuration: .Debug)) { error in
866+
XCTAssertMatch("\(error)", .contains("You don’t have permission to save the file “generated” in the folder “MyLibrary”."))
867+
}
868+
}
869+
870+
// Check that the build succeeds if we disable the sandbox.
871+
try fixture(name: "Miscellaneous/Plugins/SandboxViolatingBuildToolPluginCommands") { path in
872+
let (stdout, stderr) = try executeSwiftBuild(path.appending(component: "MyLibrary"), configuration: .Debug, extraArgs: ["--disable-sandbox"])
873+
XCTAssert(stdout.contains("Compiling MyLibrary foo.swift"), "[STDOUT]\n\(stdout)\n[STDERR]\n\(stderr)\n")
874+
}
875+
876+
}
858877
}

0 commit comments

Comments
 (0)