Skip to content

Commit b835f75

Browse files
committed
fix(server): explain Git index locks when checkpoint restore fails
1 parent 20363c3 commit b835f75

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

‎apps/server/src/vcs/VcsProcess.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import * as Duration from "effect/Duration";
55
import * as Deferred from "effect/Deferred";
66
import * as Effect from "effect/Effect";
77
import * as Fiber from "effect/Fiber";
8+
import * as FileSystem from "effect/FileSystem";
89
import * as Layer from "effect/Layer";
10+
import * as Path from "effect/Path";
911
import * as Queue from "effect/Queue";
1012
import * as Ref from "effect/Ref";
1113
import { TestClock } from "effect/testing";
@@ -50,6 +52,91 @@ const captureProcessResult = (
5052
);
5153

5254
describe("VcsProcess.run", () => {
55+
it.effect.each([
56+
{
57+
name: "Windows index lock",
58+
stderr: "fatal: Unable to create 'C:\\private\\repo\\.git\\index.lock': File exists.\n",
59+
locked: true,
60+
},
61+
{
62+
name: "index permission error",
63+
stderr: "fatal: Unable to create '/private/repo/.git/index.lock': Permission denied\n",
64+
locked: false,
65+
},
66+
{
67+
name: "a different Git lock",
68+
stderr: "fatal: Unable to create '/private/repo/.git/config.lock': File exists.\n",
69+
locked: false,
70+
},
71+
])("distinguishes $name without retaining raw stderr", ({ stderr, locked }) =>
72+
Effect.gen(function* () {
73+
const error = yield* captureProcessResult(
74+
Effect.succeed({
75+
code: ChildProcessSpawner.ExitCode(128),
76+
stdout: "",
77+
stderr,
78+
stdoutTruncated: false,
79+
stderrTruncated: false,
80+
stdoutInvalidUtf8: false,
81+
stderrInvalidUtf8: false,
82+
timedOut: false,
83+
}),
84+
);
85+
expect(error).toMatchObject({
86+
failureKind: "command-failed",
87+
detail: locked
88+
? "Git's index is locked. Wait for other Git operations to finish, then try again."
89+
: "Process exited with a non-zero status.",
90+
});
91+
expect(error.message).not.toContain("private");
92+
expect(error).not.toHaveProperty("stderr");
93+
}),
94+
);
95+
96+
it.effect("explains a locked Git index and allows restore after the lock is released", () =>
97+
Effect.gen(function* () {
98+
const fs = yield* FileSystem.FileSystem;
99+
const path = yield* Path.Path;
100+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-index-lock-" });
101+
const git = (args: readonly string[]) =>
102+
run({ operation: "test.checkpoint.restore", command: "git", cwd, args });
103+
yield* git(["init"]);
104+
const file = path.join(cwd, "README.md");
105+
yield* fs.writeFileString(file, "before\n");
106+
yield* git(["add", "README.md"]);
107+
yield* git([
108+
"-c",
109+
"user.name=Test",
110+
"-c",
111+
112+
"commit",
113+
"-m",
114+
"initial",
115+
]);
116+
yield* fs.writeFileString(file, "after\n");
117+
const lock = path.join(cwd, ".git", "index.lock");
118+
yield* fs.writeFileString(lock, "");
119+
const restore = ["restore", "--source=HEAD", "--worktree", "--staged", "--", "README.md"];
120+
121+
const error = yield* Effect.flip(git(restore));
122+
expect(error).toBeInstanceOf(VcsProcessExitError);
123+
expect(error).toMatchObject({
124+
exitCode: 128,
125+
failureKind: "command-failed",
126+
detail: "Git's index is locked. Wait for other Git operations to finish, then try again.",
127+
});
128+
expect(error.message).not.toContain("index.lock");
129+
expect(error.message).toContain("Git's index is locked.");
130+
expect(error).not.toHaveProperty("stderr");
131+
expect(yield* fs.readFileString(file)).toBe("after\n");
132+
expect(yield* fs.exists(lock)).toBe(true);
133+
134+
yield* fs.remove(lock);
135+
yield* git(restore);
136+
expect(yield* fs.readFileString(file)).toBe("before\n");
137+
}).pipe(provideLive, Effect.provide(NodeServices.layer), Effect.scoped),
138+
);
139+
53140
it.effect("bounds a synthetic burst of GitHub API processes", () =>
54141
Effect.gen(function* () {
55142
const gate = yield* Deferred.make<void>();

‎apps/server/src/vcs/VcsProcess.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export const make = Effect.gen(function* () {
165165
}
166166

167167
if (!input.allowNonZeroExit && result.code !== 0) {
168-
return yield* VcsProcessExitError.fromProcessExit(
168+
const error = VcsProcessExitError.fromProcessExit(
169169
baseError,
170170
{
171171
exitCode: result.code,
@@ -174,6 +174,18 @@ export const make = Effect.gen(function* () {
174174
},
175175
classifyNonZeroExit(input.command, result.stderr),
176176
);
177+
if (
178+
input.command === "git" &&
179+
/^fatal: unable to create ['"][^\r\n]*[/\\]index\.lock['"]: file exists\.?$/im.test(
180+
result.stderr,
181+
)
182+
) {
183+
return yield* new VcsProcessExitError({
184+
...error,
185+
detail: "Git's index is locked. Wait for other Git operations to finish, then try again.",
186+
});
187+
}
188+
return yield* error;
177189
}
178190

179191
return {

0 commit comments

Comments
 (0)