fix(git): report missing cwd distinctly from missing git binary

A deleted/nonexistent cwd also makes Bun.spawn throw ENOENT; only claim
'git is not installed.' when the working directory actually exists, else
name the missing directory. Addresses the Codex P2 review on #6237.
This commit is contained in:
can1357
2026-07-22 20:42:10 +02:00
parent a4bd7a4df3
commit a07d72f638
2 changed files with 17 additions and 1 deletions
+8 -1
View File
@@ -442,7 +442,14 @@ async function git(cwd: string, args: readonly string[], options: CommandOptions
windowsHide: true,
});
} catch (err) {
if (isEnoent(err)) return { exitCode: GIT_SPAWN_ENOENT_EXIT_CODE, stdout: "", stderr: "git is not installed." };
if (isEnoent(err)) {
// A deleted/nonexistent cwd also surfaces as a spawn ENOENT; only blame
// the binary when the working directory actually exists.
const stderr = fs.existsSync(cwd)
? "git is not installed."
: `working directory does not exist: ${cwd}`;
return { exitCode: GIT_SPAWN_ENOENT_EXIT_CODE, stdout: "", stderr };
}
throw err;
}
@@ -33,6 +33,15 @@ describe("git helpers with git binary absent (#6169)", () => {
await expect(git.diff.has("/")).rejects.toThrow("git is not installed.");
});
it("does not blame the git binary when the cwd is what is missing", async () => {
vi.spyOn(Bun, "spawn").mockImplementation(throwSpawnEnoent);
// A deleted cwd also makes spawn throw ENOENT; the error must name the
// directory, not falsely claim git is uninstalled.
await expect(git.diff.has("/nonexistent-omp-eval-dir")).rejects.toThrow(
"working directory does not exist: /nonexistent-omp-eval-dir",
);
});
it("repo.root degrades to null instead of throwing ENOENT", async () => {
vi.spyOn(Bun, "spawn").mockImplementation(throwSpawnEnoent);
expect(await git.repo.root("/")).toBeNull();