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:
terminarandClaude Sonnet 5 committed 2026-09-20 16:37:30 +02:00
1 parent e6b5324ad3
commit da2ec85381
13 files changed
+515 -70

No files matched your search

+87 -25
View File
@@ -27,24 +27,28 @@ speculatively.
## Platform tuning
- FreeBSD's flavor (`freebsd.go`) is a best-effort first pass. Other BSD
and Unix-family `GOOS` values (`openbsd`, `netbsd`, `dragonfly`,
`solaris`, `illumos`, ...) currently fall back to the Linux flavor
(`flavor()`'s default case) -- verified to compile and behave
sensibly, not verified against each OS's actual convention. Adding a
dedicated flavor table for one of them is a small, additive change
(a new `<os>.go` file plus one `case` in `flavor()`), never a
redesign.
- macOS Desktop's `RuntimeDir()` (`~/Library/Application
Support/<App>/Runtime`) is a placeholder -- macOS has no strong native
convention for a GUI app's ephemeral runtime directory the way
XDG_RUNTIME_DIR does on Linux. Revisit if a real Desktop consumer
needs something more specific (e.g. a `TemporaryItems`-based
location).
- `LogAliasDir()` (spec section 39: `~/.local/log/<app>` as a Unix
convenience symlink target) is resolved but never created -- no
symlink-creation API exists yet. Decide whether this is worth adding
before any consumer actually wants it.
- [x] macOS Desktop's `RuntimeDir()` now prefers `$TMPDIR` (the
OS-managed, per-user, securely permissioned ephemeral directory every
macOS process gets -- what `NSTemporaryDirectory()` returns), the real
native equivalent to XDG_RUNTIME_DIR's role. It falls back to the
previous `~/Library/Application Support/<App>/Runtime` placeholder only
when `TMPDIR` is unset in the resolved environment (simulated/test
environments that never set it).
- [x] `LogAliasDir()` (spec section 39: `~/.local/log/<app>` as a Unix
convenience symlink target) now has a creation API:
`Resolver.EnsureLogAlias()` creates the `LogDir()` symlink if missing,
leaves any pre-existing entry untouched, and is a no-op alias of
`EnsureLogDir()` on Windows/macOS Desktop (where the two already
coincide).
- **Low priority, not an MVP blocker:** FreeBSD's flavor (`freebsd.go`)
is a best-effort first pass. Other BSD and Unix-family `GOOS` values
(`openbsd`, `netbsd`, `dragonfly`, `solaris`, `illumos`, ...) currently
fall back to the Linux flavor (`flavor()`'s default case) -- verified
to compile and behave sensibly, not verified against each OS's actual
convention. Nobody has asked for one of these yet; adding a dedicated
flavor table for one is a small, additive change (a new `<os>.go` file
plus one `case` in `flavor()`), never a redesign, and can wait until a
real consumer needs it.
## Windows
@@ -56,13 +60,71 @@ speculatively.
launcher, spec section 20) is explicitly out of scope for `core` --
path resolution and package activation stay separate concerns (spec
section 20's own instruction).
- The native `SHGetKnownFolderPath` fallback in
`knownfolders_windows.go` has only been verified to compile
(`GOOS=windows go build`/`go vet`), never run on an actual Windows
host. It is a secondary fallback below environment variables, so a
latent bug there would only matter in the edge case where
`APPDATA`/`LOCALAPPDATA`/`PROGRAMDATA` are all unset in the process
environment on real Windows.
- [x] **The native `SHGetKnownFolderPath` fallback has now been run on a
real Windows host** (Windows 10, Go 1.25), not just cross-compiled.
With `APPDATA`/`LOCALAPPDATA`/`PROGRAMDATA` all unset, `ConfigDir()`/
`DataDir()`/system-scope `ConfigDir()` correctly resolved the real
roaming/local/`ProgramData` known folders via the native API (source
reported as `windows-known-folder`), confirmed via `Explain()` across
every category. `go build`/`go test ./...` are clean on real Windows.
`go vet ./...` reports exactly one finding there, and it is expected
and permanent, not a bug: `knownfolders_windows.go:71` converts a
`uintptr` returned via `SHGetKnownFolderPath`'s out-parameter to
`unsafe.Pointer` to walk the UTF-16 string it points to -- `go vet`'s
`unsafeptr` check can never prove this safe (the pointer is written
into memory by the syscall, not derived from a Go pointer in the same
expression), for any formulation of this code, without depending on
`golang.org/x/sys/windows` (deliberately not done, per this module's
minimal-dependency policy -- see the file's own comment). Plain `go
vet ./...` on macOS/Linux never sees this file at all (`//go:build
windows`); only `GOOS=windows go vet ./...`/vet run on a real Windows
host reports it. Verification commands for future reference: `go
build ./... && go test ./...` (clean), `go vet ./...` (clean on
macOS/Linux; one documented, accepted `unsafeptr` finding on Windows).
- [x] **Running on a real Windows host also surfaced and fixed a real,
previously-invisible bug**, unrelated to `SHGetKnownFolderPath`: see
"WithRoot cross-platform-simulation bug" below.
## WithRoot cross-platform-simulation bug (found via real-Windows testing)
`WithRoot` lets tests sandbox real filesystem I/O under a temp directory
while `WithPlatform` simulates a *different* platform's path logic (see
README's "Testing on any host") -- e.g. `WithPlatform("linux")` with
`WithRoot(t.TempDir())` on any host. `root` (typically `t.TempDir()`) is
always in the REAL host's native address family, which does not have to
match the *simulated* platform's family. `applyRoot` previously assumed
they always matched (choosing unix- or windows-style joining purely from
`r.platform`), so root's own separators were never normalized: on a real
Windows host simulating Linux, `ConfigDir()` etc. produced mixed-
separator paths (`C:\...\001/home/test/.config/yet`), and in one case
(an override value that itself already embedded `root`) an outright
invalid path (a mid-string drive letter). This was invisible on
macOS/Linux hosts, where `t.TempDir()` happens to already share the
simulated Unix family with whatever was being tested, and was only
exposed by actually running this package's own test suite on a real
Windows host for the first time (see the `SHGetKnownFolderPath`
verification above) -- **this class of bug is exactly why "verified to
compile via cross-compilation" is not the same as "verified on the real
platform," and is worth remembering before declaring any future
Windows/macOS-specific change done from cross-compilation alone.**
Fixed: `applyRoot` (now `applyRootRebase` in `join.go`) decomposes the
resolved path into plain segments and re-joins them onto `root` using
root's *own* family, inferred from its content (the same technique
`JoinDirFile` already used) -- not `r.platform`'s. Two call sites that
joined an already-rooted value with one more suffix (`logDirUnix`,
`darwinDesktopStateDir`/`darwinDesktopRuntimeDir`'s fallback) were
switched from a hardcoded `unixJoin` to the family-aware `JoinDirFile`
for the same reason. `core/config`'s working-directory layer had the
identical bug one level up (`JoinFamily` decided family from the
simulated platform, not from `r.workdirPath`'s own content when it had
one) -- fixed via a new `paths.JoinDirFileFamily`, which behaves like
`JoinDirFile` whenever `dir` has a separator of its own, falling back to
`JoinFamily`'s platform-driven choice only for an ambiguous bare `dir`
like the default `"."`. See `TestApplyRootRebaseMismatchedFamily` and
`TestJoinDirFileFamily` for the regression coverage (constructing a
mismatched root/platform pair directly, so this no longer depends on
which host CI happens to run on).
## Portability / distribution model