Skip to content

Commit 506762c

Browse files
committed
Fix Swiftly toolchain selection
1 parent 6aca05f commit 506762c

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/toolchain/swiftly.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ export class Swiftly {
101101
return response.toolchains.map(t => t.version.name);
102102
} catch (error) {
103103
outputChannel?.appendLine(`Failed to retrieve Swiftly installations: ${error}`);
104-
throw new Error(
105-
`Failed to retrieve Swiftly installations from disk: ${(error as Error).message}`
106-
);
104+
return [];
107105
}
108106
}
109107

@@ -143,6 +141,13 @@ export class Swiftly {
143141
return inUse.trimEnd();
144142
}
145143

144+
public static async use(version: string): Promise<void> {
145+
if (!this.isSupported()) {
146+
throw new Error("Swiftly is not supported on this platform");
147+
}
148+
await execFile("swiftly", ["use", version]);
149+
}
150+
146151
/**
147152
* Determine if Swiftly is being used to manage the active toolchain and if so, return
148153
* the path to the active toolchain.

src/ui/ToolchainSelection.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,22 @@ export async function showToolchainSelectionQuickPick(activeToolchain: SwiftTool
304304
}
305305
}
306306
// Update the toolchain path
307-
const isUpdated = await setToolchainPath(selected.swiftFolderPath, developerDir);
307+
let swiftPath = selected.swiftFolderPath;
308+
309+
// Handle Swiftly toolchains specially
310+
if (selected.category === "swiftly") {
311+
try {
312+
// Run swiftly use <version> and get the path to the toolchain
313+
await Swiftly.use(selected.label);
314+
const inUseLocation = await Swiftly.inUseLocation("swiftly");
315+
swiftPath = path.join(inUseLocation, "usr", "bin");
316+
} catch (error) {
317+
void vscode.window.showErrorMessage(`Failed to switch Swiftly toolchain: ${error}`);
318+
return;
319+
}
320+
}
321+
322+
const isUpdated = await setToolchainPath(swiftPath, developerDir);
308323
if (isUpdated && selected.onDidSelect) {
309324
await selected.onDidSelect();
310325
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,45 @@ suite("Swiftly Unit Tests", () => {
3838
toolchains: [
3939
{
4040
inUse: true,
41-
installed: true,
4241
isDefault: true,
43-
name: "swift-5.9.0-RELEASE",
4442
version: {
4543
major: 5,
4644
minor: 9,
4745
patch: 0,
46+
name: "swift-5.9.0-RELEASE",
4847
type: "stable",
4948
},
5049
},
5150
{
5251
inUse: false,
53-
installed: true,
5452
isDefault: false,
55-
name: "swift-5.8.0-RELEASE",
5653
version: {
5754
major: 5,
5855
minor: 8,
5956
patch: 0,
57+
name: "swift-5.8.0-RELEASE",
6058
type: "stable",
6159
},
6260
},
6361
{
6462
inUse: false,
65-
installed: false,
6663
isDefault: false,
67-
name: "swift-DEVELOPMENT-SNAPSHOT-2023-10-15-a",
6864
version: {
6965
major: 5,
7066
minor: 10,
7167
branch: "development",
7268
date: "2023-10-15",
69+
name: "swift-DEVELOPMENT-SNAPSHOT-2023-10-15-a",
7370
type: "snapshot",
7471
},
7572
},
7673
],
7774
};
7875

79-
mockUtilities.execFile
80-
.withArgs("swiftly", ["list-available", "--format=json"])
81-
.resolves({
82-
stdout: JSON.stringify(jsonOutput),
83-
stderr: "",
84-
});
76+
mockUtilities.execFile.withArgs("swiftly", ["list", "--format=json"]).resolves({
77+
stdout: JSON.stringify(jsonOutput),
78+
stderr: "",
79+
});
8580

8681
const result = await Swiftly.listAvailableToolchains();
8782

@@ -93,7 +88,7 @@ suite("Swiftly Unit Tests", () => {
9388

9489
expect(mockUtilities.execFile).to.have.been.calledWith("swiftly", ["--version"]);
9590
expect(mockUtilities.execFile).to.have.been.calledWith("swiftly", [
96-
"list-available",
91+
"list",
9792
"--format=json",
9893
]);
9994
});

0 commit comments

Comments
 (0)