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

+72
View File
@@ -68,6 +68,52 @@ wrapper legitimately needs a build tag; a string-joining decision does
not, and gating it behind one would break the testability requirement
elsewhere in the same spec.
## WithRoot: root's family is the real host's, not the simulated one
`WithRoot` sandboxes real filesystem I/O (for `Ensure*`, `FindFile`,
`Explain`) under a directory while `WithPlatform` simulates a
*different* platform's path logic -- e.g. `WithPlatform("linux")` with
`WithRoot(t.TempDir())` on any host, so Linux-shaped path *logic* can be
exercised with real file I/O on whatever host runs the test. The
resolved path being rebased (`p`) is always in `r.platform`'s simulated
family, but `root` itself -- almost always `t.TempDir()`, or otherwise a
literal, real filesystem location the caller chose -- is always in the
REAL host's native family, which does not have to match `r.platform`.
`applyRoot` (via `applyRootRebase` in `join.go`) therefore decomposes
`p` into plain path segments (discarding its own absolute-path root
marker) and re-joins them onto `root` using **root's own family**,
inferred from its content the same way `JoinDirFile` infers a
directory's family -- never from `r.platform`. This is the only
correct choice: `root` must end up as a single, valid, real path the
actual host filesystem can use, regardless of which platform's path
*shape* is being simulated for everything above it.
Two consequences fall out of this and are easy to reintroduce by
accident:
- Any code that joins one more suffix onto an *already-rooted* value
(e.g. `logDirUnix` building `<StateDir>/log`, or
`darwinDesktopStateDir`/`darwinDesktopRuntimeDir`'s fallback building
`<AppDir>/State`/`<AppDir>/Runtime`) must use `JoinDirFile`, not a
hardcoded `unixJoin`/`windowsJoin` -- the already-rooted value's family
now depends on `root`'s family, not on `r.platform`.
- `core/config`'s working-directory layer has the identical concern one
level up: `r.workdirPath` can be a real filesystem path (e.g. under
`WithRoot`'s root) whose family may not match `r.paths.Platform()`.
`paths.JoinDirFileFamily` handles this: it behaves like `JoinDirFile`
whenever `dir` carries its own separator, falling back to
`JoinFamily`'s platform-driven choice only when `dir` is ambiguous
(no separator at all, e.g. the default `"."`).
This was a real, previously-invisible bug (mixed-separator and, in one
override case, outright invalid paths) that macOS/Linux test runs could
never surface, because `t.TempDir()` there happens to already share the
simulated Unix family with whatever was under test. It was only found
by running this package's own test suite on a real Windows host for the
first time (see TODO.md) -- worth remembering next time "verified via
`GOOS=windows go build/go vet`" is mistaken for "verified on Windows."
## Canonical directory vs. search directories
This is the single most important distinction in the API (see
@@ -140,6 +186,32 @@ macOS don't have this ambiguity (there's no "session runtime directory
that may not exist" convention to get wrong there), so `RuntimeDir()`
always succeeds on those platforms.
## macOS Desktop RuntimeDir: $TMPDIR, not a persistent placeholder
Unlike Unix's XDG_RUNTIME_DIR, macOS has no documented per-app ephemeral
runtime directory convention -- but it does hand every process a real
equivalent: `$TMPDIR`, the per-user, securely permissioned (`0700`),
OS-managed directory `confstr(_CS_DARWIN_USER_TEMP_DIR)` resolves and
`NSTemporaryDirectory()` returns. `darwinDesktopRuntimeDir()` prefers it
(joined with the app name) over the previous placeholder,
`~/Library/Application Support/<App>/Runtime` -- a persistent directory
that misrepresented "ephemeral" -- falling back to that placeholder only
when `TMPDIR` is absent from the resolved environment (a
simulated/test environment that never set it; a real macOS process
always has it set).
## LogAliasDir is resolved separately from being created
`LogAliasDir()` (the optional `~/.local/log/<app>` convenience symlink
target, spec section 39) stays a pure computation like every other
`*Dir()` method -- resolving it never touches the filesystem. Creating
the actual symlink is `EnsureLogAlias()`'s job, kept separate and
explicit for the same reason every other `Ensure*` method is separate
from its `*Dir()` counterpart (see "No implicit filesystem mutation" in
the package doc comment): a caller that only wants to know the path
(e.g. to show it in `doctor`/`Explain` output) should never risk
creating a symlink as a side effect.
## Unix system-scope directories follow each OS's own convention
Linux SystemScope uses the FHS layout (`/etc`, `/var/lib`, `/var/cache`,