Skip to content
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

Pass arguments to disable sandboxing when enabled #993

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@
"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.",
"order": 3
},
"swift.disableSandox": {
"type": "boolean",
"default": false,
"markdownDescription": "Disable sandboxing when running SwiftPM commands. You will almost always want this setting disabled.",
"order": 4
},
"swift.diagnostics": {
"type": "boolean",
"default": false,
Expand Down
4 changes: 4 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ const configuration = {
.getConfiguration("swift")
.get<boolean>("enableTerminalEnvironment", true);
},
/** Whether or not to disable SwiftPM sandboxing */
get disableSandbox(): boolean {
return vscode.workspace.getConfiguration("swift").get<boolean>("disableSandbox", false);
},
};

export default configuration;
1 change: 1 addition & 0 deletions src/tasks/SwiftTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export function createSwiftTask(
): SwiftTask {
const swift = toolchain.getToolchainExecutable("swift");
args = toolchain.buildFlags.withSwiftSDKFlags(args);
args = toolchain.buildFlags.withDisableSandboxFlags(args);

// Add relative path current working directory
const cwd = config.cwd.fsPath;
Expand Down
32 changes: 32 additions & 0 deletions src/toolchain/BuildFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ export class BuildFlags {
return indirect ? args.flatMap(arg => ["-Xswiftc", arg]) : args;
}

/**
* Get modified swift arguments with new arguments for disabling
* sandboxing if the `swift.disableSandbox` setting is enabled.
*
* @param args original commandline arguments
*/
withDisableSandboxFlags(args: string[]): string[] {
if (!configuration.disableSandbox) {
return args;
}
switch (args[0]) {
case "package": {
return [args[0], ...BuildFlags.disableSandboxFlags(), ...args.slice(1)];
}
case "build":
case "run":
case "test": {
return [...args, ...BuildFlags.disableSandboxFlags()];
}
default:
// Do nothing for other commands
return args;
}
}

/**
* Get flags for disabling sandboxing when running SwiftPM
*/
static disableSandboxFlags(): string[] {
return ["--disable-sandbox", "-Xswiftc", "-disable-sandbox"];
}

/**
* Filter argument list
* @param args argument list
Expand Down
5 changes: 4 additions & 1 deletion src/toolchain/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ export class SwiftToolchain {
private static async getSwiftTargetInfo(): Promise<SwiftTargetInfo> {
try {
try {
const { stdout } = await execSwift(["-print-target-info"], "default");
const { stdout } = await execSwift(
["-print-target-info", ...BuildFlags.disableSandboxFlags()],
"default"
);
const targetInfo = JSON.parse(stdout.trimEnd()) as SwiftTargetInfo;
if (targetInfo.compilerVersion) {
return targetInfo;
Expand Down
1 change: 1 addition & 0 deletions src/utilities/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export async function execSwift(
}
if (toolchain !== "default") {
args = toolchain.buildFlags.withSwiftSDKFlags(args);
args = toolchain.buildFlags.withDisableSandboxFlags(args);
}
if (Object.keys(configuration.swiftEnvironmentVariables).length > 0) {
// when adding environment vars we either combine with vars passed
Expand Down
23 changes: 16 additions & 7 deletions test/suite/tasks/SwiftTaskProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ suite("SwiftTaskProvider Test Suite", () => {

test("Exit code on success", async () => {
const task = createSwiftTask(
["--help"],
"help",
["build", "--show-bin-path"],
"show bin path",
{ cwd: workspaceFolder.uri, scope: vscode.TaskScope.Workspace },
toolchain
);
Expand Down Expand Up @@ -84,7 +84,12 @@ suite("SwiftTaskProvider Test Suite", () => {
new vscode.CancellationTokenSource().token
);
const task = tasks.find(t => t.name === "Build All (defaultPackage)");
assert.equal(task?.detail, "swift build --build-tests -Xswiftc -diagnostic-style=llvm");
assert.equal(
task?.detail?.startsWith(
"swift build --build-tests -Xswiftc -diagnostic-style=llvm"
),
true
);
});

test("includes product debug task", async () => {
Expand All @@ -94,8 +99,10 @@ suite("SwiftTaskProvider Test Suite", () => {
);
const task = tasks.find(t => t.name === "Build Debug PackageExe (defaultPackage)");
assert.equal(
task?.detail,
"swift build --product PackageExe -Xswiftc -diagnostic-style=llvm"
task?.detail?.startsWith(
"swift build --product PackageExe -Xswiftc -diagnostic-style=llvm"
),
true
);
});

Expand All @@ -106,8 +113,10 @@ suite("SwiftTaskProvider Test Suite", () => {
);
const task = tasks.find(t => t.name === "Build Release PackageExe (defaultPackage)");
assert.equal(
task?.detail,
"swift build -c release --product PackageExe -Xswiftc -diagnostic-style=llvm"
task?.detail?.startsWith(
"swift build -c release --product PackageExe -Xswiftc -diagnostic-style=llvm"
),
true
);
});
});
Expand Down