Skip to content

Commit 8000fdb

Browse files
committed
Ignore --scratch-path when calling swift package plugin --list
If the user has a `swift.buildPath` set, and they've got backgroundCompilation turned on, the extension will attempt to list their package's plugins with a `--scratch-path`, which `swift package plugin --list` does not accept. This causes the command to fail. Omit this argument from the command.
1 parent 75d1511 commit 8000fdb

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

src/extension.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,9 @@ function handleFolderEvent(logger: SwiftLogger): (event: FolderEvent) => Promise
222222
// function called when a folder is added. I broke this out so we can trigger it
223223
// without having to await for it.
224224
async function folderAdded(folder: FolderContext, workspace: WorkspaceContext) {
225-
if (
226-
!configuration.folder(folder.workspaceFolder).disableAutoResolve ||
227-
configuration.backgroundCompilation.enabled
228-
) {
225+
const disableAutoResolve = configuration.folder(folder.workspaceFolder).disableAutoResolve;
226+
const backgroundCompilationEnabled = configuration.backgroundCompilation.enabled;
227+
if (!disableAutoResolve || backgroundCompilationEnabled) {
229228
// if background compilation is set then run compile at startup unless
230229
// this folder is a sub-folder of the workspace folder. This is to avoid
231230
// kicking off compile for multiple projects at the same time

src/toolchain/BuildFlags.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,27 @@ export class BuildFlags {
4444
private withSwiftSDKFlags(args: string[]): string[] {
4545
switch (args[0]) {
4646
case "package": {
47-
const subcommand = args.splice(0, 2).concat(this.buildPathFlags());
48-
switch (subcommand[1]) {
49-
case "dump-symbol-graph":
50-
case "diagnose-api-breaking-changes":
51-
case "resolve": {
52-
// These two tools require building the package, so SDK
53-
// flags are needed. Destination control flags are
54-
// required to be placed before subcommand options.
55-
return [...subcommand, ...this.swiftpmSDKFlags(), ...args];
47+
switch (args[1]) {
48+
case "plugin":
49+
// Don't append build path flags for `swift package plugin` commands
50+
return args;
51+
default: {
52+
const subcommand = args.splice(0, 2).concat(this.buildPathFlags());
53+
switch (subcommand[1]) {
54+
case "dump-symbol-graph":
55+
case "diagnose-api-breaking-changes":
56+
case "resolve": {
57+
// These two tools require building the package, so SDK
58+
// flags are needed. Destination control flags are
59+
// required to be placed before subcommand options.
60+
return [...subcommand, ...this.swiftpmSDKFlags(), ...args];
61+
}
62+
default:
63+
// Other swift-package subcommands operate on the host,
64+
// so it doesn't need to know about the destination.
65+
return subcommand.concat(args);
66+
}
5667
}
57-
default:
58-
// Other swift-package subcommands operate on the host,
59-
// so it doesn't need to know about the destination.
60-
return subcommand.concat(args);
6168
}
6269
}
6370
case "build":
@@ -212,6 +219,9 @@ export class BuildFlags {
212219
const disableSandboxFlags = ["--disable-sandbox", "-Xswiftc", "-disable-sandbox"];
213220
switch (args[0]) {
214221
case "package": {
222+
if (args[1] === "plugin") {
223+
return args;
224+
}
215225
return [args[0], ...disableSandboxFlags, ...args.slice(1)];
216226
}
217227
case "build":

test/unit-tests/toolchain/BuildFlags.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
import { expect } from "chai";
15+
import { beforeEach } from "mocha";
1516
import * as sinon from "sinon";
1617

1718
import configuration from "@src/configuration";
@@ -242,6 +243,13 @@ suite("BuildFlags Test Suite", () => {
242243

243244
suite("withAdditionalFlags", () => {
244245
const sdkConfig = mockGlobalValue(configuration, "sdk");
246+
const buildPathConfig = mockGlobalValue(configuration, "buildPath");
247+
248+
beforeEach(() => {
249+
buildPathConfig.setValue("");
250+
sdkConfig.setValue("");
251+
sandboxConfig.setValue(false);
252+
});
245253

246254
test("package", () => {
247255
for (const sub of ["dump-symbol-graph", "diagnose-api-breaking-changes", "resolve"]) {
@@ -284,6 +292,29 @@ suite("BuildFlags Test Suite", () => {
284292
]);
285293
});
286294

295+
test("package plugin", () => {
296+
buildPathConfig.setValue("");
297+
sdkConfig.setValue("");
298+
expect(
299+
buildFlags.withAdditionalFlags(["package", "plugin", "my-plugin"])
300+
).to.deep.equal(["package", "plugin", "my-plugin"]);
301+
302+
buildPathConfig.setValue("/some/build/path");
303+
expect(
304+
buildFlags.withAdditionalFlags(["package", "plugin", "my-plugin", "--verbose"])
305+
).to.deep.equal(["package", "plugin", "my-plugin", "--verbose"]);
306+
307+
sdkConfig.setValue("/some/full/path/to/sdk");
308+
expect(
309+
buildFlags.withAdditionalFlags(["package", "plugin", "my-plugin"])
310+
).to.deep.equal(["package", "plugin", "my-plugin"]);
311+
312+
sandboxConfig.setValue(true);
313+
expect(
314+
buildFlags.withAdditionalFlags(["package", "plugin", "my-plugin"])
315+
).to.deep.equal(["package", "plugin", "my-plugin"]);
316+
});
317+
287318
test("build", () => {
288319
sdkConfig.setValue("");
289320
expect(

0 commit comments

Comments
 (0)