Skip to content

Add macOS support for Swiftly toolchain management #1673

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
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- Add macOS support for Swiftly toolchain management ([#1673](https://github.com/swiftlang/vscode-swift/pull/1673))
- Show revision hash or local/editing keyword in project panel dependency descriptions ([#1667](https://github.com/swiftlang/vscode-swift/pull/1667))

## 2.6.2 - 2025-07-02
Expand Down
5 changes: 2 additions & 3 deletions src/toolchain/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ export class SwiftToolchain {
* @returns an array of toolchain paths
*/
public static async getSwiftlyToolchainInstalls(): Promise<string[]> {
// Swiftly is only available on Linux right now
// TODO: Add support for macOS
if (process.platform !== "linux") {
// Swiftly is available on Linux and macOS
if (process.platform !== "linux" && process.platform !== "darwin") {
return [];
}
try {
Expand Down
5 changes: 3 additions & 2 deletions src/ui/ToolchainSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ async function getQuickPickItems(
}
// Various actions that the user can perform (e.g. to install new toolchains)
const actionItems: ActionItem[] = [];
if (process.platform === "linux") {
if (process.platform === "linux" || process.platform === "darwin") {
const platformName = process.platform === "linux" ? "Linux" : "macOS";
actionItems.push({
type: "action",
label: "$(swift-icon) Install Swiftly for toolchain management...",
detail: "Install https://swiftlang.github.io/swiftly to manage your toolchains on Linux",
detail: `Install https://swiftlang.github.io/swiftly to manage your toolchains on ${platformName}`,
run: installSwiftly,
});
}
Expand Down
100 changes: 100 additions & 0 deletions test/unit-tests/toolchain/toolchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,104 @@ suite("SwiftToolchain Unit Test Suite", () => {
await expect(SwiftToolchain.findXcodeInstalls()).to.eventually.be.empty;
});
});

suite("getSwiftlyToolchainInstalls()", () => {
const mockedEnv = mockGlobalValue(process, "env");

test("returns installed toolchains on Linux", async () => {
mockedPlatform.setValue("linux");
const mockHomeDir = "/home/user/.swiftly";
mockedEnv.setValue({ SWIFTLY_HOME_DIR: mockHomeDir });

mockFS({
[path.join(mockHomeDir, "config.json")]: JSON.stringify({
installedToolchains: ["swift-5.9.0", "swift-6.0.0"],
}),
});

const toolchains = await SwiftToolchain.getSwiftlyToolchainInstalls();
expect(toolchains).to.deep.equal([
path.join(mockHomeDir, "toolchains", "swift-5.9.0"),
path.join(mockHomeDir, "toolchains", "swift-6.0.0"),
]);
});

test("returns installed toolchains on macOS", async () => {
mockedPlatform.setValue("darwin");
const mockHomeDir = "/Users/user/.swiftly";
mockedEnv.setValue({ SWIFTLY_HOME_DIR: mockHomeDir });

mockFS({
[path.join(mockHomeDir, "config.json")]: JSON.stringify({
installedToolchains: ["swift-5.9.0", "swift-6.0.0"],
}),
});

const toolchains = await SwiftToolchain.getSwiftlyToolchainInstalls();
expect(toolchains).to.deep.equal([
path.join(mockHomeDir, "toolchains", "swift-5.9.0"),
path.join(mockHomeDir, "toolchains", "swift-6.0.0"),
]);
});

test("returns empty array when SWIFTLY_HOME_DIR is not set", async () => {
mockedPlatform.setValue("linux");
mockedEnv.setValue({});

const toolchains = await SwiftToolchain.getSwiftlyToolchainInstalls();
expect(toolchains).to.be.empty;
});

test("returns empty array when config file does not exist", async () => {
mockedPlatform.setValue("linux");
const mockHomeDir = "/home/user/.swiftly";
mockedEnv.setValue({ SWIFTLY_HOME_DIR: mockHomeDir });

mockFS({});

await expect(SwiftToolchain.getSwiftlyToolchainInstalls()).to.be.rejectedWith(
"Failed to retrieve Swiftly installations from disk."
);
});

test("returns empty array when config has no installedToolchains", async () => {
mockedPlatform.setValue("linux");
const mockHomeDir = "/home/user/.swiftly";
mockedEnv.setValue({ SWIFTLY_HOME_DIR: mockHomeDir });

mockFS({
[path.join(mockHomeDir, "config.json")]: JSON.stringify({
someOtherProperty: "value",
}),
});

const toolchains = await SwiftToolchain.getSwiftlyToolchainInstalls();
expect(toolchains).to.be.empty;
});

test("returns empty array on Windows", async () => {
mockedPlatform.setValue("win32");
const toolchains = await SwiftToolchain.getSwiftlyToolchainInstalls();
expect(toolchains).to.be.empty;
});

test("filters out non-string toolchain entries", async () => {
mockedPlatform.setValue("linux");
const mockHomeDir = "/home/user/.swiftly";
mockedEnv.setValue({ SWIFTLY_HOME_DIR: mockHomeDir });

mockFS({
[path.join(mockHomeDir, "config.json")]: JSON.stringify({
installedToolchains: ["swift-5.9.0", null, "swift-6.0.0", 123, "swift-6.1.0"],
}),
});

const toolchains = await SwiftToolchain.getSwiftlyToolchainInstalls();
expect(toolchains).to.deep.equal([
path.join(mockHomeDir, "toolchains", "swift-5.9.0"),
path.join(mockHomeDir, "toolchains", "swift-6.0.0"),
path.join(mockHomeDir, "toolchains", "swift-6.1.0"),
]);
});
});
});