on main help sign in
fix: skip non-existent command dirs instead of creating them (#2534)

Check if the command source directory exists before walking it. If it doesn't exist, return early with no commands instead of auto-creating it via MkdirAll. Remove the now-unused ensureDir helper. Fixes #2526 Signed-off-by: majiayu000 <1835304752@qq.com>

commit73c2a9dd82591875b4b9f2ce85310b33d5ef834d authorlif <1835304752@qq.com> dateWed 1 Apr 2026, 01:25 (7 months ago) parent42c8bcc
2 files changed+55−9
internal/commands/commands.go+2−9 internal/commands/commands_test.go+53
internal/commands/commands.go+2−9
@@ -130,8 +130,8 @@ func loadAll(sources []commandSource) ([]CustomCommand, error) {
130130}
131131
132132func loadFromSource(source commandSource) ([]CustomCommand, error) {
133− if err := ensureDir(source.path); err != nil {
134− return nil, err
133+ if _, err := os.Stat(source.path); os.IsNotExist(err) {
134+ return nil, nil
135135 }
136136
137137 var commands []CustomCommand
@@ -216,13 +216,6 @@ func getXDGCommandsDir() string {
216216 return ""
217217}
218218
219−func ensureDir(path string) error {
220− if _, err := os.Stat(path); os.IsNotExist(err) {
221− return os.MkdirAll(path, 0o755)
222− }
223− return nil
224−}
225−
226219func isMarkdownFile(name string) bool {
227220 return strings.HasSuffix(strings.ToLower(name), ".md")
228221}
internal/commands/commands_test.gonew file+53
@@ -0,0 +1,53 @@
1+package commands
2+
3+import (
4+ "os"
5+ "path/filepath"
6+ "testing"
7+
8+ "github.com/stretchr/testify/require"
9+)
10+
11+func TestLoadFromSource_NonExistentDir(t *testing.T) {
12+ t.Parallel()
13+
14+ dir := filepath.Join(t.TempDir(), "does-not-exist")
15+
16+ cmds, err := loadFromSource(commandSource{path: dir, prefix: userCommandPrefix})
17+ require.NoError(t, err)
18+ require.Empty(t, cmds)
19+
20+ // directory must NOT have been created
21+ _, statErr := os.Stat(dir)
22+ require.True(t, os.IsNotExist(statErr))
23+}
24+
25+func TestLoadFromSource_ExistingDir(t *testing.T) {
26+ t.Parallel()
27+
28+ dir := t.TempDir()
29+ require.NoError(t, os.WriteFile(filepath.Join(dir, "hello.md"), []byte("say hello"), 0o644))
30+
31+ cmds, err := loadFromSource(commandSource{path: dir, prefix: userCommandPrefix})
32+ require.NoError(t, err)
33+ require.Len(t, cmds, 1)
34+ require.Equal(t, "user:hello", cmds[0].ID)
35+ require.Equal(t, "say hello", cmds[0].Content)
36+}
37+
38+func TestLoadAll_MixedSources(t *testing.T) {
39+ t.Parallel()
40+
41+ existing := t.TempDir()
42+ require.NoError(t, os.WriteFile(filepath.Join(existing, "cmd.md"), []byte("content"), 0o644))
43+
44+ missing := filepath.Join(t.TempDir(), "nope")
45+
46+ cmds, err := loadAll([]commandSource{
47+ {path: existing, prefix: userCommandPrefix},
48+ {path: missing, prefix: projectCommandPrefix},
49+ })
50+ require.NoError(t, err)
51+ require.Len(t, cmds, 1)
52+ require.Equal(t, "user:cmd", cmds[0].ID)
53+}