Refine macOS/LogAlias platform tuning, fix a real WithRoot bug found on Windows
Platform tuning:
- macOS Desktop's RuntimeDir() now prefers $TMPDIR (the OS-managed,
per-user ephemeral directory NSTemporaryDirectory() returns) over
the previous ~/Library/Application Support/<App>/Runtime placeholder.
- Resolver.EnsureLogAlias() creates the optional LogAliasDir() symlink
to LogDir(), idempotently, leaving any pre-existing entry alone.
Verified the SHGetKnownFolderPath fallback (knownfolders_windows.go)
on a real Windows host for the first time: with APPDATA/LOCALAPPDATA/
PROGRAMDATA all unset, it correctly resolves the real known folders.
go vet's one unsafeptr finding there is a documented, permanent,
unavoidable characteristic of this raw-syscall pattern, not a bug.
That same real-Windows run surfaced a genuine, previously-invisible
bug: WithRoot assumed root's own address family always matched the
*simulated* platform's, which silently broke (mixed separators, or an
outright invalid mid-path drive letter) whenever the real host's
native family differed from what WithPlatform was simulating -- e.g.
WithPlatform("linux") with WithRoot(t.TempDir()) on a real Windows
host. This was invisible on macOS/Linux, where t.TempDir() happens to
already share the simulated Unix family.
Fixed applyRoot (now applyRootRebase) to decompose the resolved path
into segments and re-join them onto root using root's own family,
inferred from its content like JoinDirFile already does, not from the
simulated platform. Two call sites chaining onto an already-rooted
value (logDirUnix, darwin.go's State/Runtime fallback) switched from a
hardcoded unixJoin to JoinDirFile for the same reason. core/config's
working-directory layer had the identical bug one level up; fixed via
a new paths.JoinDirFileFamily.
TODO.md/ARCHITECTURE.md/README.md updated accordingly; core/TODO.md's
remaining items are now all explicitly out-of-scope or deliberately
deprioritized, not open work.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e6b5324ad3
commit
da2ec85381
13 files changed
+515
-70
No files matched your search
+81
-14
@@ -76,6 +76,22 @@ func JoinFamily(platform string, elems ...string) string {
|
||||
return unixJoin(elems...)
|
||||
}
|
||||
|
||||
// JoinDirFileFamily joins dir and name like JoinDirFile (inferring the
|
||||
// family from dir's own content when it contains a separator of either
|
||||
// kind), falling back to platform's family (as JoinFamily does) only
|
||||
// when dir is ambiguous -- has no separator of its own to infer from,
|
||||
// such as a bare "." (core/config's default working-directory layer
|
||||
// before WithWorkdirPath overrides it). Prefer this over JoinFamily
|
||||
// whenever dir might come from a real filesystem path (e.g. WithRoot's
|
||||
// root, or a caller-supplied override) rather than always being a
|
||||
// hardcoded literal in platform's own family.
|
||||
func JoinDirFileFamily(platform, dir, name string) string {
|
||||
if !strings.ContainsRune(dir, '/') && !strings.ContainsRune(dir, '\\') {
|
||||
return JoinFamily(platform, dir, name)
|
||||
}
|
||||
return JoinDirFile(dir, name)
|
||||
}
|
||||
|
||||
// JoinDirFile joins a directory (as produced by ConfigSearchDirs/
|
||||
// DataSearchDirs/ExecutableSearchDirs, in either address family) with a
|
||||
// plain file name, picking the join style from the directory's own
|
||||
@@ -113,24 +129,75 @@ func splitWindowsRoot(p string) (root, rest string, ok bool) {
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
// applyRootUnix rebases an absolute unix-family path under root, when
|
||||
// root is set. An empty root leaves p untouched.
|
||||
func applyRootUnix(root, p string) string {
|
||||
if root == "" {
|
||||
return p
|
||||
}
|
||||
return unixJoin(root, strings.TrimPrefix(p, "/"))
|
||||
// isWindowsPath reports whether p looks like a Windows-family path
|
||||
// (backslash-separated, no forward slash) -- the same content-based
|
||||
// heuristic JoinDirFile uses, applied here to root rather than to a
|
||||
// resolved category directory.
|
||||
func isWindowsPath(p string) bool {
|
||||
return strings.ContainsRune(p, '\\') && !strings.ContainsRune(p, '/')
|
||||
}
|
||||
|
||||
// applyRootWindows rebases an absolute windows-family path under root,
|
||||
// when root is set. An empty root leaves p untouched.
|
||||
func applyRootWindows(root, p string) string {
|
||||
// pathSegments splits an absolute path p (in the given family) into its
|
||||
// plain path components, discarding p's own absolute-path root marker
|
||||
// (a leading "/" for Unix; a drive letter or UNC share for Windows).
|
||||
func pathSegments(p string, windowsFamily bool) []string {
|
||||
if windowsFamily {
|
||||
rest := p
|
||||
if _, r, ok := splitWindowsRoot(p); ok {
|
||||
rest = r
|
||||
}
|
||||
return splitNonEmpty(rest, '\\')
|
||||
}
|
||||
return splitNonEmpty(strings.TrimPrefix(p, "/"), '/')
|
||||
}
|
||||
|
||||
// dirName returns p's parent directory, picking "/"- or "\"-based
|
||||
// splitting from p's own content (the same heuristic JoinDirFile uses)
|
||||
// rather than the host OS's path/filepath conventions -- a resolved
|
||||
// path's family depends on WithRoot's root, not on the host this code
|
||||
// happens to run on (see applyRootRebase), so path/filepath (or the
|
||||
// stdlib "path" package) would silently pick the wrong separator, or
|
||||
// none at all, whenever the two differ.
|
||||
func dirName(p string) string {
|
||||
sep := byte('/')
|
||||
if isWindowsPath(p) {
|
||||
sep = '\\'
|
||||
}
|
||||
if i := strings.LastIndexByte(p, sep); i >= 0 {
|
||||
return p[:i]
|
||||
}
|
||||
return "."
|
||||
}
|
||||
|
||||
func splitNonEmpty(s string, sep byte) []string {
|
||||
var out []string
|
||||
for part := range strings.SplitSeq(s, string(sep)) {
|
||||
if part != "" {
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// applyRootRebase rebases p -- an absolute path in the family denoted by
|
||||
// pIsWindows (r.platform's *simulated* family) -- under root, when root
|
||||
// is set. root is always in the REAL host's native family (e.g.
|
||||
// t.TempDir()'s result, or a literal caller-chosen path), which need not
|
||||
// match pIsWindows: WithRoot exists precisely to sandbox real filesystem
|
||||
// I/O while a *different* platform's path logic is being simulated (see
|
||||
// README's "Testing on any host"). p is decomposed into plain segments
|
||||
// and re-joined onto root using root's own family -- inferred from its
|
||||
// content via isWindowsPath, not from pIsWindows -- so the result is a
|
||||
// single, valid, host-usable path regardless of which platform is being
|
||||
// simulated. An empty root leaves p untouched.
|
||||
func applyRootRebase(root, p string, pIsWindows bool) string {
|
||||
if root == "" {
|
||||
return p
|
||||
}
|
||||
_, rest, ok := splitWindowsRoot(p)
|
||||
if !ok {
|
||||
return windowsJoin(root, p)
|
||||
segments := pathSegments(p, pIsWindows)
|
||||
elems := append([]string{root}, segments...)
|
||||
if isWindowsPath(root) {
|
||||
return windowsJoin(elems...)
|
||||
}
|
||||
return windowsJoin(root, rest)
|
||||
return unixJoin(elems...)
|
||||
}
|
||||
Reference in new issue
Block a user