TypeScript
fix(server): Windows worktrees with long paths no longer fail or strand (#14917)
Roger Maxwell4ce6a51a4c4b
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex 1238477d4cb..a71226c06ae 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -35,6 +35,7 @@ import {makeGitVcsDriverCore,parseGitCheckoutProgressLine,splitNullSeparatedGitStdoutPaths,+ windowsLongPathConfigEnv,} from "./GitVcsDriverCore.ts";import * as GitVcsDriver from "./GitVcsDriver.ts";@@ -3955,3 +3956,94 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {);});});++describe("Windows long path configuration", () => {+ const readGitConfig = Effect.fn("readGitConfig")(function* (+ platform: NodeJS.Platform,+ key: string,+ ) {+ const layer = GitVcsDriver.layer.pipe(+ Layer.provide(layerServerConfig),+ Layer.provideMerge(NodeServices.layer),+ Layer.provide(Layer.succeed(HostProcessPlatform, platform)),+ );+ return yield* Effect.gen(function* () {+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const cwd = yield* makeTmpDir("git-longpath-test-");+ const result = yield* driver.execute({+ operation: "GitVcsDriverTest.readGitConfig",+ cwd,+ args: ["config", "--get", key],+ // Replaces the suite's own core.longpaths=true so it cannot mask a+ // missing injection. Entry 2 sits past the count and must stay unread.+ env: {+ GIT_CONFIG_COUNT: "2",+ GIT_CONFIG_KEY_0: "user.name",+ GIT_CONFIG_VALUE_0: "inherited-name",+ GIT_CONFIG_KEY_1: "core.longpaths",+ GIT_CONFIG_VALUE_1: "false",+ GIT_CONFIG_KEY_2: "user.name",+ GIT_CONFIG_VALUE_2: "outside-count",+ },+ });+ return result.stdout.trim();+ }).pipe(Effect.provide(layer), Effect.scoped);+ });++ it.effect("enables long paths for every Git command on Windows", () =>+ Effect.gen(function* () {+ assert.equal(yield* readGitConfig("win32", "core.longpaths"), "true");+ assert.equal(yield* readGitConfig("win32", "user.name"), "inherited-name");+ }),+ );++ it.effect("leaves Git config untouched on other platforms", () =>+ Effect.gen(function* () {+ assert.equal(yield* readGitConfig("linux", "core.longpaths"), "false");+ assert.equal(yield* readGitConfig("linux", "user.name"), "inherited-name");+ }),+ );++ it("extends an inherited count spelled in a different case", () => {+ // Two spellings collapse to one on spawn (the uppercase one wins), so the+ // count must be bumped under the spelling that is already there.+ assert.deepStrictEqual(windowsLongPathConfigEnv("win32", { git_config_count: "2" }), {+ git_config_count: "3",+ GIT_CONFIG_KEY_2: "core.longpaths",+ GIT_CONFIG_VALUE_2: "true",+ });+ });++ it("appends after inherited entries, parsing the count as Git does", () => {+ for (const [count, next] of [+ [undefined, 0],+ ["", 0],+ ["0", 0],+ ["-0", 0],+ ["2", 2],+ [" 2", 2],+ ["+2", 2],+ ["02", 2],+ ] as const) {+ assert.deepStrictEqual(+ windowsLongPathConfigEnv("win32", count === undefined ? {} : { GIT_CONFIG_COUNT: count }),+ {+ GIT_CONFIG_COUNT: String(next + 1),+ [`GIT_CONFIG_KEY_${next}`]: "core.longpaths",+ [`GIT_CONFIG_VALUE_${next}`]: "true",+ },+ `GIT_CONFIG_COUNT=${JSON.stringify(count)}`,+ );+ }+ });++ it("leaves a count Git would reject for Git to report", () => {+ for (const count of ["nope", "2x", "-1", "1.5", " ", "1 "]) {+ assert.deepStrictEqual(+ windowsLongPathConfigEnv("win32", { GIT_CONFIG_COUNT: count }),+ {},+ `GIT_CONFIG_COUNT=${JSON.stringify(count)}`,+ );+ }+ });+});diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 16932533096..002311fda2c 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -899,12 +899,52 @@ const collectOutput = Effect.fnUntraced(function* (};});+/**+ * Git on Windows cannot create or delete a path longer than MAX_PATH (260)+ * unless `core.longpaths` is set. The OS-level LongPathsEnabled setting does not+ * cover it, and git leaves it off by default, so deep worktrees fail to check+ * out and fail midway through removal.+ *+ * Passed through `GIT_CONFIG_*` rather than `-c` so argv stays the same on every+ * platform and nothing is written to the user's config. The entry is appended+ * after any inherited ones. Windows env names are case-insensitive, so the+ * inherited count is found regardless of casing and updated under its own name.+ * A count git would reject is left alone so git still reports it.+ */+export const windowsLongPathConfigEnv = (+ platform: NodeJS.Platform,+ env: NodeJS.ProcessEnv,+): NodeJS.ProcessEnv => {+ if (platform !== "win32") {+ return {};+ }+ const countKey =+ Object.keys(env).find((key) => key.toUpperCase() === "GIT_CONFIG_COUNT") ?? "GIT_CONFIG_COUNT";+ const inherited = env[countKey];+ if (+ inherited !== undefined &&+ inherited !== "" &&+ // Git parses the count with strtoul: leading whitespace, an optional sign+ // (only -0 is non-negative), digits, and nothing after them.+ /^[ \t\r\n\v\f]*(?:\+?\d+|-0+)/.exec(inherited)?.[0] !== inherited+ ) {+ return {};+ }+ const count = inherited === undefined || inherited === "" ? 0 : Number.parseInt(inherited, 10);+ return {+ [countKey]: String(count + 1),+ [`GIT_CONFIG_KEY_${count}`]: "core.longpaths",+ [`GIT_CONFIG_VALUE_${count}`]: "true",+ };+};+export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* () {const fileSystem = yield* FileSystem.FileSystem;const path = yield* Path.Path;const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;const { worktreesDir } = yield* ServerConfig.ServerConfig;const crypto = yield* Crypto.Crypto;+ const hostPlatform = yield* HostProcessPlatform;const executeRaw: GitVcsDriver.GitVcsDriver["Service"]["execute"] = Effect.fnUntraced(function* (input) {@@ -929,18 +969,19 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*}),),);+ const env = {+ ...process.env,+ // Status polling runs beside the user's own git commands; without this,+ // `git status` takes index.lock to save its refreshed index.+ GIT_OPTIONAL_LOCKS: "0",+ ...input.env,+ ...trace2Monitor.env,+ };const child = yield* commandSpawner.spawn(ChildProcess.make("git", commandInput.args, {cwd: commandInput.cwd,- env: {- ...process.env,- // Status polling runs beside the user's own git commands; without this,- // `git status` takes index.lock to save its refreshed index.- GIT_OPTIONAL_LOCKS: "0",- ...input.env,- ...trace2Monitor.env,- },+ env: { ...env, ...windowsLongPathConfigEnv(hostPlatform, env) },}),).pipe(