diff --git a/internal/config/agent_config_dir.go b/internal/config/agent_config_dir.go index 00bcfd71e..07d63a012 100644 --- a/internal/config/agent_config_dir.go +++ b/internal/config/agent_config_dir.go @@ -185,8 +185,8 @@ func (c *Config) AddAgentConfigDirTarget(name, agent, dir, cli string) (string, return target.SkillsConfig().Path, nil } -// withoutDerivedPaths drops the paths expandAgentConfigDirs filled in, so they keep -// following config_dir instead of being saved as written ones. +// withoutDerivedPaths drops the paths filled in from config_dir or the environment, so +// they keep following it instead of being saved as written ones. func (tc TargetConfig) withoutDerivedPaths() TargetConfig { strip := func(rc *ResourceTargetConfig, derived string) *ResourceTargetConfig { if rc == nil || derived == "" || rc.Path != derived { diff --git a/internal/config/config.go b/internal/config/config.go index cb29450fe..55805d1d6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -168,7 +168,7 @@ type TargetConfig struct { Files []string `yaml:"files,omitempty"` defaultTargetNaming string `yaml:"-"` - // derivedSkills and derivedAgents are the paths expandAgentConfigDirs filled in. + // derivedSkills and derivedAgents are the paths filled in from config_dir or the environment. derivedSkills, derivedAgents string `yaml:"-"` // projectRoot marks a target expanded from projects; see expandProjects. projectRoot string `yaml:"-"` @@ -838,6 +838,10 @@ func decodeConfig(path string, data []byte, persist bool) (*Config, error) { if target.SkillsConfig().Path == "" { if def, ok := defaults[name]; ok { target.EnsureSkills().Path = expandPath(def.Path) + // A default from the environment keeps following it instead of being saved. + if targetConfigHome(name, runtime.GOOS) != "" { + target.derivedSkills = target.SkillsConfig().Path + } } } cfg.Targets[name] = target diff --git a/internal/config/targets_new_tools_test.go b/internal/config/targets_new_tools_test.go index 562bae1b8..dbc987ba9 100644 --- a/internal/config/targets_new_tools_test.go +++ b/internal/config/targets_new_tools_test.go @@ -100,6 +100,23 @@ func TestTargets_ConfigHomeOverrides(t *testing.T) { if got := filepath.Clean(target.SkillsConfig().Path); got != filepath.Join(root, "pinned", "skills") { t.Errorf("explicit path = %q, want pinned path", got) } + + // A pathless entry keeps following the override after a save. + cfg, _, err = loadWithTargets(t, " "+tt.name+": {}\n") + if err != nil { + t.Fatal(err) + } + if err := cfg.Save(); err != nil { + t.Fatal(err) + } + t.Setenv(tt.env, filepath.Join(base, "moved")) + if cfg, err = Load(); err != nil { + t.Fatal(err) + } + target = cfg.Targets[tt.name] + if got := target.SkillsConfig().Path; got != DefaultTargets()[tt.name].Path { + t.Errorf("path after save = %q, want %q", got, DefaultTargets()[tt.name].Path) + } }) } }