← Back to Pull requests

Fix installations going out of sync #607

Merged
MahdiBM merged 3 files (64 lines) on Oct 19, 2026 14:20 UTC
View on GitHub
MahdiBM MahdiBM commented on Oct 02, 2026 12:14 UTC
Author

Fixes https://github.com/swiftlang/swiftly/issues/439.

The issue manifests like so: https://github.com/swift-dns/swift-idna/actions/runs/36990986233/job/110786900189?pr=53 (similar to the linked issue above).

Claude's report

Swiftly considers a toolchain installed as soon as it is listed in installedToolchains of config.json:

https://github.com/swiftlang/swiftly/blob/c8cf2e35bfca9e7c219d840cb605f81bcf009256/Sources/Swiftly/Install.swift#L253

If the toolchain's files are gone while config.json still lists it (removed by hand, a different SWIFTLY_TOOLCHAINS_DIR, or a CI cache that restored ~/.swiftly but not the toolchains directory), swiftly install reports it as already installed. swiftly use then fails while listing the toolchain's usr/bin to set up the proxies:

https://github.com/swiftlang/swiftly/blob/c8cf2e35bfca9e7c219d840cb605f81bcf009256/Sources/Swiftly/Install.swift#L161-L162

$ swiftly install 6.3
Fetching the latest stable Swift 6.3 release...
Swift 6.3.3 is already installed
$ swiftly use 6.3
Error: The folder “bin” doesn’t exist.

This patch makes swiftly install install such a toolchain again instead of reporting it as already installed:

$ swiftly install 6.3
Fetching the latest stable Swift 6.3 release...
Toolchain Swift 6.3.3 could not be located in /Users/runner/Library/Developer/Toolchains/swift-6.3.3-RELEASE.xctoolchain/usr/bin.
Installing Swift 6.3.3
...
Swift 6.3.3 is installed successfully!

It also makes the proxy setup of swiftly use and swiftly link report the missing toolchain, the way proxyEnvironment already does, instead of surfacing the Foundation error.

Both cases are covered by new tests.

Diff: 64 lines (+54 / -10) across 3 files Reviewer: cmcgee1024 Review duration: 24606m
All Vouch review confidence checks passed
Residual risk 0.09 = Change Risk (0.36) × (1 − Review Confidence (0.75)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.36 · 64 lines diff across 3 files
0.36
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.52 · 5 substantive comment(s)
0.52
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
75%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.250
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
15 items
cmcgee1024 cmcgee1024 commented on code on Oct 15, 2026 17:07 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {
+            let toolchainBinDir = try await Swiftly.currentPlatform.findToolchainBinDir(ctx, version)
+
+            if try await fs.exists(atPath: toolchainBinDir) {
+                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, pathChanged)
             }
 
-            try await ctx.output(installInfo)
-            return (nil, pathChanged)
+            await ctx.message("Toolchain \(version) could not be located in \(toolchainBinDir).")

suggestion (non-blocking): This can give a more actionable error message indicating that swiftly uninstall <version> followed by swiftly install <version> could get everything back in order for this toolchain version.

cmcgee1024 cmcgee1024 commented on code on Oct 15, 2026 17:09 UTC
Code Review
Tests/SwiftlyTests/InstallTests.swift:1
@@ -239,6 +239,26 @@ import Testing
         #expect(before == after)
     }
 
+    /// Tests that a toolchain that is installed according to the config, but missing from disk, is installed again.

praise: It's always good to see another test for edge cases, especially ones that have a direct user impact.

cmcgee1024 cmcgee1024 approved these changes on Oct 15, 2026 17:10 UTC
Approved Reviewer

Approved these changes.

etcwilde etcwilde commented on code on Oct 15, 2026 17:12 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {
+            let toolchainBinDir = try await Swiftly.currentPlatform.findToolchainBinDir(ctx, version)
+
+            if try await fs.exists(atPath: toolchainBinDir) {
+                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, pathChanged)
             }
 
-            try await ctx.output(installInfo)
-            return (nil, pathChanged)
+            await ctx.message("Toolchain \(version) could not be located in \(toolchainBinDir).")

Does this compile? This function is supposed to return a tuple but I don't see this returning anything?

etcwilde etcwilde commented on code on Oct 15, 2026 17:14 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {

Once we have the return value, I think that a guard here is easier to read.
e.g:

guard config.installedToolchains.contains(version) else {
  await ctx.message("Toolchain \(version) could not be located in \(toolchainBinDir)")
  <return (nil, false)>
}

// everything else ...

MahdiBM MahdiBM commented on code on Oct 15, 2026 18:16 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {
+            let toolchainBinDir = try await Swiftly.currentPlatform.findToolchainBinDir(ctx, version)
+
+            if try await fs.exists(atPath: toolchainBinDir) {
+                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, pathChanged)
             }
 
-            try await ctx.output(installInfo)
-            return (nil, pathChanged)
+            await ctx.message("Toolchain \(version) could not be located in \(toolchainBinDir).")

Hmm there is only 1 func message in the whole repo, it's this one, and it doesn't return anything. You must be thinking about some other function I guess?
Should I change this func message's behavior?

MahdiBM MahdiBM commented on code on Oct 15, 2026 18:28 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {

I mean the suggested code wouldn't really match the logic there 🤔.

MahdiBM MahdiBM commented on code on Oct 15, 2026 18:33 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {
+            let toolchainBinDir = try await Swiftly.currentPlatform.findToolchainBinDir(ctx, version)
+
+            if try await fs.exists(atPath: toolchainBinDir) {
+                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, pathChanged)
             }
 
-            try await ctx.output(installInfo)
-            return (nil, pathChanged)
+            await ctx.message("Toolchain \(version) could not be located in \(toolchainBinDir).")

Done

etcwilde etcwilde commented on code on Oct 15, 2026 18:45 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {

doh, I misread the indentation level.

etcwilde etcwilde commented on code on Oct 15, 2026 18:46 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -250,18 +256,24 @@ struct Install: SwiftlyCommand {
         assumeYes: Bool,
         progressFile: FilePath? = nil
     ) async throws -> (postInstall: String?, pathChanged: Bool) {
-        guard !config.installedToolchains.contains(version) else {
-            let installInfo = InstallInfo(
-                version: version, alreadyInstalled: true
-            )
-            let pathChanged = if useInstalledToolchain {
-                try await Use.execute(ctx, version, globalDefault: false, verbose: verbose, &config)
-            } else {
-                false
+        if config.installedToolchains.contains(version) {
+            let toolchainBinDir = try await Swiftly.currentPlatform.findToolchainBinDir(ctx, version)
+
+            if try await fs.exists(atPath: toolchainBinDir) {
+                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, pathChanged)
             }
 
-            try await ctx.output(installInfo)
-            return (nil, pathChanged)
+            await ctx.message("Toolchain \(version) could not be located in \(toolchainBinDir).")

No, execute returns the (String?, Bool). I was thinking that this was at the bottom of the function since I misread the indentation level.

MahdiBM MahdiBM commented on Oct 16, 2026 07:47 UTC
Author

Looks like some auto-formatting changes sneaked into the last commit (presumably due to my VSCode auto-formatting the file, as I've set it to, using swift-format). I've amended those formatting changes.

cmcgee1024 cmcgee1024 commented on code on Oct 16, 2026 20:11 UTC
Code Review
Sources/Swiftly/Install.swift:1
@@ -159,6 +159,12 @@ struct Install: SwiftlyCommand {
             let swiftlyBinDirContents =
                 (try? await fs.ls(atPath: swiftlyBinDir)) ?? [String]()
             let toolchainBinDir = try await Swiftly.currentPlatform.findToolchainBinDir(ctx, version)
+            guard try await fs.exists(atPath: toolchainBinDir) else {
+                throw SwiftlyError(
+                    message:
+                    "Toolchain \(version) could not be located in \(toolchainBinDir). You can try `swiftly install \(version.name)` to install it again."

issue: this is a bit misleading now because you'd have to uninstall the toolchain to clear it out of config.json first, and then install it again to download and sync up the config.json with the toolchain directory in the fs.

cmcgee1024 cmcgee1024 requested changes on Oct 16, 2026 20:11 UTC
Changes Requested Reviewer

Requested changes.

cmcgee1024 cmcgee1024 approved these changes on Oct 19, 2026 14:20 UTC
Approved Reviewer

Looks good. Thank you.

Reviewers
Assignees
MahdiBM MahdiBM
Labels
risk: low confidence: 75.1% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 35.9% (MEDIUM) · Higher = larger blast radius
35.9%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 75.1% · Lower = rushed or shallow review
75.1%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 8.9% · Safe to merge
8.9%