diff --git a/packages/core/src/ripgrep.ts b/packages/core/src/ripgrep.ts index 5e49d46223c..81f0368a6c2 100644 --- a/packages/core/src/ripgrep.ts +++ b/packages/core/src/ripgrep.ts @@ -45,6 +45,8 @@ export class Error extends Schema.TaggedError()("Ripgrep.Error", { cause: Schema.optional(Schema.Defect()), }) {} +export class InvalidGlobError extends Error {} + export class InvalidPatternError extends Schema.TaggedError()("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() }) } diff --git a/packages/core/src/tool/plugin/glob.ts b/packages/core/src/tool/plugin/glob.ts index 1fd7ee41ff4..ff3e20c4c99 100644 --- a/packages/core/src/tool/plugin/glob.ts +++ b/packages/core/src/tool/plugin/glob.ts @@ -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), + ), ), }), ) diff --git a/packages/core/src/tool/plugin/grep.ts b/packages/core/src/tool/plugin/grep.ts index e14269876ed..6caebdc01c0 100644 --- a/packages/core/src/tool/plugin/grep.ts +++ b/packages/core/src/tool/plugin/grep.ts @@ -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 }) + }), ), }), ) diff --git a/packages/core/test/ripgrep.test.ts b/packages/core/test/ripgrep.test.ts index b1c7fc0e465..4f84d779051 100644 --- a/packages/core/test/ripgrep.test.ts +++ b/packages/core/test/ripgrep.test.ts @@ -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}`, () => diff --git a/packages/core/test/tool-search.test.ts b/packages/core/test/tool-search.test.ts index e871525a336..0f3e5bda595 100644 --- a/packages/core/test/tool-search.test.ts +++ b/packages/core/test/tool-search.test.ts @@ -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}`, () =>