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>
114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
package paths
|
|
|
|
import "yetano.dev/yetano/core"
|
|
|
|
// layer is one directory in a search/priority hierarchy (see
|
|
// AGENTS.md-style docs in paths.go: "canonical path vs. search paths").
|
|
// Search order is descending priority; merge order (used by core/config)
|
|
// is the reverse.
|
|
type layer struct {
|
|
path string
|
|
source Source
|
|
priority int
|
|
canonical bool
|
|
}
|
|
|
|
// Priority constants shared with core/config, which adds the working
|
|
// directory layer (always PriorityWorkingDirectory) on top of whatever
|
|
// paths.Resolver reports for the user/system layers.
|
|
const (
|
|
PriorityWorkingDirectory = 100
|
|
PriorityUserLocal = 90 // Windows-only machine-local override layer
|
|
PriorityUser = 80
|
|
PrioritySystem = 10
|
|
)
|
|
|
|
// composeSearch applies the one Scope rule used across every search-dir
|
|
// category (Config, Data, Executable): SystemScope resolves only the
|
|
// system tier (a system-scope process, e.g. a system service, has no
|
|
// meaningful per-user home directory); UserScope resolves the user
|
|
// tier(s) plus the system tier as a lower-priority fallback, so a
|
|
// system-wide default can still be found -- and, per spec section 36,
|
|
// a user installation overrides a system one.
|
|
func composeSearch(user, system []layer, scope core.Scope) []layer {
|
|
if scope == core.SystemScope {
|
|
return system
|
|
}
|
|
out := make([]layer, 0, len(user)+len(system))
|
|
out = append(out, user...)
|
|
out = append(out, system...)
|
|
return out
|
|
}
|
|
|
|
// envSuffix maps each category to the "_HOME" suffix used to build its
|
|
// YETANO_*/<PREFIX>_* environment variable names (e.g. ConfigPath ->
|
|
// "YETANO_CONFIG_HOME" / "YET_CONFIG_HOME").
|
|
var envSuffix = map[PathKind]string{
|
|
ConfigPath: "CONFIG_HOME",
|
|
DataPath: "DATA_HOME",
|
|
StatePath: "STATE_HOME",
|
|
CachePath: "CACHE_HOME",
|
|
RuntimePath: "RUNTIME_HOME",
|
|
LogPath: "LOG_HOME",
|
|
BinPath: "BIN_HOME",
|
|
}
|
|
|
|
// appendsAppName reports whether the YetanoEnvironment/platform-native/
|
|
// PlatformDefault tiers for kind are treated as a *base* directory that
|
|
// the application name is joined onto (true, e.g. XDG_CONFIG_HOME) or as
|
|
// an already-final, shared directory used as-is (false; only BinPath: a
|
|
// bin directory is conventionally shared across every application on the
|
|
// machine, like ~/.local/bin, not namespaced per app).
|
|
//
|
|
// APIOverride and AppEnvironment are always used as-is for every
|
|
// category, regardless of appendsAppName -- an explicit or app-specific
|
|
// override already names an exact directory; it would be surprising for
|
|
// "YET_CONFIG_HOME=/foo" to actually mean "/foo/yet".
|
|
func appendsAppName(kind PathKind) bool {
|
|
return kind != BinPath
|
|
}
|
|
|
|
// overrideValue resolves tiers 1-3 of the override chain (API override,
|
|
// app-specific environment, shared YETANO_* environment) for kind. ok is
|
|
// false if none of these tiers is set, meaning the caller must fall back
|
|
// to its own platform-native tier 4 / default tier 5.
|
|
func (r *Resolver) overrideValue(kind PathKind) (v value, ok bool) {
|
|
if explicit, set := r.overrides[kind]; set {
|
|
return value{path: explicit, source: APIOverride}, true
|
|
}
|
|
suffix := envSuffix[kind]
|
|
if r.envPrefix != "" {
|
|
if v, set := lookup(r.env, r.envPrefix+"_"+suffix); set {
|
|
return value{path: v, source: AppEnvironment}, true
|
|
}
|
|
}
|
|
if v, set := lookup(r.env, "YETANO_"+suffix); set {
|
|
base := v
|
|
if appendsAppName(kind) {
|
|
base = r.join(v, r.app.Name)
|
|
}
|
|
return value{path: base, source: YetanoEnvironment}, true
|
|
}
|
|
return value{}, false
|
|
}
|
|
|
|
// join joins path elements using the address family of r.platform
|
|
// (windows: backslash; everything else: forward slash) -- see join.go.
|
|
func (r *Resolver) join(elems ...string) string {
|
|
if r.isWindows() {
|
|
return windowsJoin(elems...)
|
|
}
|
|
return unixJoin(elems...)
|
|
}
|
|
|
|
func (r *Resolver) isWindows() bool { return r.platform == "windows" }
|
|
func (r *Resolver) isDarwin() bool { return r.platform == "darwin" }
|
|
|
|
// applyRoot rebases an absolute path (in r.platform's simulated address
|
|
// family) under the portable root (WithRoot), if one is set -- see
|
|
// applyRootRebase for why root's own family, not r.platform's, decides
|
|
// the result's family.
|
|
func (r *Resolver) applyRoot(p string) string {
|
|
return applyRootRebase(r.root, p, r.isWindows())
|
|
}
|