Skip to content

Add swift.packageArguments setting #1342

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 7 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@
"items": {
"type": "string"
},
"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."
"markdownDescription": "Additional arguments to pass to `swift build` and `swift test`. 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."
},
"swift.packageArguments": {
"type": "array",
"default": [],
"items": {
"type": "string"
},
"markdownDescription": "Additional arguments to pass to swift commands that do package resolution, such as `swift package resolve`, `swift package update`, `swift build` and `swift test`. Keys and values should be provided as individual entries in the list."
},
"swift.additionalTestArguments": {
"type": "array",
Expand Down
1 change: 1 addition & 0 deletions src/commands/dependencies/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { FolderContext } from "../../FolderContext";
import { createSwiftTask, SwiftTaskProvider } from "../../tasks/SwiftTaskProvider";
import { WorkspaceContext } from "../../WorkspaceContext";
import { executeTaskWithUI, updateAfterError } from "../utilities";
import configuration from "../../configuration";

/**
* Executes a {@link vscode.Task task} to resolve this package's dependencies.
Expand Down
1 change: 1 addition & 0 deletions src/commands/dependencies/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { FolderContext } from "../../FolderContext";
import { WorkspaceContext } from "../../WorkspaceContext";
import { createSwiftTask, SwiftTaskProvider } from "../../tasks/SwiftTaskProvider";
import { executeTaskWithUI, updateAfterError } from "./../utilities";
import configuration from "../../configuration";

/**
* Executes a {@link vscode.Task task} to update this package's dependencies.
Expand Down
1 change: 1 addition & 0 deletions src/commands/resetPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { FolderContext } from "../FolderContext";
import { createSwiftTask, SwiftTaskProvider } from "../tasks/SwiftTaskProvider";
import { WorkspaceContext } from "../WorkspaceContext";
import { executeTaskWithUI } from "./utilities";
import configuration from "../configuration";

/**
* Executes a {@link vscode.Task task} to reset the complete cache/build directory.
Expand Down
4 changes: 4 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ const configuration = {
get buildArguments(): string[] {
return vscode.workspace.getConfiguration("swift").get<string[]>("buildArguments", []);
},
/** swift package arguments */
get packageArguments(): string[] {
return vscode.workspace.getConfiguration("swift").get<string[]>("packageArguments", []);
},
/** thread/address sanitizer */
get sanitizer(): string {
return vscode.workspace.getConfiguration("swift").get<string>("sanitizer", "off");
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/SwiftTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export function createSwiftTask(
cmdEnv: { [key: string]: string } = {}
): SwiftTask {
const swift = toolchain.getToolchainExecutable("swift");
args = toolchain.buildFlags.withSwiftSDKFlags(args);
args = toolchain.buildFlags.withSwiftPackageFlags(toolchain.buildFlags.withSwiftSDKFlags(args));

// Add relative path current working directory
const cwd = config.cwd.fsPath;
Expand Down
14 changes: 14 additions & 0 deletions src/toolchain/BuildFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ export class BuildFlags {
}
}

withSwiftPackageFlags(args: string[]): string[] {
switch (args[0]) {
case "package":
if (args[1] === "resolve" || args[1] === "update") {
return [...args, ...configuration.packageArguments];
}
return args;
case "build":
return [...args, ...configuration.packageArguments];
default:
return args;
}
}

/**
* Get SDK flags for SwiftPM
*/
Expand Down
14 changes: 14 additions & 0 deletions test/integration-tests/WorkspaceContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ suite("WorkspaceContext Test Suite", () => {

suiteTeardown(async () => {
await swiftConfig.update("buildArguments", undefined);
await swiftConfig.update("packageArguments", undefined);
await swiftConfig.update("path", undefined);
await swiftConfig.update("diagnosticsStyle", undefined);
});
Expand Down Expand Up @@ -163,6 +164,19 @@ suite("WorkspaceContext Test Suite", () => {
await swiftConfig.update("buildArguments", []);
});

test("Package Arguments Settings", async () => {
const folder = workspaceContext.folders.find(
f => f.folder.fsPath === packageFolder.fsPath
);
assert(folder);
await swiftConfig.update("diagnosticsStyle", undefined);
await swiftConfig.update("packageArguments", ["--replace-scm-with-registry"]);
const buildAllTask = createBuildAllTask(folder);
const execution = buildAllTask.execution as SwiftExecution;
assertContainsArg(execution, "--replace-scm-with-registry");
await swiftConfig.update("packageArguments", []);
});

test("Swift Path", async () => {
/* Temporarily disabled (need swift path to update immediately for this to work)
const folder = workspaceContext.folders.find(
Expand Down
Loading