Files
core/paths/darwin.go
T
terminarandClaude Sonnet 5 da2ec85381 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>
2026-09-20 16:37:30 +02:00

181 lines
6.2 KiB
Go

package paths
import "yetano.dev/yetano/core"
// macOS is deliberately split by ApplicationKind (spec section 12): CLI
// and Service applications use the same XDG-flavored layout as Linux
// (unix.go, via flavor()'s default case) for consistent cross-platform
// administration; only Desktop (native GUI) applications use these
// ~/Library-based conventions.
//
// Config and Data intentionally share one canonical directory (a native
// macOS app's Application Support directory commonly holds both its
// preferences-adjacent files and its bulk data side by side); State gets
// its own subdirectory so it doesn't collide with either.
func darwinAppSupportBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Application Support"
}
return unixJoin(home, "Library", "Application Support")
}
func darwinCachesBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Caches"
}
return unixJoin(home, "Library", "Caches")
}
func darwinLogsBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Logs"
}
return unixJoin(home, "Library", "Logs")
}
// darwinHomeIfNeeded returns "" (unused) for SystemScope, else the real
// home directory -- avoiding a spurious ErrNoHomeDirectory when
// SystemScope is resolved without HOME set.
func (r *Resolver) darwinHomeIfNeeded() (string, error) {
if r.app.Scope == core.SystemScope {
return "", nil
}
return r.homeDir()
}
func (r *Resolver) darwinDesktopAppDir() (value, error) {
if v, ok := r.overrideValue(ConfigPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinAppSupportBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopConfigDir() (value, error) {
return r.darwinDesktopAppDir()
}
func (r *Resolver) darwinDesktopDataDir() (value, error) {
// Config overrides (WithConfigDir) must not leak into DataDir, so
// DataDir consults its own override tier first.
if v, ok := r.overrideValue(DataPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
return r.darwinDesktopAppDir()
}
func (r *Resolver) darwinDesktopStateDir() (value, error) {
if v, ok := r.overrideValue(StatePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
app, err := r.darwinDesktopAppDir()
if err != nil {
return value{}, err
}
// app.path already applied the root (see applyRootRebase), which can
// be a different address family than this simulated platform -- use
// JoinDirFile, not unixJoin, so the "State" suffix lands in whichever
// family app.path actually ended up in.
return value{path: JoinDirFile(app.path, "State"), source: app.source}, nil
}
func (r *Resolver) darwinDesktopCacheDir() (value, error) {
if v, ok := r.overrideValue(CachePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinCachesBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopLogDir() (value, error) {
if v, ok := r.overrideValue(LogPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinLogsBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
// darwinDesktopRuntimeDir prefers $TMPDIR -- the per-user, securely
// permissioned, OS-managed ephemeral directory macOS itself hands every
// process (confstr(_CS_DARWIN_USER_TEMP_DIR), what NSTemporaryDirectory()
// returns), the closest real native equivalent to XDG_RUNTIME_DIR's
// role. It falls back to the previous Application Support-based location
// only when TMPDIR is unset in the resolved environment (e.g. a
// simulated/test environment that never set it) -- see AGENTS.md TODO
// history: this was a placeholder before TMPDIR was wired in here.
func (r *Resolver) darwinDesktopRuntimeDir() (value, error) {
if v, ok := r.overrideValue(RuntimePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
if tmpdir, ok := lookup(r.env, "TMPDIR"); ok {
return value{path: r.applyRoot(unixJoin(tmpdir, r.app.Name)), source: MacOSNative}, nil
}
app, err := r.darwinDesktopAppDir()
if err != nil {
return value{}, err
}
// See darwinDesktopStateDir: app.path is already rooted and may be in
// either address family, so JoinDirFile picks the right one.
return value{path: JoinDirFile(app.path, "Runtime"), source: app.source}, nil
}
// darwinDesktopBinDir has no native GUI-app convention; it reuses the
// shared Unix bin directory so a Desktop app that also ships a CLI shim
// still has a sensible, documented answer.
func (r *Resolver) darwinDesktopBinDir() (value, error) {
return r.binDirUnix()
}
func (r *Resolver) darwinDesktopConfigLayers() ([]layer, error) {
return r.darwinDesktopLayers(ConfigPath, darwinAppSupportBase)
}
func (r *Resolver) darwinDesktopDataLayers() ([]layer, error) {
return r.darwinDesktopLayers(DataPath, darwinAppSupportBase)
}
func (r *Resolver) darwinDesktopLayers(kind PathKind, base func(core.Scope, string) string) ([]layer, error) {
if v, ok := r.overrideValue(kind); ok {
return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil
}
home, err := r.homeDir()
if err != nil {
return nil, err
}
user := []layer{{
path: r.applyRoot(unixJoin(base(core.UserScope, home), r.app.Name)),
source: MacOSNative,
priority: PriorityUser,
canonical: true,
}}
system := []layer{{
path: r.applyRoot(unixJoin(base(core.SystemScope, ""), r.app.Name)),
source: SystemDirectory,
priority: PrioritySystem,
canonical: r.app.Scope == core.SystemScope,
}}
return composeSearch(user, system, r.app.Scope), nil
}