Files
core/ARCHITECTURE.md
T
terminar 13801bec11 Add platform-independent path/config resolution library
Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
2026-09-19 16:26:01 +02:00

8.4 KiB

Architecture

This document records the design decisions behind core/paths and core/config, and why, for anyone extending this library later. See README.md for the user-facing API summary and priority tables.

Package split: paths vs. config

core/paths resolves directories. It has no notion of a configuration file, YAML, or file contents. core/config resolves files within those directories, and owns everything about search order, layering/merging, and environment expansion. config depends on paths (it needs directories to look in); paths never depends on, or knows about, config. This split is a hard rule (see the meta-repo's AGENTS.md), not a style preference: a future caller that only needs "where does yasta's cache directory live" should never have to pull in YAML parsing.

Application model: Kind and Scope are orthogonal

ApplicationKind (CLI/Desktop/Service) drives platform-style defaults (most visibly on macOS and Windows). Scope (User/System) drives install-context defaults (which of "the machine" vs. "this user" a path belongs to). They are independent axes on purpose: yasta is a Service whether it happens to be running as a per-user agent or a system-wide daemon; conflating "service" with "system-scoped" would make that impossible to express. Neither axis defaults from the other.

Platform strategy: injectable, not build-tag-per-file

Spec-wise, the natural instinct is paths_linux.go, paths_windows.go, etc., each guarded by a //go:build tag. This library does not do that for the bulk of the logic, and the reason is spec section 32's own requirement: "tests must not assume they're running on the OS under test" -- Windows path logic has to be verifiable on a Linux/macOS CI runner, and vice versa. A build-tag-gated paths_windows.go simply cannot compile, let alone run, anywhere but Windows.

Instead:

  • Every platform's logic lives in a plain .go file, organized by name for readability (unix.go, linux.go, freebsd.go, darwin.go, windows.go) but selected at runtime by Resolver.platform (runtime.GOOS by default, overridable via paths.WithPlatform for tests/simulation).
  • All environment lookups go through the injectable Environment interface (paths.WithEnvironment/WithEnvironmentProvider), never os.Getenv directly.
  • Windows path construction never uses path/filepath (which is host-native and would silently misbehave when simulating Windows on a non-Windows host, or vice versa). It uses the address-family-aware helpers in join.go (unixJoin/windowsJoin/JoinFamily/ JoinDirFile) instead, so a computed Windows path is always backslash-joined and a computed Unix path is always forward-slash- joined, regardless of the host running the test.
  • The one genuine exception is knownfolders_windows.go: an optional, best-effort native lookup via SHGetKnownFolderPath, //go:build windows-gated, consulted only as a fallback below the injectable environment variables and only when actually running on Windows (not merely simulating it). It exists purely as extra fidelity for edge cases (redirected known folders, a service account without APPDATA set) -- correctness of the deterministic test suite never depends on it.

This satisfies spec section 1's "OS-specific implementations... where it makes sense" (sofern sinnvoll) reading: a hand-written syscall 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.

Canonical directory vs. search directories

This is the single most important distinction in the API (see README.md). It exists because Windows genuinely has three simultaneously-relevant layers (%LOCALAPPDATA%, %APPDATA%, %PROGRAMDATA%) with different semantics -- a new config file should be written to the roaming location (so it follows the user), but an existing file should be found even if it's parked in the machine-local override layer or a system-wide default. Every <Category>Dir()/<Category>SearchDirs() pair in this library follows that same canonical-vs-search split, even on platforms (Linux) where they're less obviously necessary, so the API is uniform.

Scope gates which layers exist, not just which one is canonical

SystemScope excludes the user tier entirely from every search list (Config/Data/Executable), rather than merely changing which layer is canonical. Rationale: a system-scope resolution models an execution context with no meaningful per-user home directory (a system daemon account, SYSTEM on Windows) -- including ~/.config/<app> in its search list would be actively misleading, not just lower priority. UserScope (the default) includes the user tier and the system tier as a lower-priority fallback, which is what makes "a user install overrides a system install" (spec section 36) fall out of the priority model for free, rather than needing special-case code.

BinDir is shared, not namespaced

Every other category joins the application name onto a base directory (~/.config/<app>, %APPDATA%\<app>, ...). BinDir deliberately does not: ~/.local/bin and (the spec's own literal example) %LOCALAPPDATA%\Yetano\bin are shared across every Yetano application, mirroring how a PATH entry is inherently a shared, not per-app, concept. This is appendsAppName in layout.go -- the one category where it's false.

App-specific vs. shared environment tiers: as-is vs. base

YET_CONFIG_HOME=/foo resolves to exactly /foo -- not /foo/yet. YETANO_CONFIG_HOME=/foo (and XDG_CONFIG_HOME, and every platform- native/default tier) resolves to /foo/yet. The reasoning: an app-specific variable's name already names the application, so appending the name again would be surprising and is not how any comparable app-specific override convention (elsewhere in the Unix or Windows world) behaves. A shared variable (Yetano-wide, or XDG, or a Windows known folder) is inherently a base many applications share, so it needs the per-app name appended to stay collision-free. This asymmetry is deliberate and tested (TestAppOverrideWinsOverYetano).

macOS: Config and Data share one directory

A native macOS app's ~/Library/Application Support/<App> commonly holds both preferences-adjacent files and bulk data side by side -- this library's Desktop strategy keeps ConfigDir() and DataDir() pointing at the exact same directory on macOS Desktop, rather than inventing an artificial Config/Data subdirectory split that no native macOS app actually uses. StateDir() gets its own State subdirectory so it doesn't collide with either.

RuntimeDir: no fabricated fallback

Spec section 8 explicitly forbids treating ~/.local/run (or any other invented path) as an XDG_RUNTIME_DIR equivalent, because a runtime directory's real semantics (session-scoped, gone after logout, specific permissions) can't be approximated by just picking a persistent directory. RuntimeDir() on Unix therefore returns paths.ErrRuntimeDirUnavailable rather than a wrong-but-present value when neither an override nor XDG_RUNTIME_DIR is set. Windows and 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.

Unix system-scope directories follow each OS's own convention

Linux SystemScope uses the FHS layout (/etc, /var/lib, /var/cache, /run, /var/log). FreeBSD does not reuse it wholesale (spec sections 33/56): it uses /usr/local/etc (ports convention) and /var/db (BSD's writable-application-data convention) instead of /etc and /var/lib. Any other Unix-family GOOS this library doesn't specifically tune yet falls back to the Linux flavor (flavor()'s default case) -- a documented, working default, not a silent wrong answer, and addable later (a new <os>.go flavor table) without touching any other file (see TODO.md).

Explain/doctor never mutates the filesystem

Explain() on both paths.Resolver and config.Resolver only ever performs read-only os.Stat calls (existence and a best-effort writability check via the owner-write permission bit -- deliberately not a real access probe, which would require creating and deleting a test file). This is spec section 47's explicit requirement, and it's why Explain and Ensure* are entirely separate code paths: the former must never accidentally gain a side effect by sharing implementation with the latter.