From 6dbb5f50089f520884fc1ca42cb4ac3efe0b991e Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Thu, 27 Aug 2026 11:44:44 -0400 Subject: [PATCH] fix(core): reuse ignore checks during index refresh (#45459) --- .changeset/git-ignore-check.md | 5 +++ packages/core/src/git.ts | 22 ++++------- packages/core/test/git.test.ts | 60 +++++++++++++++++++++++++++++ packages/core/test/snapshot.test.ts | 25 ++++++++++++ 4 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 .changeset/git-ignore-check.md diff --git a/.changeset/git-ignore-check.md b/.changeset/git-ignore-check.md new file mode 100644 index 00000000000..cc6699a75ed --- /dev/null +++ b/.changeset/git-ignore-check.md @@ -0,0 +1,5 @@ +--- +"@opencode-ai/core": patch +--- + +Propagate fatal Git ignore-check errors during index refresh so best-effort snapshot capture returns unavailable instead of treating the check as having no matches. diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts index 7388045840d..21698b8e1ec 100644 --- a/packages/core/src/git.ts +++ b/packages/core/src/git.ts @@ -398,22 +398,16 @@ const layer = Layer.effect( ], { concurrency: 2 }, ) - const candidates = Array.from(new Set([...tracked, ...untracked])) + const candidates = Array.from(new Set([...tracked, ...untracked])).map((file) => RelativePath.make(file)) if (!candidates.length) return { skipped: [] } - const ignored = input.ignores - ? new Set( - (yield* repositoryOperation("refresh", input.ignores, ["check-ignore", "--no-index", "--stdin", "-z"], { - stdin: candidates.join("\0") + "\0", - }).pipe(Effect.orElseSucceed(() => ({ text: "", stderr: "" })))).text - .split("\0") - .filter(Boolean), - ) - : new Set() - const allowed = candidates.filter((item) => !ignored.has(item)) + const excluded = input.ignores + ? yield* ignored({ repository: input.ignores, paths: candidates }) + : new Set() + const allowed = candidates.filter((item) => !excluded.has(item)) const maximum = input.maximumUntrackedFileBytes const skipped = maximum ? (yield* Effect.forEach( - untracked.filter((item) => allowed.includes(item)), + untracked.filter((item) => allowed.includes(RelativePath.make(item))), (item) => fs.stat(path.join(input.repository.worktree, item)).pipe( Effect.map((info) => @@ -424,8 +418,8 @@ const layer = Layer.effect( { concurrency: 8 }, )).filter((item): item is RelativePath => item !== undefined) : [] - const stage = allowed.filter((item) => !skipped.includes(RelativePath.make(item))) - const remove = [...ignored, ...skipped] + const stage = allowed.filter((item) => !skipped.includes(item)) + const remove = [...excluded, ...skipped] if (remove.length) yield* repositoryOperation( "refresh", diff --git a/packages/core/test/git.test.ts b/packages/core/test/git.test.ts index b930e614048..a099031d8bc 100644 --- a/packages/core/test/git.test.ts +++ b/packages/core/test/git.test.ts @@ -131,6 +131,66 @@ describe("Git worktrees", () => { }) describe("Git trees", () => { + ;[0, 1, 128].forEach((exitCode) => { + it.live(`refresh handles check-ignore exit ${exitCode}`, () => + Effect.gen(function* () { + const root = yield* Effect.acquireRelease( + Effect.promise(() => tmpdir()), + (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()), + ) + const project = path.join(root.path, "project") + const paths = ["scope/allowed.txt", "scope/ignored name.txt"].map((file) => RelativePath.make(file)) + yield* Effect.promise(async () => { + await fs.mkdir(path.join(project, "scope"), { recursive: true }) + await initRepo(project) + await Promise.all(paths.map((file) => Bun.write(path.join(project, file), "one\n"))) + await $`git add .`.cwd(project).quiet() + await $`git commit -m initial`.cwd(project).quiet() + }) + const git = yield* Git.Service + const source = yield* git.repo.discover(AbsolutePath.make(project)) + if (!source) throw new Error("Repository not found") + const repository = yield* git.repo.create({ + worktree: source.worktree, + gitDirectory: AbsolutePath.make(path.join(root.path, "storage")), + seed: source, + }) + const before = yield* git.tree.write(repository) + yield* Effect.promise(async () => { + await Promise.all(paths.map((file) => Bun.write(path.join(project, file), "two\n"))) + await Bun.write(path.join(source.gitDirectory, "info", "exclude"), exitCode === 0 ? `${paths[1]}\n` : "") + if (exitCode === 128) await Bun.write(path.join(source.gitDirectory, "config"), "[broken\n") + const result = + await $`git --git-dir ${source.gitDirectory} --work-tree ${source.worktree} check-ignore --no-index --stdin -z < ${Buffer.from(paths.join("\0") + "\0")}` + .cwd(project) + .quiet() + .nothrow() + expect(result.exitCode).toBe(exitCode) + }) + const refresh = git.index.refresh({ repository, scope: RelativePath.make("scope"), ignores: source }) + if (exitCode === 128) { + const error = yield* refresh.pipe(Effect.flip) + expect(error).toBeInstanceOf(Git.OperationError) + expect(error.message).toContain("bad config line") + expect(yield* git.tree.write(repository)).toBe(before) + expect( + yield* git.index.refresh({ repository, scope: RelativePath.make("missing"), ignores: source }), + ).toEqual({ skipped: [] }) + return + } + expect(yield* git.index.ignored({ repository: source, paths })).toEqual( + new Set(exitCode === 0 ? [paths[1]] : []), + ) + expect(yield* refresh).toEqual({ skipped: [] }) + const after = yield* git.tree.write(repository) + expect((yield* git.tree.diff({ repository, from: before, to: after })).map((file) => file.status)).toEqual([ + "modified", + exitCode === 0 ? "deleted" : "modified", + ]) + }), + ) + }) + it.live("lists both sides of a rename as separate file changes", () => Effect.gen(function* () { const root = yield* Effect.acquireRelease( diff --git a/packages/core/test/snapshot.test.ts b/packages/core/test/snapshot.test.ts index 988db9fcddb..122818facd5 100644 --- a/packages/core/test/snapshot.test.ts +++ b/packages/core/test/snapshot.test.ts @@ -127,6 +127,31 @@ describe("Snapshot", () => { ), ) + testEffect(Layer.empty).live("treats fatal ignore checks as unavailable captures", () => + Effect.acquireUseRelease( + Effect.promise(() => tmpdir()), + (tmp) => + Effect.gen(function* () { + const project = path.join(tmp.path, "project") + yield* Effect.promise(async () => { + await fs.mkdir(project) + await Bun.write(path.join(project, "tracked.txt"), "one\n") + await initGit(project) + }) + yield* Effect.gen(function* () { + const snapshot = yield* Snapshot.Service + expect(yield* snapshot.capture()).toBeDefined() + yield* Effect.promise(async () => { + await Bun.write(path.join(project, "tracked.txt"), "two\n") + await Bun.write(path.join(project, ".git", "config"), "[broken\n") + }) + expect(yield* snapshot.capture()).toBeUndefined() + }).pipe(Effect.provide(snapshotLayer(tmp.path, project))) + }), + (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), + ), + ) + testEffect(Layer.empty).live("applies availability transforms", () => Effect.acquireUseRelease( Effect.promise(() => tmpdir()),