Skip to content

Commit 3e4815e

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 4015cec commit 3e4815e

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@
273273
},
274274
"markdownDescription": "Additional arguments to pass to `swift` commands such as `swift build`, `swift package`, `swift test`, etc... Keys and values should be provided as individual entries in the list. If you have created a copy of the build task in `tasks.json` then these build arguments will not be propagated to that task."
275275
},
276+
"swift.disableSandbox": {
277+
"type": "boolean",
278+
"default": false,
279+
"markdownDescription": "Disable sandboxing when running SwiftPM commands."
280+
},
276281
"swift.additionalTestArguments": {
277282
"type": "array",
278283
"default": [],
@@ -681,7 +686,12 @@
681686
"swift.swiftSDK": {
682687
"type": "string",
683688
"default": "",
684-
"markdownDescription": "The [Swift SDK](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md) to compile against (`--swift-sdk` parameter).",
689+
"markdownDescription": "The [Swift SDK](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md) to compile against (`--swift-sdk` parameter)."
690+
},
691+
"swift.disableSandox": {
692+
"type": "boolean",
693+
"default": false,
694+
"markdownDescription": "Disable sandboxing when running SwiftPM commands. You will almost always want this setting disabled.",
685695
"order": 4
686696
},
687697
"swift.diagnostics": {

src/configuration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ const configuration = {
358358
.getConfiguration("swift")
359359
.get<boolean>("enableTerminalEnvironment", true);
360360
},
361+
/** Whether or not to disable SwiftPM sandboxing */
362+
get disableSandbox(): boolean {
363+
return vscode.workspace.getConfiguration("swift").get<boolean>("disableSandbox", false);
364+
},
361365
};
362366

363367
export default configuration;

src/tasks/SwiftTaskProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ export function createSwiftTask(
287287
): SwiftTask {
288288
const swift = toolchain.getToolchainExecutable("swift");
289289
args = toolchain.buildFlags.withSwiftSDKFlags(args);
290+
args = toolchain.buildFlags.withDisableSandboxFlags(args);
290291

291292
// Add relative path current working directory
292293
const cwd = config.cwd.fsPath;

src/toolchain/BuildFlags.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,38 @@ export class BuildFlags {
176176
return indirect ? args.flatMap(arg => ["-Xswiftc", arg]) : args;
177177
}
178178

179+
/**
180+
* Get modified swift arguments with new arguments for disabling
181+
* sandboxing if the `swift.disableSandbox` setting is enabled.
182+
*
183+
* @param args original commandline arguments
184+
*/
185+
withDisableSandboxFlags(args: string[]): string[] {
186+
if (!configuration.disableSandbox) {
187+
return args;
188+
}
189+
switch (args[0]) {
190+
case "package": {
191+
return [args[0], ...BuildFlags.disableSandboxFlags(), ...args.slice(1)];
192+
}
193+
case "build":
194+
case "run":
195+
case "test": {
196+
return [...args, ...BuildFlags.disableSandboxFlags()];
197+
}
198+
default:
199+
// Do nothing for other commands
200+
return args;
201+
}
202+
}
203+
204+
/**
205+
* Get flags for disabling sandboxing when running SwiftPM
206+
*/
207+
static disableSandboxFlags(): string[] {
208+
return ["--disable-sandbox", "-Xswiftc", "-disable-sandbox"];
209+
}
210+
179211
/**
180212
* Filter argument list
181213
* @param args argument list

src/toolchain/toolchain.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,10 @@ export class SwiftToolchain {
813813
private static async getSwiftTargetInfo(): Promise<SwiftTargetInfo> {
814814
try {
815815
try {
816-
const { stdout } = await execSwift(["-print-target-info"], "default");
816+
const { stdout } = await execSwift(
817+
["-print-target-info", ...BuildFlags.disableSandboxFlags()],
818+
"default"
819+
);
817820
const targetInfo = JSON.parse(stdout.trimEnd()) as SwiftTargetInfo;
818821
if (targetInfo.compilerVersion) {
819822
return targetInfo;

src/utilities/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export async function execSwift(
188188
}
189189
if (toolchain !== "default") {
190190
args = toolchain.buildFlags.withSwiftSDKFlags(args);
191+
args = toolchain.buildFlags.withDisableSandboxFlags(args);
191192
}
192193
if (Object.keys(configuration.swiftEnvironmentVariables).length > 0) {
193194
// when adding environment vars we either combine with vars passed

0 commit comments

Comments
 (0)