Skip to content

Commit 0533e5d

Browse files
fix(server): committing a large selection of files no longer fails with spawn E2BIG
A selected-file commit passed every path to `git add` as an argument, so a large selection went over the OS argument limit and spawn threw E2BIG. That synchronous throw became a defect instead of a GitCommandError, so the source control action never settled in the client. Send the selected pathspecs to `git add` on stdin with `--pathspec-from-file=- --pathspec-file-nul`, and turn synchronous spawn throws into a typed GitCommandError. Fixes #35112 AI-assisted; verified with the tests and tools listed above.
1 parent bf91048 commit 0533e5d

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
19091909
const selected = files.slice(1);
19101910
yield* driver.commit(cwd, "selected", "", { stage: { filePaths: selected } });
19111911
assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "selected");
1912-
assert.equal(yield* git(cwd, ["status", "--porcelain"]), ` M ${files[0]}`);
1912+
assert.equal(yield* git(cwd, ["status", "--porcelain"]), `M ${files[0]}`);
19131913
}),
19141914
);
19151915
describe("structured errors", () => {
@@ -1945,6 +1945,28 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
19451945
}),
19461946
);
19471947

1948+
it.effect("fails with a GitCommandError when spawn throws", () =>
1949+
Effect.gen(function* () {
1950+
const cwd = yield* makeTmpDir();
1951+
const driver = yield* GitVcsDriver.GitVcsDriver;
1952+
1953+
// Over the OS argument limit, Node's spawn throws E2BIG synchronously.
1954+
const error = yield* driver
1955+
.execute({
1956+
operation: "GitVcsDriver.test.spawnThrow",
1957+
cwd,
1958+
args: ["status", "x".repeat(4 * 1024 * 1024)],
1959+
})
1960+
.pipe(Effect.flip);
1961+
1962+
assert.deepInclude(error, {
1963+
_tag: "GitCommandError",
1964+
operation: "GitVcsDriver.test.spawnThrow",
1965+
detail: "Failed to spawn Git process.",
1966+
});
1967+
}),
1968+
);
1969+
19481970
// regression: #8645 name the cause of a failed git command
19491971
it.effect("names the branch conflict behind a failed worktree add", () =>
19501972
Effect.gen(function* () {

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10031003
const env = { ...baseEnv, ...explicitBareRepositoryEnv(baseEnv, hostPlatform) };
10041004
const spawnEnv = { ...env, ...windowsLongPathConfigEnv(hostPlatform, env) };
10051005
const resolved = yield* resolveSpawnCommand("git", [], { env: spawnEnv });
1006+
const spawnError = (cause: unknown) =>
1007+
new GitCommandError({
1008+
...gitCommandContext(commandInput),
1009+
detail: "Failed to spawn Git process.",
1010+
cause,
1011+
});
10061012
// cmd wrappers truncate multiline commits; leave git.cmd handling to Node lookup.
10071013
const executable = resolved.shell ? "git" : resolved.command;
10081014
const child = yield* commandSpawner
@@ -1018,14 +1024,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10181024
}),
10191025
)
10201026
.pipe(
1021-
Effect.mapError(
1022-
(cause) =>
1023-
new GitCommandError({
1024-
...gitCommandContext(commandInput),
1025-
detail: "Failed to spawn Git process.",
1026-
cause,
1027-
}),
1028-
),
1027+
Effect.mapError(spawnError),
1028+
// Synchronous spawn throws (such as E2BIG) arrive as defects.
1029+
Effect.catchDefect((cause) => Effect.fail(spawnError(cause))),
10291030
);
10301031

10311032
const [stdout, stderr, exitCode] = yield* Effect.all(
@@ -2521,9 +2522,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
25212522
yield* runGit(
25222523
"GitVcsDriver.commit.addSelected",
25232524
cwd,
2524-
// Status paths are repository-relative, while cwd can be a subdirectory.
2525-
[...config, "add", "-A", "--", ...filePaths.map((file) => `:(top,literal)${file}`)],
2526-
env ? { env } : {},
2525+
[...config, "add", "-A", "--pathspec-from-file=-", "--pathspec-file-nul"],
2526+
{
2527+
...(env ? { env } : {}),
2528+
// Status paths are repository-relative, while cwd can be a subdirectory.
2529+
stdin: filePaths.map((file) => `:(top,literal)${file}\0`).join(""),
2530+
},
25272531
);
25282532
} else {
25292533
yield* runGit(

0 commit comments

Comments
 (0)