mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): identify invalid filename globs in search tools (#63960)
This commit is contained in:
committed by
GitHub
Unverified
parent
f61411af46
commit
b659188ac8
@@ -45,6 +45,8 @@ export class Error extends Schema.TaggedError<Error>()("Ripgrep.Error", {
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
}) {}
|
||||
|
||||
export class InvalidGlobError extends Error {}
|
||||
|
||||
export class InvalidPatternError extends Schema.TaggedError<InvalidPatternError>()("Ripgrep.InvalidPatternError", {
|
||||
pattern: Schema.String,
|
||||
message: Schema.String,
|
||||
@@ -143,6 +145,11 @@ const layer = Layer.effect(
|
||||
|
||||
const code = yield* handle.exitCode
|
||||
const stderr = yield* Fiber.join(stderrFiber)
|
||||
if (code === 2) {
|
||||
// rg 13 omits the program name. Ignore-file diagnostics start with a path and line number.
|
||||
const prefix = /^(?:rg: )?error parsing glob /.exec(stderr)
|
||||
if (prefix) return yield* new InvalidGlobError({ message: stderr.slice(prefix[0].length).trimEnd() })
|
||||
}
|
||||
if (input.pattern && code === 2 && isInvalidPattern(stderr)) {
|
||||
return yield* new InvalidPatternError({ pattern: input.pattern, message: stderr.trim() })
|
||||
}
|
||||
|
||||
@@ -105,7 +105,11 @@ export const Plugin = {
|
||||
),
|
||||
metadata: { count: result.entries.length, truncated: result.truncated },
|
||||
})),
|
||||
Effect.mapError(toolFailure(`Unable to find files matching ${input.pattern}`)),
|
||||
Effect.mapError((error) =>
|
||||
error instanceof Ripgrep.InvalidGlobError
|
||||
? new ToolFailure({ message: `Invalid glob in pattern ${error.message}` })
|
||||
: toolFailure(`Unable to find files matching ${input.pattern}`)(error),
|
||||
),
|
||||
),
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -136,13 +136,14 @@ export const Plugin = {
|
||||
),
|
||||
metadata: { matches: result.matches.length, truncated: result.truncated },
|
||||
})),
|
||||
Effect.mapError((error) =>
|
||||
error instanceof ToolFailure
|
||||
? error
|
||||
: error instanceof Ripgrep.InvalidPatternError
|
||||
? new ToolFailure({ message: `Invalid regex pattern: ${error.message}` })
|
||||
: new ToolFailure({ message: `Unable to grep for ${input.pattern}`, error }),
|
||||
),
|
||||
Effect.mapError((error) => {
|
||||
if (error instanceof ToolFailure) return error
|
||||
if (error instanceof Ripgrep.InvalidGlobError)
|
||||
return new ToolFailure({ message: `Invalid glob in include ${error.message}` })
|
||||
if (error instanceof Ripgrep.InvalidPatternError)
|
||||
return new ToolFailure({ message: `Invalid regex pattern: ${error.message}` })
|
||||
return new ToolFailure({ message: `Unable to grep for ${input.pattern}`, error })
|
||||
}),
|
||||
),
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -14,6 +14,22 @@ import { tempLocationLayer } from "./fixture/location"
|
||||
const it = testEffect(AppNodeBuilder.build(Ripgrep.node, [Location.node.replace(tempLocationLayer)]))
|
||||
|
||||
describe("Ripgrep", () => {
|
||||
// Creating a file symlink can require elevated privileges on Windows.
|
||||
describe.skipIf(process.platform === "win32")("partial results", () => {
|
||||
it.live("keeps matching files when an ignore glob is malformed and a symlink is broken", () =>
|
||||
withTempDir((tmp) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "visible.ts"), "needle\n"))
|
||||
yield* Effect.promise(() => Bun.write(path.join(tmp.path, ".ignore"), "[z-a]\n"))
|
||||
yield* Effect.promise(() => fs.symlink("missing", path.join(tmp.path, "broken")))
|
||||
const ripgrep = yield* Ripgrep.Service
|
||||
const files = yield* ripgrep.glob({ cwd: tmp.path, pattern: "*.ts", follow: true, limit: 10 })
|
||||
expect(files.map((file) => file.path)).toEqual([RelativePath.make("visible.ts")])
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
for (const hidden of [undefined, false, true]) {
|
||||
for (const limit of hidden ? [10] : [1, 10]) {
|
||||
it.live(`glob honors hidden=${hidden} before limit=${limit}`, () =>
|
||||
|
||||
@@ -67,6 +67,31 @@ const call = (name: "glob" | "grep", input: unknown) => ({
|
||||
})
|
||||
|
||||
describe("search tools", () => {
|
||||
for (const name of ["glob", "grep"] as const) {
|
||||
it.live(`${name} distinguishes a malformed filename glob from an empty search`, () =>
|
||||
withTempDir((tmp) =>
|
||||
withTools(tmp.path, (registry) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "visible.ts"), "needle\n"))
|
||||
const search = (glob: string) =>
|
||||
executeTool(
|
||||
registry,
|
||||
call(name, name === "glob" ? { pattern: glob } : { pattern: "needle", include: glob }),
|
||||
)
|
||||
expect(yield* search("*.txt")).toMatchObject({ status: "completed", output: [] })
|
||||
expect(yield* search("[")).toMatchObject({
|
||||
status: "error",
|
||||
error: {
|
||||
type: "tool.execution",
|
||||
message: `Invalid glob in ${name === "glob" ? "pattern" : "include"} '[': unclosed character class; missing ']'`,
|
||||
},
|
||||
})
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
for (const hidden of [undefined, false, true]) {
|
||||
for (const limit of hidden ? [10] : [1, 10]) {
|
||||
it.live(`glob honors hidden=${hidden} before limit=${limit}`, () =>
|
||||
|
||||
Reference in New Issue
Block a user