Skip to content

Commit 9b52d33

Browse files
committed
core: improve directory validation error messages to help users fix invalid directory names
1 parent 096710a commit 9b52d33

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

‎packages/opencode/src/cli/error.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export function FormatError(input: unknown) {
1010
`Config file at ${input.data.path} is not valid JSON(C)` + (input.data.message ? `: ${input.data.message}` : "")
1111
)
1212
}
13+
if (Config.DirectoryError.isInstance(input))
14+
return `Directory "${input.data.dir}" in ${input.data.path} is not valid. Did you mean "${input.data.suggestion}"?`
1315
if (Config.InvalidError.isInstance(input))
1416
return [
1517
`Config file at ${input.data.path} is invalid` + (input.data.message ? `: ${input.data.message}` : ""),

‎packages/opencode/src/config/config.ts‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export namespace Config {
6262
]
6363

6464
for (const dir of directories) {
65-
await assertValid(dir).catch(() => {})
65+
await assertValid(dir)
6666
installDependencies(dir)
6767
result.command = mergeDeep(result.command ?? {}, await loadCommand(dir))
6868
result.agent = mergeDeep(result.agent, await loadAgent(dir))
@@ -120,23 +120,20 @@ export namespace Config {
120120
}
121121
})
122122

123+
const INVALID_DIRS = new Bun.Glob(`{${["agents", "commands", "plugins", "tools"].join(",")}}/`)
123124
async function assertValid(dir: string) {
124-
const ALLOWED_DIRS = new Set(["agent", "command", "mode", "plugin", "tool", "themes"])
125-
const UNEXPECTED_DIR_GLOB = new Bun.Glob("*/")
126-
for await (const item of UNEXPECTED_DIR_GLOB.scan({
127-
absolute: true,
128-
followSymlinks: true,
129-
dot: true,
130-
cwd: dir,
131-
onlyFiles: false,
132-
})) {
133-
const dirname = path.basename(item)
134-
if (!ALLOWED_DIRS.has(dirname)) {
135-
throw new InvalidError({
136-
path: dir,
137-
message: `Unexpected directory "${dirname}" found in "${dir}". Only ${ALLOWED_DIRS.values().toArray().join(", ")} directories are allowed.`,
138-
})
139-
}
125+
const invalid = await Array.fromAsync(
126+
INVALID_DIRS.scan({
127+
onlyFiles: false,
128+
cwd: dir,
129+
}),
130+
)
131+
for (const item of invalid) {
132+
throw new DirectoryError({
133+
path: dir,
134+
dir: item,
135+
suggestion: item.substring(0, item.length - 1),
136+
})
140137
}
141138
}
142139

@@ -714,6 +711,15 @@ export namespace Config {
714711
}),
715712
)
716713

714+
export const DirectoryError = NamedError.create(
715+
"ConfigDirectoryError",
716+
z.object({
717+
path: z.string(),
718+
dir: z.string(),
719+
suggestion: z.string(),
720+
}),
721+
)
722+
717723
export const InvalidError = NamedError.create(
718724
"ConfigInvalidError",
719725
z.object({

0 commit comments

Comments
 (0)