Skip to content

Commit 79102df

Browse files
committed
fix(cli): refuse missing or non-directory update targets as invalid_target
Refs #390
1 parent 21ece3a commit 79102df

4 files changed

Lines changed: 129 additions & 21 deletions

File tree

‎docs/operating.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ runtime=directory presence=present compatibility=compatible action=configure tou
8383
trailing separator counts as unclean: pass `/path/dir`, not `/path/dir/`) with the
8484
same error before reading or writing anything; an empty value never falls back to the
8585
runtime mode. A malformed command line is still refused first as `invalid_command` (for
86-
example `update --target-dir <dir>` without `--catalog`). A missing absolute target keeps each command's own answer (doctor reports
87-
`presence=absent`, update refuses with `reason=target_absent`), and uninstall also
88-
refuses a filesystem root.
86+
example `update --target-dir <dir>` without `--catalog`). Update, like install, also
87+
refuses an absolute target that is missing or not a directory as `invalid_target`
88+
(exit 64): there is nothing to update. Doctor reports a missing target as
89+
`presence=absent` instead of refusing it, uninstall refuses a missing target with
90+
`uninstall=not_installed` and exit 3, and uninstall also refuses a filesystem root.
8991
- Update and uninstall work on the recorded ownership:
9092
`cortex update --target-dir <dir> --catalog ./catalog [--apply]` (a target with no prior
9193
state is refused with `reason=not_installed` instead of becoming a first install), and

‎internal/cli/directory_lifecycle_test.go‎

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,80 @@ func TestUpdateDirectoryTargetFailsClosedOnUnknownState(t *testing.T) {
8282
}
8383
}
8484

85+
// TestUpdateDirectoryTargetRefusesMissingOrNonDirectory pins the #390 ruling:
86+
// update refuses an absolute target that is missing or not a directory as
87+
// invalid_target, the same as install, and writes nothing.
88+
func TestUpdateDirectoryTargetRefusesMissingOrNonDirectory(t *testing.T) {
89+
for _, apply := range []bool{false, true} {
90+
for _, kind := range []string{"missing", "file"} {
91+
name := kind
92+
if apply {
93+
name += "/apply"
94+
}
95+
t.Run(name, func(t *testing.T) {
96+
home := canonicalHome(t)
97+
target := filepath.Join(home, "agent-skills")
98+
if kind == "file" {
99+
if err := os.WriteFile(target, []byte("user bytes"), 0o600); err != nil {
100+
t.Fatal(err)
101+
}
102+
}
103+
before := homeListing(t, home)
104+
args := []string{"update", "--target-dir", target, "--catalog", "../../catalog"}
105+
if apply {
106+
args = append(args, "--apply")
107+
}
108+
code, stdout, stderr := runRefusal(t, home, args)
109+
if code != exitUsage || stdout != "" || stderr != invalidTargetRefusal {
110+
t.Fatalf("exit = %d stdout = %q stderr = %q, want %d \"\" %q", code, stdout, stderr, exitUsage, invalidTargetRefusal)
111+
}
112+
if after := homeListing(t, home); after != before {
113+
t.Fatalf("home changed on refusal: %q, want %q", after, before)
114+
}
115+
})
116+
}
117+
}
118+
}
119+
120+
// Doctor keeps reporting a missing absolute target instead of refusing it
121+
// (#390): exit 0, presence=absent, and nothing created.
122+
func TestDoctorDirectoryTargetReportsMissingAsAbsent(t *testing.T) {
123+
home := canonicalHome(t)
124+
target := filepath.Join(home, "agent-skills")
125+
code, stdout, stderr := runRefusal(t, home, []string{"doctor", "--target-dir", target})
126+
if code != exitOK || stderr != "" || stdout != "target="+target+" presence=absent\n" {
127+
t.Fatalf("exit = %d stdout = %q stderr = %q", code, stdout, stderr)
128+
}
129+
if _, err := os.Lstat(target); !os.IsNotExist(err) {
130+
t.Fatalf("missing target must not be created: %v", err)
131+
}
132+
}
133+
134+
// homeListing lists every path and file content under root.
135+
func homeListing(t *testing.T, root string) string {
136+
t.Helper()
137+
var entries []string
138+
err := filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {
139+
if err != nil {
140+
return err
141+
}
142+
line := path
143+
if !entry.IsDir() {
144+
data, err := os.ReadFile(path)
145+
if err != nil {
146+
return err
147+
}
148+
line += "=" + string(data)
149+
}
150+
entries = append(entries, line)
151+
return nil
152+
})
153+
if err != nil {
154+
t.Fatal(err)
155+
}
156+
return strings.Join(entries, "\n")
157+
}
158+
85159
func TestUninstallDirectoryTargetRemovesOnlyOwnedFiles(t *testing.T) {
86160
home := canonicalHome(t)
87161
target := filepath.Join(home, "agent-skills")
@@ -149,10 +223,16 @@ func guardedUninstallDependencies(t *testing.T) uninstallDependencies {
149223
// with the same diagnostic and exit code, and writes nothing.
150224
func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
151225
const existing = "test-relative-target-372"
226+
shipped, err := filepath.Abs(filepath.Join("..", "..", "catalog"))
227+
if err != nil {
228+
t.Fatal(err)
229+
}
230+
// The relative fixture lives in a temporary working directory, never in
231+
// the package source tree.
232+
t.Chdir(t.TempDir())
152233
if err := os.MkdirAll(existing, 0o700); err != nil {
153234
t.Fatal(err)
154235
}
155-
t.Cleanup(func() { _ = os.RemoveAll(existing) })
156236
if err := os.WriteFile(filepath.Join(existing, "foreign-sentinel.txt"), []byte("user bytes"), 0o600); err != nil {
157237
t.Fatal(err)
158238
}
@@ -173,13 +253,17 @@ func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
173253
commands := map[string][]string{
174254
"install": nil,
175255
"doctor": nil,
176-
"update": {"--catalog", "../../catalog", "--apply"},
256+
"update": {"--catalog", shipped, "--apply"},
177257
"uninstall": nil,
178258
}
179259
cwd, err := os.Getwd()
180260
if err != nil {
181261
t.Fatal(err)
182262
}
263+
cwd, err = filepath.EvalSymlinks(cwd)
264+
if err != nil {
265+
t.Fatal(err)
266+
}
183267
// unclean is absolute and names the existing directory, so only the clean
184268
// check can refuse it. trailing-separator is the form shell completion
185269
// produces. empty is what an unset shell variable passes.
@@ -228,11 +312,33 @@ func TestDirectoryTargetCommandsRefuseRelativeTargets(t *testing.T) {
228312
// An explicit empty --runtime is a usage error, so the runtime mode never
229313
// reaches the directory mode through an empty runtime ID.
230314
t.Run("update/empty-runtime-is-a-usage-error", func(t *testing.T) {
231-
code, stdout, stderr := runRefusal(t, canonicalHome(t), []string{"update", "--runtime", "", "--catalog", "../../catalog"})
315+
code, stdout, stderr := runRefusal(t, canonicalHome(t), []string{"update", "--runtime", "", "--catalog", shipped})
232316
if code != exitUsage || stdout != "" || stderr != "error=invalid_command\n" {
233317
t.Fatalf("exit = %d stdout = %q stderr = %q", code, stdout, stderr)
234318
}
235319
})
320+
// Contradictory modes are a usage error even when the runtime is empty,
321+
// the same as with a non-empty runtime.
322+
t.Run("update/empty-runtime-with-target-is-a-usage-error", func(t *testing.T) {
323+
code, stdout, stderr := runRefusal(t, canonicalHome(t), []string{"update", "--runtime", "", "--target-dir", filepath.Join(cwd, existing), "--catalog", shipped})
324+
if code != exitUsage || stdout != "" || stderr != "error=invalid_command\n" {
325+
t.Fatalf("exit = %d stdout = %q stderr = %q", code, stdout, stderr)
326+
}
327+
})
328+
// An empty first --target-dir still counts as given, so a second one is a
329+
// duplicate flag rather than a silent override.
330+
for command, extra := range map[string][]string{"doctor": nil, "update": {"--catalog", shipped}, "uninstall": nil} {
331+
t.Run(command+"/duplicate-target-after-empty", func(t *testing.T) {
332+
args := append([]string{command, "--target-dir", "", "--target-dir", filepath.Join(cwd, existing)}, extra...)
333+
code, stdout, stderr := runRefusal(t, canonicalHome(t), args)
334+
if code != exitUsage || stdout != "" || stderr != "error=invalid_command\n" {
335+
t.Fatalf("exit = %d stdout = %q stderr = %q", code, stdout, stderr)
336+
}
337+
if after := listing(existing); after != before {
338+
t.Fatalf("target changed: %q, want %q", after, before)
339+
}
340+
})
341+
}
236342
// Control: the shared check must not over-refuse a valid absolute target.
237343
t.Run("doctor/accepts-valid-absolute", func(t *testing.T) {
238344
code, stdout, stderr := runRefusal(t, canonicalHome(t), []string{"doctor", "--target-dir", filepath.Join(cwd, existing)})

‎internal/cli/install.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ func validTargetDir(target string) bool {
103103
return filepath.IsAbs(target) && target == filepath.Clean(target)
104104
}
105105

106+
// usableTargetDir is the install and update target check: a valid path that
107+
// names an existing directory. The path check runs first so a relative path is
108+
// never stat'ed against the caller's working directory.
109+
func usableTargetDir(target string) bool {
110+
return validTargetDir(target) && existingDirectory(target)
111+
}
112+
106113
// runDirectoryInstall projects the catalog into the caller-owned directory.
107114
// The backup root stays under the user home like every other install: backups
108115
// are a Cortex ownership artifact, and writing them inside the target would
@@ -112,12 +119,7 @@ func runDirectoryInstall(stdout, stderr io.Writer, target string, deps installDe
112119
// malformed target shares the one explicit invalid_target diagnostic
113120
// (issue #367): a relative path that exists on disk would otherwise pass
114121
// the stat check below and surface as install_composition_failed.
115-
if !validTargetDir(target) {
116-
writeError(stderr, "invalid_target")
117-
return exitUsage
118-
}
119-
info, err := os.Stat(target)
120-
if err != nil || !info.IsDir() {
122+
if !usableTargetDir(target) {
121123
writeError(stderr, "invalid_target")
122124
return exitUsage
123125
}

‎internal/cli/update.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,18 @@ func runWithUpdateDependencies(ctx context.Context, args []string, stdout, stder
140140
// default runtime or target. The two selection modes are mutually exclusive.
141141
func parseUpdateArgs(args []string) (runtimematrix.RuntimeID, string, bool, bool, string, bool) {
142142
var runtimeID, catalogDir, targetDir string
143-
apply, adoptGuidance, targetSet := false, false, false
143+
apply, adoptGuidance, runtimeSet, targetSet := false, false, false, false
144144
for len(args) > 0 {
145145
flag := args[0]
146146
args = args[1:]
147147
switch flag {
148148
case "--runtime":
149-
if runtimeID != "" || targetSet || len(args) == 0 {
149+
if runtimeSet || targetSet || len(args) == 0 {
150150
return "", "", false, false, "", false
151151
}
152-
runtimeID, args = args[0], args[1:]
152+
runtimeID, args, runtimeSet = args[0], args[1:], true
153153
case "--target-dir":
154-
if targetSet || runtimeID != "" || len(args) == 0 {
154+
if targetSet || runtimeSet || len(args) == 0 {
155155
return "", "", false, false, "", false
156156
}
157157
targetDir, args, targetSet = args[0], args[1:], true
@@ -239,15 +239,13 @@ func buildUpdateCandidate(runtimeID runtimematrix.RuntimeID, catalogDir string,
239239
// resolved into the caller-owned root, and readiness is revalidated through
240240
// installcoord.PreflightDirectoryTarget before any write.
241241
func runDirectoryUpdate(stdout, stderr io.Writer, targetDir, catalogDir string, apply bool) int {
242-
if !validTargetDir(targetDir) {
242+
// A missing or non-directory target has nothing to update, so it is
243+
// refused the same way install refuses it (issue #390).
244+
if !usableTargetDir(targetDir) {
243245
writeError(stderr, "invalid_target")
244246
return exitUsage
245247
}
246248
decision := runtimematrix.DirectoryTargetMatrix().Decisions[0]
247-
info, err := os.Stat(targetDir)
248-
if err != nil || !info.IsDir() {
249-
return writeUpdateRefusal(stdout, decision, "target_absent")
250-
}
251249
plan, err := buildDirectoryUpdateCandidate(targetDir, catalogDir)
252250
if err != nil {
253251
writeError(stderr, "invalid_catalog")

0 commit comments

Comments
 (0)