Skip to content

Commit 17ef0c7

Browse files
authored
fix: treat slash commands as warning (#211)
1 parent 348e5ea commit 17ef0c7

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

integration_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,3 +2006,26 @@ task_name: whitespace-task
20062006
})
20072007
}
20082008
}
2009+
2010+
// TestNonExistentSlashCommandPassesThrough verifies that a task referencing a
2011+
// slash command that does not exist passes through the text as-is instead of erroring.
2012+
func TestNonExistentSlashCommandPassesThrough(t *testing.T) {
2013+
t.Parallel()
2014+
dirs := setupTestDirs(t)
2015+
2016+
// Create a task that references a non-existent slash command
2017+
taskContent := `# My Task with a File Path
2018+
2019+
/home/coder/.config should be ignored
2020+
`
2021+
taskFile := filepath.Join(dirs.tasksDir, "bad-command-task.md")
2022+
if err := os.WriteFile(taskFile, []byte(taskContent), 0o600); err != nil {
2023+
t.Fatalf("failed to write task file: %v", err)
2024+
}
2025+
2026+
output := runTool(t, "-C", dirs.tmpDir, "bad-command-task")
2027+
2028+
if !strings.Contains(output, "/home/coder/.config should be ignored") {
2029+
t.Errorf("expected non-existent slash command to pass through as-is, got: %s", output)
2030+
}
2031+
}

pkg/codingcontext/context.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,16 @@ func (cc *Context) processTaskBlock(block taskparser.Block, path string, expandP
465465
if block.SlashCommand != nil {
466466
commandContent, err := cc.findCommand(block.SlashCommand.Name, block.SlashCommand.Params())
467467
if err != nil {
468-
if cc.lintMode && errors.Is(err, ErrCommandNotFound) {
469-
cc.lintCollector.recordError(path, LintErrorKindMissingCommand,
470-
"command not found: "+block.SlashCommand.Name)
468+
if errors.Is(err, ErrCommandNotFound) {
469+
if cc.lintMode {
470+
cc.lintCollector.recordError(path, LintErrorKindMissingCommand,
471+
"command not found: "+block.SlashCommand.Name)
472+
} else {
473+
cc.logger.Warn("Command not found, passing through as-is",
474+
"command", block.SlashCommand.Name)
475+
}
471476

472-
return "/" + block.SlashCommand.Name, nil
477+
return block.SlashCommand.String(), nil
473478
}
474479

475480
return "", fmt.Errorf("failed to find command %s: %w", block.SlashCommand.Name, err)

pkg/codingcontext/context_test.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -957,14 +957,19 @@ func TestContext_Run_Commands(t *testing.T) {
957957
check: checkTaskNotEmpty,
958958
},
959959
{
960-
name: "command not found returns error",
960+
name: "command not found passes through as-is",
961961
setup: func(t *testing.T, dir string) {
962962
t.Helper()
963963
createTask(t, dir, "missing-cmd", "", "/nonexistent")
964964
},
965-
taskName: "missing-cmd",
966-
wantErr: true,
967-
errContains: "command not found",
965+
taskName: "missing-cmd",
966+
wantErr: false,
967+
check: func(t *testing.T, result *Result) {
968+
t.Helper()
969+
if !strings.Contains(result.Prompt, "/nonexistent") {
970+
t.Errorf("expected pass-through of /nonexistent, got %q", result.Prompt)
971+
}
972+
},
968973
},
969974
{
970975
name: "command parameter overrides context parameter",
@@ -1249,16 +1254,22 @@ func TestContext_Run_Errors(t *testing.T) {
12491254
taskName string
12501255
wantErr bool
12511256
errContains string
1257+
check func(t *testing.T, result *Result)
12521258
}{
12531259
{
1254-
name: "command not found in task",
1260+
name: "command not found in task passes through as-is",
12551261
setup: func(t *testing.T, dir string) {
12561262
t.Helper()
12571263
createTask(t, dir, "bad-cmd", "", "/missing-command\n")
12581264
},
1259-
taskName: "bad-cmd",
1260-
wantErr: true,
1261-
errContains: "command not found",
1265+
taskName: "bad-cmd",
1266+
wantErr: false,
1267+
check: func(t *testing.T, result *Result) {
1268+
t.Helper()
1269+
if !strings.Contains(result.Prompt, "/missing-command") {
1270+
t.Errorf("expected pass-through of /missing-command, got %q", result.Prompt)
1271+
}
1272+
},
12621273
},
12631274
{
12641275
name: "invalid agent in task frontmatter",
@@ -1298,6 +1309,10 @@ func TestContext_Run_Errors(t *testing.T) {
12981309
t.Errorf("expected error to contain %q, got %v", tt.errContains, err)
12991310
}
13001311
}
1312+
1313+
if !tt.wantErr && tt.check != nil {
1314+
tt.check(t, result)
1315+
}
13011316
})
13021317
}
13031318
}
@@ -1839,17 +1854,22 @@ func TestUserPrompt(t *testing.T) {
18391854
check: checkTaskContains("${issue_number}"), // expand:false applies to user_prompt too
18401855
},
18411856
{
1842-
name: "user_prompt with invalid slash command",
1857+
name: "user_prompt with invalid slash command passes through as-is",
18431858
setup: func(t *testing.T, dir string) {
18441859
t.Helper()
18451860
createTask(t, dir, "invalid", "", "Task content\n")
18461861
},
18471862
opts: []Option{
18481863
WithUserPrompt("/nonexistent-command\n"),
18491864
},
1850-
taskName: "invalid",
1851-
wantErr: true,
1852-
errContains: "command not found",
1865+
taskName: "invalid",
1866+
wantErr: false,
1867+
check: func(t *testing.T, result *Result) {
1868+
t.Helper()
1869+
if !strings.Contains(result.Prompt, "/nonexistent-command") {
1870+
t.Errorf("expected pass-through of /nonexistent-command, got %q", result.Prompt)
1871+
}
1872+
},
18531873
},
18541874
{
18551875
name: "both task prompt and user prompt parse correctly",

0 commit comments

Comments
 (0)