From 799e8da6a58ad5e3807583b4ca4dc5cc38a7c9cf Mon Sep 17 00:00:00 2001 From: Bassam Khouri Date: Wed, 5 Nov 2025 11:24:41 -0500 Subject: [PATCH] Ensure `--use` will select the toolchain If a toolchain was already installed when running `swiftly install --use`, said toolchain was not being set to be used as a different code path was followed. Update the code path to set the toolchain when `--use` is selected even if the toolchain is already installed. --- Sources/Swiftly/Install.swift | 8 +++++++- Tests/SwiftlyTests/InstallTests.swift | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Sources/Swiftly/Install.swift b/Sources/Swiftly/Install.swift index 5e7ba2d2..f59e9f69 100644 --- a/Sources/Swiftly/Install.swift +++ b/Sources/Swiftly/Install.swift @@ -254,8 +254,14 @@ struct Install: SwiftlyCommand { let installInfo = InstallInfo( version: version, alreadyInstalled: true ) + let pathChanged = if useInstalledToolchain { + try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config) + } else { + false + } + try await ctx.output(installInfo) - return (nil, false) + return (nil, pathChanged) } // Ensure the system is set up correctly before downloading it. Problems that prevent installation diff --git a/Tests/SwiftlyTests/InstallTests.swift b/Tests/SwiftlyTests/InstallTests.swift index 4d387d60..728f08e2 100644 --- a/Tests/SwiftlyTests/InstallTests.swift +++ b/Tests/SwiftlyTests/InstallTests.swift @@ -264,6 +264,25 @@ import Testing try await SwiftlyTests.validateInUse(expected: .newStable) } + /// Verify that the pre-installed toolchain will be marked as in-use if the --use flag is specified. + @Test(.mockedSwiftlyVersion(), .testHomeMockedToolchain()) func installUseFlagOnAlreadyInstalledToolchain() async throws { + // GIVEN we install an old toolchain + try await SwiftlyTests.installMockedToolchain(toolchain: .oldStable) + // AND we use the toolchain + try await SwiftlyTests.runCommand(Use.self, ["use", ToolchainVersion.oldStable.name]) + // THEN the old toolchain should be in use/selected + try await SwiftlyTests.validateInUse(expected: .oldStable) + // GIVEN a new toolchain is installled without `--use` + try await SwiftlyTests.installMockedToolchain(selector: ToolchainVersion.newStable.name) + // THEN the old toolchain is in use/selected + try await SwiftlyTests.validateInUse(expected: .oldStable) + + // AND GIVEN we installing the new toolchain again with `-use` + try await SwiftlyTests.installMockedToolchain(selector: ToolchainVersion.newStable.name, args: ["--use"]) + // THEN the new toolchain should be in use/selected + try await SwiftlyTests.validateInUse(expected: .newStable) + } + /// Verify that xcode can't be installed like regular toolchains @Test(.testHomeMockedToolchain()) func installXcode() async throws { try await #expect(throws: SwiftlyError.self) {