Files
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

136 lines
3.8 KiB
Go

package config
import (
"fmt"
"os"
"yetano.dev/yetano/core"
"yetano.dev/yetano/core/paths"
)
// Layer is one location in the configuration search/merge hierarchy
// (spec section 48). Search order is descending Priority (first match
// wins); LoadMerged walks the exact same list in reverse (lowest
// priority first, so a higher-priority layer overlays it).
type Layer struct {
Path string
Source paths.Source
Priority int
Exists bool
Selected bool
}
// PriorityYetanoDefaults is lower than any layer core/paths itself
// resolves (see paths.PrioritySystem), so the shared Yetano config
// directory (spec section 4, opt-in via WithYetanoDefaults) is always
// the last fallback.
const PriorityYetanoDefaults = 0
// PriorityExplicit is higher than every other layer (including the
// working directory), for WithExplicitPath's "--config <path>" use
// case: an explicit override always wins, no exceptions.
const PriorityExplicit = 1000
func statExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// layers builds the full, ordered layer list (spec sections 21/22): an
// explicit override path (if set; see WithExplicitPath), then the
// working directory (if enabled), then core/paths's own
// ConfigSearchDirs for this application (optionally dropping the
// system-tier layer), then -- opt-in only -- the shared Yetano defaults
// directory.
func (r *Resolver) layers() ([]Layer, error) {
var out []Layer
if r.explicitPath != "" {
if !statExists(r.explicitPath) {
return nil, fmt.Errorf("config: explicit path %s does not exist", r.explicitPath)
}
out = append(out, Layer{
Path: r.explicitPath,
Source: paths.APIOverride,
Priority: PriorityExplicit,
})
}
if r.workdir {
out = append(out, Layer{
// JoinDirFileFamily (not JoinFamily): r.workdirPath may be a
// real filesystem path from a caller/test (e.g. under
// WithRoot's root), whose own family can differ from
// r.paths.Platform()'s simulated one -- see core/paths'
// applyRootRebase for why that combination is legitimate.
Path: paths.JoinDirFileFamily(r.paths.Platform(), r.workdirPath, r.filename),
Source: paths.WorkingDirectory,
Priority: paths.PriorityWorkingDirectory,
})
}
dirs, err := r.paths.ConfigSearchDirs()
if err != nil {
return nil, err
}
for _, d := range dirs {
if !r.systemConfig && d.Priority <= paths.PrioritySystem {
continue
}
out = append(out, Layer{
Path: paths.JoinDirFile(d.Path, r.filename),
Source: d.Source,
Priority: d.Priority,
})
}
if r.yetanoDefaults {
layer, err := r.yetanoDefaultsLayer()
if err != nil {
return nil, err
}
out = append(out, layer)
}
for i := range out {
out[i].Exists = statExists(out[i].Path)
}
selected := false
for i := range out {
if out[i].Exists && !selected {
out[i].Selected = true
selected = true
}
}
return out, nil
}
// yetanoDefaultsLayer resolves the ecosystem-wide shared configuration
// directory (spec section 4: "~/.config/yetano/", or the platform
// equivalent) as its own pseudo-Application named "yetano" -- always
// CLI-shaped (XDG-flavored on every OS), since it is a shared,
// admin/CLI-facing location, not tied to any one application's Kind.
func (r *Resolver) yetanoDefaultsLayer() (Layer, error) {
yetanoApp := core.Application{Name: "yetano", Kind: core.CLI, Scope: r.app.Scope}
yp, err := paths.New(yetanoApp, append([]paths.Option{paths.WithEnvironmentProvider(r.env)}, r.pathsOpts...)...)
if err != nil {
return Layer{}, err
}
ex, err := yp.Explain()
if err != nil {
return Layer{}, err
}
var dir string
var source paths.Source
for _, p := range ex.Paths {
if p.Kind == paths.ConfigPath {
dir, source = p.Path, p.Source
}
}
return Layer{
Path: paths.JoinDirFile(dir, r.filename),
Source: source,
Priority: PriorityYetanoDefaults,
}, nil
}