Skip to content

Commit 9de4afe

Browse files
fix(file): let a failed ripgrep lookup or file scan be retried, and log its details
- `Ripgrep.filepath()` resets its cached lookup when it fails. - `files()` clears a finished scan whether it succeeded or failed. - The background-scan warning logs the error and its NamedError data. - The fixture checks a later search recovers once ripgrep is available. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0174HUYiaceApqHP7EKMNgnb
1 parent 02654c0 commit 9de4afe

4 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎packages/opencode/src/file/index.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import fuzzysort from "fuzzysort"
1212
import { Global } from "../global"
1313
import { git } from "@/util/git"
1414
import { Protected } from "./protected"
15+
// altimate_change start — upstream_fix: log a failed background scan's NamedError data
16+
import { NamedError } from "@opencode-ai/util/error"
17+
// altimate_change end
1518

1619
export namespace File {
1720
const log = Log.create({ service: "file" })
@@ -403,7 +406,13 @@ export namespace File {
403406
// cannot be downloaded, or its archive cannot be extracted) was an unhandled rejection. Under bun
404407
// test that fails whichever test is running, or the run after its last test passed. Log it and
405408
// keep the empty cache: `files()` scans again on demand and returns its own failure to its caller.
406-
fn(cache).catch((err) => log.warn("background file scan failed", { err: String(err) }))
409+
// A NamedError's details (download URL and status, tar's stderr) live in `data`, not the stack.
410+
fn(cache).catch((error) =>
411+
log.warn("background file scan failed", {
412+
error,
413+
...(error instanceof NamedError ? { data: error.toObject().data } : {}),
414+
}),
415+
)
407416
// altimate_change end
408417

409418
let pending: Promise<void> | undefined
@@ -412,8 +421,15 @@ export namespace File {
412421
// Always ensure cache reflects a recent scan: if a scan is in-flight,
413422
// wait for it; otherwise kick off a fresh scan and await it.
414423
if (!pending) pending = fn({ files: [], dirs: [] })
415-
await pending
416-
pending = undefined
424+
// altimate_change start — upstream_fix: clear the finished scan even when it failed. Clearing
425+
// it only on success left a rejected scan in `pending`, so every later call re-awaited that
426+
// rejection and never scanned again.
427+
try {
428+
await pending
429+
} finally {
430+
pending = undefined
431+
}
432+
// altimate_change end
417433
return cache
418434
},
419435
}

‎packages/opencode/src/file/ripgrep.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,13 @@ export namespace Ripgrep {
168168
})
169169

170170
export async function filepath() {
171-
const { filepath } = await state()
171+
// altimate_change start — upstream_fix: do not keep a failed lookup. `state` caches its promise, so
172+
// one failed download or extraction made every later search in this process fail the same way.
173+
const { filepath } = await state().catch((error) => {
174+
state.reset()
175+
throw error
176+
})
177+
// altimate_change end
172178
return filepath
173179
}
174180

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
// Run only in a subprocess (see startup-scan-failure.test.ts): the parent gives it a PATH without
22
// ripgrep and a proxy that refuses connections, so ripgrep can be neither found nor downloaded.
3+
// The test preload gives this process its own data directory, so no cached ripgrep is found there.
34
import { expect, test } from "bun:test"
5+
import fs from "node:fs/promises"
6+
import path from "node:path"
47

5-
test("instance startup survives a file scan that cannot get ripgrep", async () => {
8+
test("a startup scan that cannot get ripgrep is handled, and a later search recovers", async () => {
69
const { tmpdir } = await import("../../fixture/fixture")
710
const { bootstrap } = await import("../../../src/cli/bootstrap")
811
const { Ripgrep } = await import("../../../src/file/ripgrep")
12+
const { File } = await import("../../../src/file")
913
await using dir = await tmpdir({ git: true })
10-
let started = false
14+
await using fake = await tmpdir()
15+
let found: string[] = []
1116
await bootstrap(dir.path, async () => {
12-
started = true
17+
// The scan bootstrap started shares this lookup, so it has failed too by the time this settles.
18+
// One more macrotask lets an unhandled rejection from that scan surface while the test runs.
19+
expect(await Ripgrep.filepath().then(() => "found", () => "failed")).toBe("failed")
20+
await new Promise((resolve) => setTimeout(resolve, 0))
21+
22+
// While ripgrep is still unavailable a search fails, and that failure must not stick.
23+
expect(await File.search({ query: "recovered" }).then(() => "ok", () => "failed")).toBe("failed")
24+
25+
// Ripgrep becomes available: a stand-in that lists one file.
26+
await fs.writeFile(path.join(fake.path, "rg"), "#!/bin/sh\necho recovered.txt\n", { mode: 0o755 })
27+
process.env.PATH = `${fake.path}${path.delimiter}${process.env.PATH}`
28+
found = await File.search({ query: "recovered", type: "file" })
1329
})
14-
expect(started).toBe(true)
15-
// The scan bootstrap started shares this lookup, so it has failed too by the time this settles.
16-
// One more macrotask lets an unhandled rejection from that scan surface before the test ends.
17-
expect(await Ripgrep.filepath().then(() => "found", () => "failed")).toBe("failed")
18-
await new Promise((resolve) => setTimeout(resolve, 0))
30+
expect(found).toContain("recovered.txt")
1931
})

‎packages/opencode/test/altimate/file-scan/startup-scan-failure.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import path from "node:path"
77
// scan downloads it first. A failed download or extraction used to be an unhandled rejection, which
88
// made bun fail a run whose tests had all passed (seen in CI when a nested test run lost its data
99
// directory mid-extract). The child gets a PATH holding only git and a proxy that refuses
10-
// connections, so the download fails at once. A child that exits 0 means the failure was handled.
10+
// connections, so the download fails at once. A child that exits 0 means the failure was handled,
11+
// and that a search recovered once ripgrep became available: neither the failed lookup nor the
12+
// failed scan is kept.
1113
test.skipIf(process.platform === "win32")("a startup file scan that cannot get ripgrep does not fail the run", async () => {
1214
const bin = await fs.mkdtemp(path.join(os.tmpdir(), "no-rg-"))
1315
try {

0 commit comments

Comments
 (0)