diff --git a/server/src/services/remote-workspace-snapshot-archive-git-stdin.test.ts b/server/src/services/remote-workspace-snapshot-archive-git-stdin.test.ts new file mode 100644 index 00000000000..2c703c5f05b --- /dev/null +++ b/server/src/services/remote-workspace-snapshot-archive-git-stdin.test.ts @@ -0,0 +1,75 @@ +import { execFile } from "node:child_process"; +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { promisify } from "node:util"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { captureRemoteWorkspaceDirectory } from "./remote-workspace-snapshot-archive.js"; + +const execute = promisify(execFile); +const gitEnv = { + ...process.env, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: os.devNull, + GIT_AUTHOR_NAME: "Snapshot", GIT_COMMITTER_NAME: "Snapshot", + GIT_AUTHOR_EMAIL: "snapshot@example.test", GIT_COMMITTER_EMAIL: "snapshot@example.test", +}; + +// The git shim is a POSIX shell script, which native Windows cannot run. +describe.skipIf(process.platform === "win32")("workspace archive git input", () => { + let root: string; + + beforeEach(async () => { + root = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-archive-git-stdin-"))); + }); + + afterEach(async () => { + vi.unstubAllEnvs(); + await fs.rm(root, { recursive: true, force: true }); + }); + + it("reports git's own failure when git exits before reading streamed ref updates", async () => { + // The git executable itself, not a wrapper that PATH may resolve to. + const gitExecPath = (await execute("git", ["--exec-path"])).stdout.trim(); + const realGit = path.join(gitExecPath, "git"); + const cwd = path.join(root, "source"); + await fs.mkdir(cwd); + const git = (args: string[], input?: string) => new Promise((resolve, reject) => { + const child = execFile(realGit, ["-C", cwd, ...args], { env: gitEnv, maxBuffer: 16 * 1024 * 1024 }, + (error, stdout) => error ? reject(error) : resolve(stdout.trim())); + if (input !== undefined) child.stdin?.end(input); + }); + await git(["init", "-q", "-b", "main"]); + await fs.writeFile(path.join(cwd, "work.txt"), "base work\n"); + await git(["add", "."]); + await git(["commit", "-qm", "base"]); + const head = await git(["rev-parse", "HEAD"]); + // Enough refs that the streamed `update-ref --stdin` input is larger than a + // pipe buffer, so the archive is still writing when git stops reading. + const refs = Array.from({ length: 3000 }, (_, index) => + `create refs/tags/archive-stdin-${String(index).padStart(5, "0")} ${head}`).join("\n"); + await git(["update-ref", "--stdin"], `${refs}\n`); + + // A git that rejects the ref update without reading its input, like a + // locked or rejected ref, a fatal start-up error, or a killed process. + const bin = path.join(root, "bin"); + await fs.mkdir(bin); + await fs.writeFile(path.join(bin, "git"), `#!/bin/sh +for arg in "$@"; do + if [ "$arg" = "update-ref" ]; then + exec 0<&- + printf '%s\\n' 'fatal: update-ref rejected' >&2 + exit 128 + fi +done +exec ${JSON.stringify(realGit)} "$@" +`, { mode: 0o700 }); + vi.stubEnv("PATH", [bin, gitExecPath, process.env.PATH ?? ""].join(path.delimiter)); + + const failure = await captureRemoteWorkspaceDirectory({ + cwd, archivePath: path.join(root, "snapshot.pcws"), gitSourceRoots: [root], + }).then(() => null, (error: unknown) => error as { code?: unknown; message?: string }); + + expect(failure).not.toBeNull(); + expect(failure?.message).not.toMatch(/EPIPE/u); + expect(failure).toMatchObject({ code: 128, message: expect.stringContaining("update-ref") }); + }, 60_000); +}); diff --git a/server/src/services/remote-workspace-snapshot-archive.ts b/server/src/services/remote-workspace-snapshot-archive.ts index b64e7d8f5b4..f8e8bf48d41 100644 --- a/server/src/services/remote-workspace-snapshot-archive.ts +++ b/server/src/services/remote-workspace-snapshot-archive.ts @@ -72,8 +72,15 @@ export async function workspaceArchive(input: { GIT_OBJECT_DIRECTORY: undefined, GIT_ALTERNATE_OBJECT_DIRECTORIES: undefined, GIT_CONFIG_PARAMETERS: undefined, GIT_CONFIG_COUNT: "0", GIT_TEMPLATE_DIR: undefined, GIT_AUTHOR_NAME: "Paperclip", GIT_COMMITTER_NAME: "Paperclip", GIT_AUTHOR_EMAIL: "workspace@paperclip.invalid", GIT_COMMITTER_EMAIL: "workspace@paperclip.invalid", GIT_AUTHOR_DATE: "@0 +0000", GIT_COMMITTER_DATE: "@0 +0000", GIT_NO_LAZY_FETCH: "1" }, - }, (error, stdout) => error ? reject(error) : resolve(stdout)); - if (stdin !== undefined) command.stdin?.end(stdin); + }, (error, stdout) => error ? reject(error) : inputError ? reject(inputError) : resolve(stdout)); + // Git can exit before it reads all of its input, for example when it + // rejects a ref update. The write then fails with EPIPE. Report git's own + // failure; the input error matters only if git claims success. + let inputError: Error | undefined; + if (stdin !== undefined) { + command.stdin?.on("error", error => { inputError = error; }); + command.stdin?.end(stdin); + } }); const portableRemoteUrl = (value: string) => { if (!value || /[\u0000-\x20\u007f\\]/u.test(value)) return false;