Skip to content

Commit 21e8612

Browse files
committed
Pass arguments to disable sandboxing when enabled
New disableSandbox setting (only intended for testing right now) These particular commands (or subcommands) use sandboxing. To get our tests to run in a sandbox, we need to disable sandboxing because you cannot create a new sandbox when you're already running under a sandbox
1 parent 9f8cf10 commit 21e8612

File tree

7 files changed

+64
-8
lines changed

7 files changed

+64
-8
lines changed

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@
580580
"markdownDescription": "The path of the SDK to compile against (`--sdk` parameter). This is of use when supporting non-standard SDK layouts on Windows and using custom SDKs. The default SDK is determined by the environment on macOS and Windows.",
581581
"order": 3
582582
},
583+
"swift.disableSandox": {
584+
"type": "boolean",
585+
"default": false,
586+
"markdownDescription": "Disable sandboxing when running SwiftPM commands. You will almost always want this setting disabled.",
587+
"order": 4
588+
},
583589
"swift.diagnostics": {
584590
"type": "boolean",
585591
"default": false,

src/configuration.ts

+4
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ const configuration = {
291291
.getConfiguration("swift")
292292
.get<boolean>("enableTerminalEnvironment", true);
293293
},
294+
/** Whether or not to disable SwiftPM sandboxing */
295+
get disableSandbox(): boolean {
296+
return vscode.workspace.getConfiguration("swift").get<boolean>("disableSandbox", false);
297+
},
294298
};
295299

296300
export default configuration;

src/tasks/SwiftTaskProvider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export function createSwiftTask(
323323
): SwiftTask {
324324
const swift = toolchain.getToolchainExecutable("swift");
325325
args = toolchain.buildFlags.withSwiftSDKFlags(args);
326+
args = toolchain.buildFlags.withDisableSandboxFlags(args);
326327

327328
// Add relative path current working directory
328329
const cwd = config.cwd.fsPath;

src/toolchain/BuildFlags.ts

+32
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,38 @@ export class BuildFlags {
166166
return indirect ? args.flatMap(arg => ["-Xswiftc", arg]) : args;
167167
}
168168

169+
/**
170+
* Get modified swift arguments with new arguments for disabling
171+
* sandboxing if the `swift.disableSandbox` setting is enabled.
172+
*
173+
* @param args original commandline arguments
174+
*/
175+
withDisableSandboxFlags(args: string[]): string[] {
176+
if (!configuration.disableSandbox) {
177+
return args;
178+
}
179+
switch (args[0]) {
180+
case "package": {
181+
return [args[0], ...BuildFlags.disableSandboxFlags(), ...args.slice(1)];
182+
}
183+
case "build":
184+
case "run":
185+
case "test": {
186+
return [...args, ...BuildFlags.disableSandboxFlags()];
187+
}
188+
default:
189+
// Do nothing for other commands
190+
return args;
191+
}
192+
}
193+
194+
/**
195+
* Get flags for disabling sandboxing when running SwiftPM
196+
*/
197+
static disableSandboxFlags(): string[] {
198+
return ["--disable-sandbox", "-Xswiftc", "-disable-sandbox"];
199+
}
200+
169201
/**
170202
* Filter argument list
171203
* @param args argument list

src/toolchain/toolchain.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,10 @@ export class SwiftToolchain {
711711
private static async getSwiftTargetInfo(): Promise<SwiftTargetInfo> {
712712
try {
713713
try {
714-
const { stdout } = await execSwift(["-print-target-info"], "default");
714+
const { stdout } = await execSwift(
715+
["-print-target-info", ...BuildFlags.disableSandboxFlags()],
716+
"default"
717+
);
715718
const targetInfo = JSON.parse(stdout.trimEnd()) as SwiftTargetInfo;
716719
if (targetInfo.compilerVersion) {
717720
return targetInfo;

src/utilities/utilities.ts

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export async function execSwift(
165165
}
166166
if (toolchain !== "default") {
167167
args = toolchain.buildFlags.withSwiftSDKFlags(args);
168+
args = toolchain.buildFlags.withDisableSandboxFlags(args);
168169
}
169170
if (Object.keys(configuration.swiftEnvironmentVariables).length > 0) {
170171
// when adding environment vars we either combine with vars passed

test/suite/tasks/SwiftTaskProvider.test.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ suite("SwiftTaskProvider Test Suite", () => {
5151

5252
test("Exit code on success", async () => {
5353
const task = createSwiftTask(
54-
["--help"],
55-
"help",
54+
["build", "--show-bin-path"],
55+
"show bin path",
5656
{ cwd: workspaceFolder.uri, scope: vscode.TaskScope.Workspace },
5757
toolchain
5858
);
@@ -84,7 +84,12 @@ suite("SwiftTaskProvider Test Suite", () => {
8484
new vscode.CancellationTokenSource().token
8585
);
8686
const task = tasks.find(t => t.name === "Build All (defaultPackage)");
87-
assert.equal(task?.detail, "swift build --build-tests -Xswiftc -diagnostic-style=llvm");
87+
assert.equal(
88+
task?.detail?.startsWith(
89+
"swift build --build-tests -Xswiftc -diagnostic-style=llvm"
90+
),
91+
true
92+
);
8893
});
8994

9095
test("includes product debug task", async () => {
@@ -94,8 +99,10 @@ suite("SwiftTaskProvider Test Suite", () => {
9499
);
95100
const task = tasks.find(t => t.name === "Build Debug PackageExe (defaultPackage)");
96101
assert.equal(
97-
task?.detail,
98-
"swift build --product PackageExe -Xswiftc -diagnostic-style=llvm"
102+
task?.detail?.startsWith(
103+
"swift build --product PackageExe -Xswiftc -diagnostic-style=llvm"
104+
),
105+
true
99106
);
100107
});
101108

@@ -106,8 +113,10 @@ suite("SwiftTaskProvider Test Suite", () => {
106113
);
107114
const task = tasks.find(t => t.name === "Build Release PackageExe (defaultPackage)");
108115
assert.equal(
109-
task?.detail,
110-
"swift build -c release --product PackageExe -Xswiftc -diagnostic-style=llvm"
116+
task?.detail?.startsWith(
117+
"swift build -c release --product PackageExe -Xswiftc -diagnostic-style=llvm"
118+
),
119+
true
111120
);
112121
});
113122
});

0 commit comments

Comments
 (0)