Skip to content

Commit a33d50a

Browse files
fix(server): committing a large selection of files no longer fails with spawn E2BIG (pingdotgg#35128)
1 parent 83ee96f commit a33d50a

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,9 +1937,12 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
19371937
yield* git(cwd, ["init"]);
19381938
yield* git(cwd, ["config", "user.email", "[email protected]"]);
19391939
yield* git(cwd, ["config", "user.name", "Test"]);
1940+
// Selected paths add up to more than Linux's default 2 MiB argument limit.
1941+
const dir = "d".repeat(200);
1942+
yield* fs.makeDirectory(path.join(cwd, dir));
19401943
const files = Array.from(
19411944
{ length: 6_000 },
1942-
(_, index) => `file-${String(index).padStart(5, "0")}-${"x".repeat(160)}.txt`,
1945+
(_, index) => `${dir}/file-${String(index).padStart(5, "0")}-${"x".repeat(160)}.txt`,
19431946
);
19441947
yield* Effect.forEach(files, (file) => fs.writeFileString(path.join(cwd, file), "before\n"), {
19451948
concurrency: 16,
@@ -1965,6 +1968,10 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
19651968
assert.isNotNull(context);
19661969
assert.isAbove(context!.stagedSummary.length, 1_000_000);
19671970
assert.lengthOf(context!.stagedSummary.split("\n"), files.length);
1971+
const selected = files.slice(1);
1972+
yield* driver.commit(cwd, "selected", "", { stage: { filePaths: selected } });
1973+
assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "selected");
1974+
assert.equal(yield* git(cwd, ["status", "--porcelain"]), `M ${files[0]}`);
19681975
}),
19691976
);
19701977
describe("structured errors", () => {
@@ -2000,6 +2007,28 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
20002007
}),
20012008
);
20022009

2010+
it.effect("fails with a GitCommandError when spawn throws", () =>
2011+
Effect.gen(function* () {
2012+
const cwd = yield* makeTmpDir();
2013+
const driver = yield* GitVcsDriver.GitVcsDriver;
2014+
2015+
// Over the OS argument limit, Node's spawn throws E2BIG synchronously.
2016+
const error = yield* driver
2017+
.execute({
2018+
operation: "GitVcsDriver.test.spawnThrow",
2019+
cwd,
2020+
args: ["status", "x".repeat(4 * 1024 * 1024)],
2021+
})
2022+
.pipe(Effect.flip);
2023+
2024+
assert.deepInclude(error, {
2025+
_tag: "GitCommandError",
2026+
operation: "GitVcsDriver.test.spawnThrow",
2027+
detail: "Failed to spawn Git process.",
2028+
});
2029+
}),
2030+
);
2031+
20032032
// regression: #8645 name the cause of a failed git command
20042033
it.effect("names the branch conflict behind a failed worktree add", () =>
20052034
Effect.gen(function* () {

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10091009
const env = { ...baseEnv, ...explicitBareRepositoryEnv(baseEnv, hostPlatform) };
10101010
const spawnEnv = { ...env, ...windowsLongPathConfigEnv(hostPlatform, env) };
10111011
const resolved = yield* resolveSpawnCommand("git", [], { env: spawnEnv });
1012+
const spawnError = (cause: unknown) =>
1013+
new GitCommandError({
1014+
...gitCommandContext(commandInput),
1015+
detail: "Failed to spawn Git process.",
1016+
cause,
1017+
});
10121018
// cmd wrappers truncate multiline commits; leave git.cmd handling to Node lookup.
10131019
const executable = resolved.shell ? "git" : resolved.command;
10141020
const child = yield* commandSpawner
@@ -1024,14 +1030,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10241030
}),
10251031
)
10261032
.pipe(
1027-
Effect.mapError(
1028-
(cause) =>
1029-
new GitCommandError({
1030-
...gitCommandContext(commandInput),
1031-
detail: "Failed to spawn Git process.",
1032-
cause,
1033-
}),
1034-
),
1033+
Effect.mapError(spawnError),
1034+
// Synchronous spawn throws (such as E2BIG) arrive as defects.
1035+
Effect.catchDefect((cause) => Effect.fail(spawnError(cause))),
10351036
);
10361037

10371038
const [stdout, stderr, exitCode] = yield* Effect.all(
@@ -2534,9 +2535,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
25342535
yield* runGit(
25352536
"GitVcsDriver.commit.addSelected",
25362537
cwd,
2537-
// Status paths are repository-relative, while cwd can be a subdirectory.
2538-
[...config, "add", "-A", "--", ...filePaths.map((file) => `:(top,literal)${file}`)],
2539-
env ? { env } : {},
2538+
[...config, "add", "-A", "--pathspec-from-file=-", "--pathspec-file-nul"],
2539+
{
2540+
...(env ? { env } : {}),
2541+
// Status paths are repository-relative, while cwd can be a subdirectory.
2542+
stdin: filePaths.map((file) => `:(top,literal)${file}\0`).join(""),
2543+
},
25402544
);
25412545
} else {
25422546
yield* runGit(

0 commit comments

Comments
 (0)