Skip to content
TypeScript

fix(server): skip git status scans while the index is locked (#9845)

Guillermo Casanova86070cbc7430
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index b2400f68779..fb741b28ee7 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -11,6 +11,7 @@ import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";
import * as Ref from "effect/Ref";
+import * as Result from "effect/Result";
import * as Scope from "effect/Scope";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
@@ -131,6 +132,51 @@ const initRepoWithCommit = (
return { initialBranch };
});
+for (const location of ["root", "nested", "worktree"] as const) {
+ it.effect(
+ `skips clean filters while the ${location} index is locked and resumes after unlock`,
+ () =>
+ Effect.gen(function* () {
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const repository = yield* makeTmpDir();
+ yield* initRepoWithCommit(repository);
+ const cwd = location === "worktree" ? yield* makeTmpDir() : repository;
+ if (location === "worktree") {
+ yield* git(repository, ["worktree", "add", "--detach", cwd]);
+ }
+ yield* git(cwd, ["config", "filter.probe.clean", "echo clean >> .filter-runs; cat"]);
+ yield* writeTextFile(cwd, ".gitattributes", "asset.bin filter=probe\n");
+ yield* writeTextFile(cwd, ".gitignore", ".filter-runs\n");
+ yield* writeTextFile(cwd, "asset.bin", "original\n");
+ yield* git(cwd, ["add", "."]);
+ yield* git(cwd, ["commit", "-m", "filtered asset"]);
+ NodeFS.utimesSync(path.join(cwd, "asset.bin"), 1, 1);
+ const runsPath = path.join(cwd, ".filter-runs");
+ yield* fs.remove(runsPath, { force: true });
+ const indexPath = yield* git(cwd, ["rev-parse", "--git-path", "index"]);
+ const lockPath = `${path.resolve(cwd, indexPath)}.lock`;
+ yield* fs.writeFileString(lockPath, "");
+ const statusCwd = location === "nested" ? path.join(cwd, "nested") : cwd;
+ yield* fs.makeDirectory(statusCwd, { recursive: true });
+
+ for (let poll = 0; poll < 3; poll++) {
+ const result = yield* driver.statusDetailsLocal(statusCwd).pipe(Effect.result);
+ assert.isTrue(Result.isFailure(result));
+ if (Result.isFailure(result)) assert.include(result.failure.detail, "index is locked");
+ }
+ assert.isFalse(yield* fs.exists(runsPath));
+ assert.isTrue(yield* fs.exists(lockPath));
+
+ yield* fs.remove(lockPath);
+ const status = yield* driver.statusDetailsLocal(statusCwd);
+ assert.isFalse(status.hasWorkingTreeChanges);
+ assert.include(yield* fs.readFileString(runsPath), "clean");
+ }).pipe(Effect.provide(TestLayer)),
+ );
+}
+
it.effect("uses stable diagnostics for every parsed non-repository command", () => {
const commands: Array<{ readonly args: ReadonlyArray<string>; readonly lcAll?: string }> = [];
const spawner = ChildProcessSpawner.make((command) =>
@@ -163,6 +209,7 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
yield* driver.listRefs({ cwd });
assert.deepStrictEqual(commands, [
+ { args: ["rev-parse", "--git-path", "index"], lcAll: "C" },
{ args: ["status", "--porcelain=2", "--branch"], lcAll: "C" },
{ args: ["rev-parse", "--abbrev-ref", "HEAD"], lcAll: "C" },
{ args: ["rev-parse", "--git-common-dir"], lcAll: "C" },
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 23f8e6f2a99..3c7e018ddea 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -1584,6 +1584,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
});
const readStatusDetailsLocal = Effect.fn("readStatusDetailsLocal")(function* (cwd: string) {
+ const indexResult = yield* executeGitWithStableDiagnostics(
+ "GitVcsDriver.statusDetails.indexPath",
+ cwd,
+ ["rev-parse", "--git-path", "index"],
+ { allowNonZeroExit: true },
+ ).pipe(
+ Effect.catchTags({
+ GitCommandError: (error) =>
+ isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),
+ }),
+ );
+ if (indexResult === null) return NON_REPOSITORY_STATUS_DETAILS;
+ if (indexResult.exitCode === 0) {
+ const lockPath = `${path.resolve(cwd, indexResult.stdout.trim())}.lock`;
+ const lockError = new GitCommandError({
+ operation: "GitVcsDriver.statusDetails.indexPath",
+ command: "git",
+ cwd,
+ detail: "Git index is locked. Status will resume when the index lock is removed.",
+ });
+ // Status can succeed while locked, repeatedly running LFS clean filters without caching.
+ if (
+ yield* fileSystem.exists(lockPath).pipe(
+ Effect.mapError(
+ (cause) =>
+ new GitCommandError({
+ ...lockError,
+ detail: "Failed to check the Git index lock.",
+ cause,
+ }),
+ ),
+ )
+ ) {
+ return yield* lockError;
+ }
+ }
const statusResult = yield* executeGitWithStableDiagnostics(
"GitVcsDriver.statusDetails.status",
cwd,