From e8bf23a064d56435afd40351b91b7a5a9da86f37 Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Tue, 22 Sep 2026 22:24:51 +0200 Subject: [PATCH] paths: skip the user search tier when there is no home directory ConfigSearchDirs/DataSearchDirs/ExecutableSearchDirs (and therefore core/config's Resolve/ResolveAll/LoadMerged) failed outright with ErrNoHomeDirectory when HOME/USERPROFILE was unset, so a service started without HOME could no longer find its working-directory or /etc/ config -- a regression against the hand-rolled lookups being migrated onto core, which just skipped the user location. Missing-home now only drops the user tier from the search lists; the canonical Dir() methods still report ErrNoHomeDirectory. Co-Authored-By: Claude Opus 5.5 (1M context) --- README.md | 7 +++++ config/nohome_test.go | 42 +++++++++++++++++++++++++ paths/darwin.go | 17 +++++----- paths/layout.go | 22 ++++++++++++- paths/nohome_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++ paths/unix.go | 51 +++++++++++++++++------------- paths/windows.go | 66 ++++++++++++++++++++++----------------- 7 files changed, 220 insertions(+), 57 deletions(-) create mode 100644 config/nohome_test.go create mode 100644 paths/nohome_test.go diff --git a/README.md b/README.md index cc14666..5fc909c 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,13 @@ write location) and, for Config/Data/executables, search, highest priority first). These are not always the same location -- see "Windows" below. +Without a home directory (`HOME`/`USERPROFILE` unset, and no override +for the user tier), the search lists simply omit the user tier and still +report the system tier -- a system service started without `HOME` keeps +finding `/etc/`. The canonical `Dir()` methods still +return `paths.ErrNoHomeDirectory` in that case; a write location is never +silently swapped for a different tier. + ### Environment variable priority Every canonical category is resolved through the same chain, highest diff --git a/config/nohome_test.go b/config/nohome_test.go new file mode 100644 index 0000000..73751e1 --- /dev/null +++ b/config/nohome_test.go @@ -0,0 +1,42 @@ +package config + +import ( + "path/filepath" + "testing" + + "yetano.dev/yetano/core/paths" +) + +// A process without HOME (e.g. a system service) still finds its +// working-directory and system config; only the user tier is skipped. +func TestResolveWithoutHome(t *testing.T) { + root := t.TempDir() + workdir := filepath.Join(root, "work") + newResolver := func() *Resolver { + return mustResolver(t, yetApp(), + WithEnvironment(map[string]string{}), + WithWorkdirPath(workdir), + WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root))) + } + + if _, found, err := newResolver().Resolve(); err != nil || found { + t.Fatalf("Resolve() on empty tree = found=%v err=%v, want found=false err=nil", found, err) + } + + system := filepath.Join(root, "etc", "yet", "yet.yaml") + writeFile(t, system, "system: true\n") + layer, found, err := newResolver().Resolve() + if err != nil || !found { + t.Fatalf("Resolve() = found=%v err=%v, want the system file", found, err) + } + if layer.Path != system { + t.Errorf("Resolve() path = %q, want %q", layer.Path, system) + } + + cwd := filepath.Join(workdir, "yet.yaml") + writeFile(t, cwd, "cwd: true\n") + layer, found, err = newResolver().Resolve() + if err != nil || !found || layer.Path != cwd { + t.Fatalf("Resolve() = %q found=%v err=%v, want %q", layer.Path, found, err, cwd) + } +} diff --git a/paths/darwin.go b/paths/darwin.go index 8216a1f..efd7728 100644 --- a/paths/darwin.go +++ b/paths/darwin.go @@ -160,16 +160,19 @@ func (r *Resolver) darwinDesktopLayers(kind PathKind, base func(core.Scope, stri if v, ok := r.overrideValue(kind); ok { return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil } + var user []layer home, err := r.homeDir() - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user := []layer{{ - path: r.applyRoot(unixJoin(base(core.UserScope, home), r.app.Name)), - source: MacOSNative, - priority: PriorityUser, - canonical: true, - }} + if err == nil { + 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, diff --git a/paths/layout.go b/paths/layout.go index d3f8f68..4dd7f48 100644 --- a/paths/layout.go +++ b/paths/layout.go @@ -1,6 +1,10 @@ package paths -import "yetano.dev/yetano/core" +import ( + "errors" + + "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"). @@ -40,6 +44,22 @@ func composeSearch(user, system []layer, scope core.Scope) []layer { return out } +// skipMissingHome filters a user-tier lookup error for the search-dir +// builders (Config/Data/Executable): a missing home directory only means +// there is no user tier to search -- the working-directory and system +// tiers are still meaningful (e.g. a system service started without +// HOME whose config lives in /etc/), so ErrNoHomeDirectory is +// dropped here and the caller simply omits its user layer(s). Any other +// error is returned unchanged. The canonical Dir() methods are +// unaffected and still report ErrNoHomeDirectory: a write location is +// never silently swapped for a different tier. +func skipMissingHome(err error) error { + if errors.Is(err, ErrNoHomeDirectory) { + return nil + } + return err +} + // envSuffix maps each category to the "_HOME" suffix used to build its // YETANO_*/_* environment variable names (e.g. ConfigPath -> // "YETANO_CONFIG_HOME" / "YET_CONFIG_HOME"). diff --git a/paths/nohome_test.go b/paths/nohome_test.go new file mode 100644 index 0000000..a4dfa48 --- /dev/null +++ b/paths/nohome_test.go @@ -0,0 +1,72 @@ +package paths + +import ( + "testing" + + "yetano.dev/yetano/core" +) + +// Without a home directory, the search-dir builders drop only the user +// tier; the system tier stays searchable, while the canonical *Dir() +// methods keep reporting ErrNoHomeDirectory. +func TestSearchDirsWithoutHome(t *testing.T) { + cases := []struct { + name string + platform string + app core.Application + search func(*Resolver) ([]SearchResult, error) + want []string + }{ + {"linux config", "linux", yetApp(), (*Resolver).ConfigSearchDirs, []string{"/etc/yet"}}, + {"freebsd config", "freebsd", yetApp(), (*Resolver).ConfigSearchDirs, []string{"/usr/local/etc/yet"}}, + {"linux data", "linux", yetApp(), (*Resolver).DataSearchDirs, []string{"/usr/local/share/yet", "/usr/share/yet"}}, + {"linux bin", "linux", yetApp(), (*Resolver).ExecutableSearchDirs, []string{"/usr/local/bin"}}, + {"darwin desktop config", "darwin", core.Application{Name: "Yet", Kind: core.Desktop}, (*Resolver).ConfigSearchDirs, []string{"/Library/Application Support/Yet"}}, + {"windows config", "windows", yetApp(), (*Resolver).ConfigSearchDirs, []string{`C:\ProgramData\yet`}}, + {"windows data", "windows", yetApp(), (*Resolver).DataSearchDirs, []string{`C:\ProgramData\yet`}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + r := mustResolver(t, tc.app, WithPlatform(tc.platform), WithEnvironment(map[string]string{})) + dirs, err := tc.search(r) + if err != nil { + t.Fatalf("search dirs: %v", err) + } + var got []string + for _, d := range dirs { + got = append(got, d.Path) + } + if len(got) != len(tc.want) { + t.Fatalf("search dirs = %v, want %v", got, tc.want) + } + for i := range got { + if got[i] != tc.want[i] { + t.Fatalf("search dirs = %v, want %v", got, tc.want) + } + } + if _, err := r.ConfigDir(); err != ErrNoHomeDirectory { + t.Errorf("ConfigDir() error = %v, want ErrNoHomeDirectory", err) + } + }) + } +} + +// A partially available Windows user tier (APPDATA set, LOCALAPPDATA and +// USERPROFILE unset) keeps the layers it can resolve. +func TestWindowsConfigSearchPartialUserTier(t *testing.T) { + r := mustResolver(t, yetApp(), WithPlatform("windows"), + WithEnvironment(map[string]string{"APPDATA": `C:\Users\t\AppData\Roaming`})) + dirs, err := r.ConfigSearchDirs() + if err != nil { + t.Fatalf("ConfigSearchDirs: %v", err) + } + want := []string{`C:\Users\t\AppData\Roaming\yet`, `C:\ProgramData\yet`} + if len(dirs) != len(want) { + t.Fatalf("ConfigSearchDirs = %v, want %v", dirs, want) + } + for i, d := range dirs { + if d.Path != want[i] { + t.Fatalf("ConfigSearchDirs[%d] = %q, want %q", i, d.Path, want[i]) + } + } +} diff --git a/paths/unix.go b/paths/unix.go index ac6f7a3..aa18fde 100644 --- a/paths/unix.go +++ b/paths/unix.go @@ -186,16 +186,19 @@ func (r *Resolver) configLayersUnix() ([]layer, error) { if v, ok := r.overrideValue(ConfigPath); ok { return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil } + var user []layer userVal, err := r.xdgBase("XDG_CONFIG_HOME", ".config") - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user := []layer{{ - path: r.applyRoot(unixJoin(userVal.path, r.app.Name)), - source: userVal.source, - priority: PriorityUser, - canonical: true, - }} + if err == nil { + user = []layer{{ + path: r.applyRoot(unixJoin(userVal.path, r.app.Name)), + source: userVal.source, + priority: PriorityUser, + canonical: true, + }} + } system := make([]layer, len(r.flavor().systemConfigDirs)) for i, dir := range r.flavor().systemConfigDirs { system[i] = layer{ @@ -212,16 +215,19 @@ func (r *Resolver) dataLayersUnix() ([]layer, error) { if v, ok := r.overrideValue(DataPath); ok { return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil } + var user []layer userVal, err := r.xdgBase("XDG_DATA_HOME", ".local", "share") - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user := []layer{{ - path: r.applyRoot(unixJoin(userVal.path, r.app.Name)), - source: userVal.source, - priority: PriorityUser, - canonical: true, - }} + if err == nil { + user = []layer{{ + path: r.applyRoot(unixJoin(userVal.path, r.app.Name)), + source: userVal.source, + priority: PriorityUser, + canonical: true, + }} + } dirs, source := r.xdgDataDirs() system := make([]layer, len(dirs)) for i, dir := range dirs { @@ -239,16 +245,19 @@ func (r *Resolver) executableLayersUnix() ([]layer, error) { if v, ok := r.overrideValue(BinPath); ok { return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil } + var user []layer home, err := r.homeDir() - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user := []layer{{ - path: r.applyRoot(unixJoin(home, ".local", "bin")), - source: PlatformDefault, - priority: PriorityUser, - canonical: true, - }} + if err == nil { + user = []layer{{ + path: r.applyRoot(unixJoin(home, ".local", "bin")), + source: PlatformDefault, + priority: PriorityUser, + canonical: true, + }} + } system := []layer{{ path: r.applyRoot(r.flavor().systemBinDir), source: SystemDirectory, diff --git a/paths/windows.go b/paths/windows.go index addac5e..a0fe5e9 100644 --- a/paths/windows.go +++ b/paths/windows.go @@ -128,26 +128,30 @@ func (r *Resolver) configLayersWindows() ([]layer, error) { var user []layer if r.app.Kind != core.Desktop { local, err := r.windowsBase("LOCALAPPDATA") - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user = append(user, layer{ - path: r.applyRoot(windowsJoin(local.path, r.app.Name)), - source: local.source, - priority: PriorityUserLocal, - canonical: false, - }) + if err == nil { + user = append(user, layer{ + path: r.applyRoot(windowsJoin(local.path, r.app.Name)), + source: local.source, + priority: PriorityUserLocal, + canonical: false, + }) + } } roaming, err := r.windowsBase("APPDATA") - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user = append(user, layer{ - path: r.applyRoot(windowsJoin(roaming.path, r.app.Name)), - source: roaming.source, - priority: PriorityUser, - canonical: true, - }) + if err == nil { + user = append(user, layer{ + path: r.applyRoot(windowsJoin(roaming.path, r.app.Name)), + source: roaming.source, + priority: PriorityUser, + canonical: true, + }) + } system, err := r.windowsSystemLayer(r.app.Name) if err != nil { return nil, err @@ -161,16 +165,19 @@ func (r *Resolver) dataLayersWindows() ([]layer, error) { if v, ok := r.overrideValue(DataPath); ok { return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil } + var user []layer local, err := r.windowsBase("LOCALAPPDATA") - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user := []layer{{ - path: r.applyRoot(windowsJoin(local.path, r.app.Name)), - source: local.source, - priority: PriorityUserLocal, - canonical: true, - }} + if err == nil { + user = []layer{{ + path: r.applyRoot(windowsJoin(local.path, r.app.Name)), + source: local.source, + priority: PriorityUserLocal, + canonical: true, + }} + } system, err := r.windowsSystemLayer(r.app.Name) if err != nil { return nil, err @@ -182,16 +189,19 @@ func (r *Resolver) executableLayersWindows() ([]layer, error) { if v, ok := r.overrideValue(BinPath); ok { return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil } + var user []layer local, err := r.windowsBase("LOCALAPPDATA") - if err != nil { + if err := skipMissingHome(err); err != nil { return nil, err } - user := []layer{{ - path: r.applyRoot(windowsJoin(local.path, "Yetano", "bin")), - source: local.source, - priority: PriorityUserLocal, - canonical: true, - }} + if err == nil { + user = []layer{{ + path: r.applyRoot(windowsJoin(local.path, "Yetano", "bin")), + source: local.source, + priority: PriorityUserLocal, + canonical: true, + }} + } system, err := r.windowsSystemLayer("Yetano", "bin") if err != nil { return nil, err