From e61a4a1cc65e645b385a27baf532528e45eec35f Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Wed, 28 Oct 2026 17:51:03 -0400 Subject: [PATCH] utils: better XToolCache location --- Documentation/Contributing/Debugging.md | 2 +- Sources/XUtils/XToolCache.swift | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Documentation/Contributing/Debugging.md b/Documentation/Contributing/Debugging.md index 710dbcd5..5e77d0d5 100644 --- a/Documentation/Contributing/Debugging.md +++ b/Documentation/Contributing/Debugging.md @@ -11,7 +11,7 @@ Some environment variables that help with debugging. Use these as e.g. `XTL_FOO= ### Disk - `XTL_DEBUG_TMP`: Set to `1` to log details when xtool creates/destroys temporary directories. -- `XTL_TMPDIR`: Control the location where xtool stores temporary directories. We also respect `TMPDIR` and (on Linux) `XDG_CACHE_HOME`. +- `XTL_TMPDIR`: Control the location where xtool stores temporary directories. We also respect `XDG_CACHE_HOME` on Linux. ### Networking diff --git a/Sources/XUtils/XToolCache.swift b/Sources/XUtils/XToolCache.swift index 3687ad70..cb8d807d 100644 --- a/Sources/XUtils/XToolCache.swift +++ b/Sources/XUtils/XToolCache.swift @@ -3,20 +3,20 @@ import Foundation package enum XToolCache { package static var directory: URL { let env = ProcessInfo.processInfo.environment - if let tmpdir = env["XTL_TMPDIR"] ?? env["TMPDIR"] { + if let tmpdir = env["XTL_TMPDIR"], !tmpdir.isEmpty { return URL(fileURLWithPath: tmpdir).appendingPathComponent("sh.xtool") } #if os(Linux) - // On Linux, /tmp is commonly a tmpfs mount while ~/.swiftpm lives on ext4. - // Foundation's FileManager.copyItem uses sendfile(2) internally, which returns - // EINVAL when copying between different filesystem types (e.g. tmpfs → ext4). - // This causes `swift sdk install` to fail on distros like Linux Mint (#181). - // Using a cache dir on the home filesystem avoids the cross-fs copy entirely. - let xdgCache = env["XDG_CACHE_HOME"].map { URL(fileURLWithPath: $0) } - ?? FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".cache") - return xdgCache.appendingPathComponent("xtool") + let xdgCache: URL + if let xdgCachePath = env["XDG_CACHE_HOME"], + (xdgCachePath as NSString).isAbsolutePath { + xdgCache = URL(filePath: xdgCachePath) + } else { + xdgCache = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".cache") + } + return xdgCache.appending(component: "xtool") #else - return FileManager.default.temporaryDirectory.appendingPathComponent("sh.xtool") + return URL.cachesDirectory.appending(component: "sh.xtool") #endif } }