Skip to content

Commit b879749

Browse files
That1Drifterclaude
andcommitted
fix(server): enable long paths for every git spawn through VcsProcess
Forgejo PR checkout and the checkpoint ls-files stream called VcsProcess directly and bypassed the GitVcsDriver wrapper, so they ran without core.longpaths. Inject at VcsProcess.run for git instead, which covers every caller, and revert the wrapper change. Also uppercase indexed GIT_CONFIG_KEY_n and GIT_CONFIG_VALUE_n names on Windows, so a caller entry replaces an inherited one regardless of casing, and restore the exact env assertion in the forwarding test. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 4b4944d commit b879749

6 files changed

Lines changed: 126 additions & 79 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
10251025
outputMode: "error",
10261026
});
10271027

1028-
assert.deepInclude(observedEnv, {
1028+
assert.deepStrictEqual(observedEnv, {
10291029
GIT_INDEX_FILE: "/tmp/t3-index",
10301030
});
10311031
assert.strictEqual(observedAppendTruncationMarker, true);

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ import {
3232
type VcsStatusResult,
3333
type WorktreeSubmodules,
3434
} from "@t3tools/contracts";
35-
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
3635
import {
37-
gitCommandEnv,
3836
makeGitVcsDriverCore,
3937
PATCH_RENDER_PREFIX_ARGS,
4038
splitNullSeparatedGitStdoutPaths,
@@ -474,7 +472,7 @@ function parseGitRemoteVerboseOutput(
474472
return remotes;
475473
}
476474

477-
const gitCommand = Effect.fn("GitVcsDriver.gitCommand")(function* (
475+
const gitCommand = (
478476
process: VcsProcess.VcsProcess["Service"],
479477
operation: string,
480478
cwd: string,
@@ -488,20 +486,15 @@ const gitCommand = Effect.fn("GitVcsDriver.gitCommand")(function* (
488486
readonly outputMode?: VcsProcess.VcsProcessInput["outputMode"];
489487
readonly appendTruncationMarker?: boolean;
490488
},
491-
) {
492-
const platform = yield* HostProcessPlatform;
493-
let env = options?.env;
494-
if (platform === "win32") {
495-
env = gitCommandEnv(platform, globalThis.process.env, env);
496-
}
497-
return yield* process.run({
489+
) =>
490+
process.run({
498491
operation,
499492
command: "git",
500493
args: ["-C", cwd, ...args],
501494
cwd,
502495
spawnCwd: globalThis.process.cwd(),
503496
...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
504-
...(env !== undefined ? { env } : {}),
497+
...(options?.env !== undefined ? { env: options.env } : {}),
505498
...(options?.allowNonZeroExit !== undefined
506499
? { allowNonZeroExit: options.allowNonZeroExit }
507500
: {}),
@@ -512,7 +505,6 @@ const gitCommand = Effect.fn("GitVcsDriver.gitCommand")(function* (
512505
? { appendTruncationMarker: options.appendTruncationMarker }
513506
: {}),
514507
});
515-
});
516508

517509
export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* () {
518510
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ import {
3131
import { ServerConfig } from "../config.ts";
3232
import { gitCommandDuration } from "../observability/Metrics.ts";
3333
import {
34-
gitCommandEnv,
3534
makeGitVcsDriverCore,
3635
parseGitCheckoutProgressLine,
3736
splitNullSeparatedGitStdoutPaths,
38-
windowsLongPathConfigEnv,
3937
} from "./GitVcsDriverCore.ts";
4038
import * as GitVcsDriver from "./GitVcsDriver.ts";
39+
import { gitCommandEnv, windowsLongPathConfigEnv } from "./VcsProcess.ts";
4140

4241
const encodeGitCommandError = Schema.encodeEffect(Schema.fromJsonString(GitCommandError));
4342

@@ -3347,6 +3346,28 @@ describe("gitCommandEnv", () => {
33473346
}
33483347
});
33493348

3349+
it("lets caller config entries replace inherited ones regardless of casing", () => {
3350+
const host = {
3351+
GIT_CONFIG_COUNT: "2",
3352+
GIT_CONFIG_KEY_0: "user.name",
3353+
GIT_CONFIG_VALUE_0: "host-name",
3354+
GIT_CONFIG_KEY_1: "user.email",
3355+
GIT_CONFIG_VALUE_1: "[email protected]",
3356+
};
3357+
const caller = {
3358+
git_config_count: "1",
3359+
git_config_key_0: "user.name",
3360+
git_config_value_0: "caller-name",
3361+
};
3362+
assert.deepStrictEqual(gitCommandEnv("win32", host, caller), {
3363+
GIT_CONFIG_COUNT: "2",
3364+
GIT_CONFIG_KEY_0: "user.name",
3365+
GIT_CONFIG_VALUE_0: "caller-name",
3366+
GIT_CONFIG_KEY_1: "core.longpaths",
3367+
GIT_CONFIG_VALUE_1: "true",
3368+
});
3369+
});
3370+
33503371
it("preserves a malformed caller override for Git to reject", () => {
33513372
assert.deepStrictEqual(
33523373
gitCommandEnv("win32", { GIT_CONFIG_COUNT: "2" }, { git_config_count: "nope" }),

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

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
parseRemoteRefWithRemoteNames,
4242
} from "../git/remoteRefs.ts";
4343
import { ServerConfig } from "../config.ts";
44+
import { gitCommandEnv } from "./VcsProcess.ts";
4445

4546
const DEFAULT_TIMEOUT_MS = 30_000;
4647
const gitProcesses = Semaphore.makeUnsafe(8);
@@ -112,68 +113,6 @@ const NON_REPOSITORY_REMOTE_STATUS_DETAILS = Object.freeze<GitVcsDriver.GitRemot
112113
aheadOfDefaultCount: 0,
113114
});
114115

115-
/**
116-
* Git on Windows refuses to create or delete a path longer than MAX_PATH (260)
117-
* unless `core.longpaths` is set. The OS-level LongPathsEnabled setting does not
118-
* cover it: git opts in per repository, and it is off by default.
119-
*
120-
* Worktrees are where this bites. A worktree base path is longer than the
121-
* repository root, so a repository that clones fine can still fail to check out
122-
* into a worktree, and fail to be removed afterwards. A failed removal can drop
123-
* the administrative record and leave files that `git worktree list` cannot see.
124-
*
125-
* Passed per invocation through `GIT_CONFIG_*` rather than argv so the config
126-
* reaches every git subcommand without changing the command line, and nothing is
127-
* written to the user's config. Appended after any entries the caller already
128-
* set so an inherited `GIT_CONFIG_COUNT` keeps working.
129-
*
130-
* A `GIT_CONFIG_COUNT` that is not a count is left alone. Git rejects a bogus
131-
* value itself, and appending to it would overwrite the caller's first entry and
132-
* turn that loud failure into a silently different config.
133-
*/
134-
export const windowsLongPathConfigEnv = (
135-
platform: NodeJS.Platform,
136-
env: NodeJS.ProcessEnv,
137-
): NodeJS.ProcessEnv => {
138-
if (platform !== "win32") {
139-
return {};
140-
}
141-
const countKey =
142-
Object.keys(env).find((key) => key.toUpperCase() === "GIT_CONFIG_COUNT") ?? "GIT_CONFIG_COUNT";
143-
const inherited = env[countKey];
144-
if (
145-
inherited !== undefined &&
146-
inherited !== "" &&
147-
/^[ \t\r\n\v\f]*\+?\d+/.exec(inherited)?.[0] !== inherited
148-
) {
149-
return {};
150-
}
151-
const count = inherited === undefined || inherited === "" ? 0 : Number.parseInt(inherited, 10);
152-
return {
153-
[countKey]: String(count + 1),
154-
[`GIT_CONFIG_KEY_${count}`]: "core.longpaths",
155-
[`GIT_CONFIG_VALUE_${count}`]: "true",
156-
};
157-
};
158-
159-
/** Merge in precedence order so a caller's count wins regardless of casing on Windows. */
160-
export const gitCommandEnv = (
161-
platform: NodeJS.Platform,
162-
...sources: ReadonlyArray<NodeJS.ProcessEnv | undefined>
163-
): NodeJS.ProcessEnv => {
164-
const env: NodeJS.ProcessEnv = {};
165-
for (const source of sources) {
166-
if (platform !== "win32") {
167-
Object.assign(env, source);
168-
continue;
169-
}
170-
for (const [key, value] of Object.entries(source ?? {})) {
171-
env[key.toUpperCase() === "GIT_CONFIG_COUNT" ? "GIT_CONFIG_COUNT" : key] = value;
172-
}
173-
}
174-
return { ...env, ...windowsLongPathConfigEnv(platform, env) };
175-
};
176-
177116
type TraceTailState = {
178117
processedChars: number;
179118
remainder: string;

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, describe, expect, it } from "@effect/vitest";
3-
import { HostProcessWorkingDirectory } from "@t3tools/shared/hostProcess";
3+
import { HostProcessPlatform, HostProcessWorkingDirectory } from "@t3tools/shared/hostProcess";
44
import * as Duration from "effect/Duration";
55
import * as Deferred from "effect/Deferred";
66
import * as Effect from "effect/Effect";
@@ -292,6 +292,26 @@ describe("VcsProcess.run", () => {
292292
}).pipe(provideLive),
293293
);
294294

295+
it.effect.each(["win32", "linux"] as const)(
296+
"enables Git long paths only on Windows hosts (%s)",
297+
(platform) =>
298+
Effect.gen(function* () {
299+
const result = yield* run({
300+
operation: "test.longpaths",
301+
command: "git",
302+
args: ["config", "--get", "core.longpaths"],
303+
cwd: process.cwd(),
304+
env: {
305+
GIT_CONFIG_COUNT: "1",
306+
GIT_CONFIG_KEY_0: "core.longpaths",
307+
GIT_CONFIG_VALUE_0: "false",
308+
},
309+
});
310+
311+
expect(result.stdout.trim()).toBe(platform === "win32" ? "true" : "false");
312+
}).pipe(provideLive, Effect.provideService(HostProcessPlatform, platform)),
313+
);
314+
295315
it.effect("writes stdin before waiting for exit", () =>
296316
Effect.gen(function* () {
297317
const result = yield* run({

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
VcsProcessStdinWriteError,
1919
VcsProcessTimeoutError,
2020
} from "@t3tools/contracts";
21+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
2122
import * as ProcessRunner from "../processRunner.ts";
2223

2324
export interface VcsProcessInput {
@@ -113,6 +114,75 @@ const isTransientGitExit = (stderr: string) =>
113114
/unable to create [^\n]*\.lock['"]?: file exists/i.test(stderr) ||
114115
/(?:unable to stat|lstat\(|error: open\()[^\n]+: no such file or directory/i.test(stderr);
115116

117+
/**
118+
* Git on Windows refuses to create or delete a path longer than MAX_PATH (260)
119+
* unless `core.longpaths` is set. The OS-level LongPathsEnabled setting does not
120+
* cover it: git opts in per repository, and it is off by default.
121+
*
122+
* Worktrees are where this bites. A worktree base path is longer than the
123+
* repository root, so a repository that clones fine can still fail to check out
124+
* into a worktree, and fail to be removed afterwards. A failed removal can drop
125+
* the administrative record and leave files that `git worktree list` cannot see.
126+
*
127+
* Passed per invocation through `GIT_CONFIG_*` rather than argv so the config
128+
* reaches every git subcommand without changing the command line, and nothing is
129+
* written to the user's config. Appended after any entries the caller already
130+
* set so an inherited `GIT_CONFIG_COUNT` keeps working.
131+
*
132+
* A `GIT_CONFIG_COUNT` that is not a count is left alone. Git rejects a bogus
133+
* value itself, and appending to it would overwrite the caller's first entry and
134+
* turn that loud failure into a silently different config.
135+
*/
136+
export const windowsLongPathConfigEnv = (
137+
platform: NodeJS.Platform,
138+
env: NodeJS.ProcessEnv,
139+
): NodeJS.ProcessEnv => {
140+
if (platform !== "win32") {
141+
return {};
142+
}
143+
const countKey =
144+
Object.keys(env).find((key) => key.toUpperCase() === "GIT_CONFIG_COUNT") ?? "GIT_CONFIG_COUNT";
145+
const inherited = env[countKey];
146+
if (
147+
inherited !== undefined &&
148+
inherited !== "" &&
149+
/^[ \t\r\n\v\f]*\+?\d+/.exec(inherited)?.[0] !== inherited
150+
) {
151+
return {};
152+
}
153+
const count = inherited === undefined || inherited === "" ? 0 : Number.parseInt(inherited, 10);
154+
return {
155+
[countKey]: String(count + 1),
156+
[`GIT_CONFIG_KEY_${count}`]: "core.longpaths",
157+
[`GIT_CONFIG_VALUE_${count}`]: "true",
158+
};
159+
};
160+
161+
const GIT_CONFIG_ENV_KEY = /^GIT_CONFIG_(?:COUNT|KEY_\d+|VALUE_\d+)$/i;
162+
163+
/**
164+
* Merge env sources in precedence order for a git spawn. On Windows, env names
165+
* are case-insensitive and spawn keeps only one spelling of each, so `GIT_CONFIG_*`
166+
* names are uppercased first and a later source's entry replaces an earlier one
167+
* regardless of casing.
168+
*/
169+
export const gitCommandEnv = (
170+
platform: NodeJS.Platform,
171+
...sources: ReadonlyArray<NodeJS.ProcessEnv | undefined>
172+
): NodeJS.ProcessEnv => {
173+
const env: NodeJS.ProcessEnv = {};
174+
for (const source of sources) {
175+
if (platform !== "win32") {
176+
Object.assign(env, source);
177+
continue;
178+
}
179+
for (const [key, value] of Object.entries(source ?? {})) {
180+
env[GIT_CONFIG_ENV_KEY.test(key) ? key.toUpperCase() : key] = value;
181+
}
182+
}
183+
return { ...env, ...windowsLongPathConfigEnv(platform, env) };
184+
};
185+
116186
export const make = Effect.gen(function* () {
117187
const processRunner = yield* ProcessRunner.ProcessRunner;
118188
const vcsProcesses = yield* Semaphore.make(VCS_PROCESS_CONCURRENCY);
@@ -125,6 +195,11 @@ export const make = Effect.gen(function* () {
125195
cwd: input.cwd,
126196
argumentCount: input.args.length,
127197
};
198+
const platform = yield* HostProcessPlatform;
199+
const env =
200+
input.command === "git" && platform === "win32"
201+
? gitCommandEnv(platform, process.env, input.env)
202+
: input.env;
128203

129204
const result = yield* processRunner
130205
.run({
@@ -134,7 +209,7 @@ export const make = Effect.gen(function* () {
134209
...(input.spawnCwd !== undefined ? { spawnCwd: input.spawnCwd } : {}),
135210
...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
136211
...(input.onStdoutChunk !== undefined ? { onStdoutChunk: input.onStdoutChunk } : {}),
137-
...(input.env !== undefined ? { env: input.env } : {}),
212+
...(env !== undefined ? { env } : {}),
138213
timeout: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
139214
maxOutputBytes: input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES,
140215
outputMode: input.outputMode ?? "truncate",

0 commit comments

Comments
 (0)