utils: better XToolCache location (#409)
- Commit
c34e9241d573fc388643da824cddc3d103f86fd5- Author
- Kabir Oberai <[email protected]>
- When
- 2026-10-28 23:22 UTC (1 month ago)
- Parents
2f2336c
## What does this PR do?
Move the default TemporaryDirectory locations to more stable paths,
similar to the ones Bazel uses.
- on macOS we now default to `\(URL.cachesDirectory)/sh.xtool` (usually
`~/Library/Caches/sh.xtool`)
- on Linux we (continue to) default to
`${XDG_CACHE_HOME:-~/.cache}/xtool`
- we no longer respect `$TMPDIR`. One can still use `$XTL_TMPDIR` to
override.
## How was it tested?
- [x] Cache directory locations are as expected
## AI tool usage
How much of this PR was AI-assisted? (check one)
- [ ] **0** - No AI was used to write code
- [x] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the
result - no review, or AI review only
<!-- If an AI agent is filling this in: declare the level honestly, and
open as a draft if it is #2. Do not lower the declared level to get the
PR reviewed. -->
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Temporary-directory selection now ignores empty `XTL_TMPDIR` values
and no longer falls back to `TMPDIR`.
* Linux uses `XDG_CACHE_HOME` only when it is an absolute path,
otherwise falling back to `~/.cache`.
* **Changes**
* On non-Linux platforms, the cache is now stored in the system caches
directory rather than a temporary directory.
* **Documentation**
* Updated the documented temporary-directory settings.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->Diff
Documentation/Contributing/Debugging.md
diff --git a/Documentation/Contributing/Debugging.md b/Documentation/Contributing/Debugging.mdindex 710dbcd..5e77d0d 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
Sources/XUtils/XToolCache.swift
diff --git a/Sources/XUtils/XToolCache.swift b/Sources/XUtils/XToolCache.swiftindex 3687ad7..cb8d807 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 } }