Skip to content

Commit b659188

Browse files
authored
fix(core): identify invalid filename globs in search tools (#63960)
1 parent f61411a commit b659188

5 files changed

Lines changed: 61 additions & 8 deletions

File tree

‎packages/core/src/ripgrep.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export class Error extends Schema.TaggedError<Error>()("Ripgrep.Error", {
4545
cause: Schema.optional(Schema.Defect()),
4646
}) {}
4747

48+
export class InvalidGlobError extends Error {}
49+
4850
export class InvalidPatternError extends Schema.TaggedError<InvalidPatternError>()("Ripgrep.InvalidPatternError", {
4951
pattern: Schema.String,
5052
message: Schema.String,
@@ -143,6 +145,11 @@ const layer = Layer.effect(
143145

144146
const code = yield* handle.exitCode
145147
const stderr = yield* Fiber.join(stderrFiber)
148+
if (code === 2) {
149+
// rg 13 omits the program name. Ignore-file diagnostics start with a path and line number.
150+
const prefix = /^(?:rg: )?error parsing glob /.exec(stderr)
151+
if (prefix) return yield* new InvalidGlobError({ message: stderr.slice(prefix[0].length).trimEnd() })
152+
}
146153
if (input.pattern && code === 2 && isInvalidPattern(stderr)) {
147154
return yield* new InvalidPatternError({ pattern: input.pattern, message: stderr.trim() })
148155
}

‎packages/core/src/tool/plugin/glob.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ export const Plugin = {
105105
),
106106
metadata: { count: result.entries.length, truncated: result.truncated },
107107
})),
108-
Effect.mapError(toolFailure(`Unable to find files matching ${input.pattern}`)),
108+
Effect.mapError((error) =>
109+
error instanceof Ripgrep.InvalidGlobError
110+
? new ToolFailure({ message: `Invalid glob in pattern ${error.message}` })
111+
: toolFailure(`Unable to find files matching ${input.pattern}`)(error),
112+
),
109113
),
110114
}),
111115
)

‎packages/core/src/tool/plugin/grep.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ export const Plugin = {
136136
),
137137
metadata: { matches: result.matches.length, truncated: result.truncated },
138138
})),
139-
Effect.mapError((error) =>
140-
error instanceof ToolFailure
141-
? error
142-
: error instanceof Ripgrep.InvalidPatternError
143-
? new ToolFailure({ message: `Invalid regex pattern: ${error.message}` })
144-
: new ToolFailure({ message: `Unable to grep for ${input.pattern}`, error }),
145-
),
139+
Effect.mapError((error) => {
140+
if (error instanceof ToolFailure) return error
141+
if (error instanceof Ripgrep.InvalidGlobError)
142+
return new ToolFailure({ message: `Invalid glob in include ${error.message}` })
143+
if (error instanceof Ripgrep.InvalidPatternError)
144+
return new ToolFailure({ message: `Invalid regex pattern: ${error.message}` })
145+
return new ToolFailure({ message: `Unable to grep for ${input.pattern}`, error })
146+
}),
146147
),
147148
}),
148149
)

‎packages/core/test/ripgrep.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ import { tempLocationLayer } from "./fixture/location"
1414
const it = testEffect(AppNodeBuilder.build(Ripgrep.node, [Location.node.replace(tempLocationLayer)]))
1515

1616
describe("Ripgrep", () => {
17+
// Creating a file symlink can require elevated privileges on Windows.
18+
describe.skipIf(process.platform === "win32")("partial results", () => {
19+
it.live("keeps matching files when an ignore glob is malformed and a symlink is broken", () =>
20+
withTempDir((tmp) =>
21+
Effect.gen(function* () {
22+
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "visible.ts"), "needle\n"))
23+
yield* Effect.promise(() => Bun.write(path.join(tmp.path, ".ignore"), "[z-a]\n"))
24+
yield* Effect.promise(() => fs.symlink("missing", path.join(tmp.path, "broken")))
25+
const ripgrep = yield* Ripgrep.Service
26+
const files = yield* ripgrep.glob({ cwd: tmp.path, pattern: "*.ts", follow: true, limit: 10 })
27+
expect(files.map((file) => file.path)).toEqual([RelativePath.make("visible.ts")])
28+
}),
29+
),
30+
)
31+
})
32+
1733
for (const hidden of [undefined, false, true]) {
1834
for (const limit of hidden ? [10] : [1, 10]) {
1935
it.live(`glob honors hidden=${hidden} before limit=${limit}`, () =>

‎packages/core/test/tool-search.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ const call = (name: "glob" | "grep", input: unknown) => ({
6767
})
6868

6969
describe("search tools", () => {
70+
for (const name of ["glob", "grep"] as const) {
71+
it.live(`${name} distinguishes a malformed filename glob from an empty search`, () =>
72+
withTempDir((tmp) =>
73+
withTools(tmp.path, (registry) =>
74+
Effect.gen(function* () {
75+
yield* Effect.promise(() => Bun.write(path.join(tmp.path, "visible.ts"), "needle\n"))
76+
const search = (glob: string) =>
77+
executeTool(
78+
registry,
79+
call(name, name === "glob" ? { pattern: glob } : { pattern: "needle", include: glob }),
80+
)
81+
expect(yield* search("*.txt")).toMatchObject({ status: "completed", output: [] })
82+
expect(yield* search("[")).toMatchObject({
83+
status: "error",
84+
error: {
85+
type: "tool.execution",
86+
message: `Invalid glob in ${name === "glob" ? "pattern" : "include"} '[': unclosed character class; missing ']'`,
87+
},
88+
})
89+
}),
90+
),
91+
),
92+
)
93+
}
94+
7095
for (const hidden of [undefined, false, true]) {
7196
for (const limit of hidden ? [10] : [1, 10]) {
7297
it.live(`glob honors hidden=${hidden} before limit=${limit}`, () =>

0 commit comments

Comments
 (0)