From 0e5574d4aadd54887f087e9899accc7edbb204ac Mon Sep 17 00:00:00 2001 From: Entrpi Date: Thu, 19 Nov 2026 10:57:10 +1100 Subject: [PATCH] fix(test): isolate git fixtures from inherited repository selectors Git hooks and bisect can export repository selectors that redirect fixture Git commands into the caller's repository. Fixture setup can then overwrite local config and refs. Share the server's existing selector rule with common test setup and the upstream fixture harnesses. Use a throwaway repository to verify that replay workspace setup leaves its config and refs intact, and register the regression with CI. User story: I can run tests from a Git hook or bisect without changing the repository that launched them. --- .github/scripts/ci-changes.cjs | 2 +- apps/server/scripts/verify-background-live.ts | 1 + .../src/project/RepositoryIdentityResolver.ts | 3 +- apps/server/src/project/worktreeContainer.ts | 3 +- apps/server/src/vcs/GitVcsDriverCore.ts | 7 +- apps/server/src/vcs/VcsProcess.ts | 24 +------ packages/provider-testing/package.json | 3 +- .../src/replayWorkspace.test.ts | 72 +++++++++++++++++++ packages/shared/package.json | 8 +++ packages/shared/src/gitEnvironment.ts | 22 ++++++ packages/shared/src/testing/gitEnv.ts | 9 +++ packages/shared/src/testing/providerEnv.ts | 2 + scripts/check-changed.test.ts | 2 + scripts/mobile-behavior/environment.ts | 1 + scripts/mobile-showcase-environment.ts | 1 + scripts/scenarios/shell.ts | 1 + 16 files changed, 129 insertions(+), 32 deletions(-) create mode 100644 packages/provider-testing/src/replayWorkspace.test.ts create mode 100644 packages/shared/src/gitEnvironment.ts create mode 100644 packages/shared/src/testing/gitEnv.ts diff --git a/.github/scripts/ci-changes.cjs b/.github/scripts/ci-changes.cjs index 7b7332903ed9f..a0d3f1a1c174a 100644 --- a/.github/scripts/ci-changes.cjs +++ b/.github/scripts/ci-changes.cjs @@ -14,7 +14,7 @@ const INERT = [ // Shared test setup and vendored mobile dependencies affect every package. const RUNS_EVERYTHING = [ - /^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv))\.ts$/, + /^packages\/shared\/src\/(HostProcess|gitEnvironment|testing\/(gitEnv|longTempDir|providerEnv))\.ts$/, /^apps\/mobile\/deps\//, ]; diff --git a/apps/server/scripts/verify-background-live.ts b/apps/server/scripts/verify-background-live.ts index 351d3056c1306..b1878502d00c8 100644 --- a/apps/server/scripts/verify-background-live.ts +++ b/apps/server/scripts/verify-background-live.ts @@ -1,4 +1,5 @@ // @effect-diagnostics nodeBuiltinImport:off globalTimers:off globalDate:off globalFetch:off globalConsole:off globalJsonInEffect:off - Host-side process verifier retains raw wire evidence and owns deadline timers. +import "@t3tools/shared/testing/gitEnv"; import * as NodeAssert from "node:assert/strict"; import * as NodeChildProcess from "node:child_process"; import * as NodeCrypto from "node:crypto"; diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts index 1a992388da2f1..012ef4c9c87d2 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.ts @@ -2,6 +2,7 @@ import type { RepositoryIdentity, SourceControlProviderError } from "@t3tools/co import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition"; import { repositoryPathFromRemoteUrl } from "@t3tools/source-control-core/server/SourceControlProvider"; import { canonicalRepositoryPath, normalizeGitRemoteUrl } from "@t3tools/shared/git"; +import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment"; import * as Cache from "effect/Cache"; import * as Context from "effect/Context"; import * as Data from "effect/Data"; @@ -14,7 +15,7 @@ import * as Path from "effect/Path"; import * as ProcessRunner from "../processRunner.ts"; import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts"; -import { explicitBareRepositoryEnv, unsetInheritedRepositoryEnv } from "../vcs/VcsProcess.ts"; +import { explicitBareRepositoryEnv } from "../vcs/VcsProcess.ts"; import * as HostProcess from "@t3tools/shared/HostProcess"; const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512; diff --git a/apps/server/src/project/worktreeContainer.ts b/apps/server/src/project/worktreeContainer.ts index f20e9c4375cae..f0e1da58cb628 100644 --- a/apps/server/src/project/worktreeContainer.ts +++ b/apps/server/src/project/worktreeContainer.ts @@ -1,10 +1,11 @@ +import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as ProcessRunner from "../processRunner.ts"; -import { explicitBareRepositoryEnv, unsetInheritedRepositoryEnv } from "../vcs/VcsProcess.ts"; +import { explicitBareRepositoryEnv } from "../vcs/VcsProcess.ts"; const parseGitfileTarget = (contents: string): string | undefined => { const firstLine = contents.split(/\r?\n/u, 1)[0] ?? ""; diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 25c7b4505e25f..7d165609a1278 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -37,6 +37,7 @@ import { type VcsRef, } from "@t3tools/contracts"; import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; +import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { compactTraceAttributes } from "@t3tools/shared/observability"; import { decodeJsonResult } from "@t3tools/shared/schemaJson"; @@ -47,11 +48,7 @@ import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observabil import * as GitVcsDriver from "./GitVcsDriver.ts"; import { removeEmptyDirectory, type RemoveEmptyDirectoryError } from "./removeEmptyDirectory.ts"; import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts"; -import { - explicitBareRepositoryEnv, - gitConfigEnv, - unsetInheritedRepositoryEnv, -} from "./VcsProcess.ts"; +import { explicitBareRepositoryEnv, gitConfigEnv } from "./VcsProcess.ts"; import { makeWorktreeCheckoutCache } from "./worktreeCheckoutCache.ts"; import { removeLinkedWorktree } from "./worktreeRemoval.ts"; import { resolveWorktreesDirectory } from "../worktreesDirectory.ts"; diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts index da2f988ce1ed8..226c02f16b259 100644 --- a/apps/server/src/vcs/VcsProcess.ts +++ b/apps/server/src/vcs/VcsProcess.ts @@ -19,6 +19,7 @@ import { VcsProcessStdinWriteError, VcsProcessTimeoutError, } from "@t3tools/contracts"; +import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment"; import * as HostProcess from "@t3tools/shared/HostProcess"; import type { SourceControlProcessInput } from "@t3tools/source-control-core/server/SourceControlHost"; @@ -135,29 +136,6 @@ export const gitConfigEnv = ( export const explicitBareRepositoryEnv = (env: NodeJS.ProcessEnv, platform: NodeJS.Platform) => gitConfigEnv(env, "safe.bareRepository", "explicit", platform); -const REPOSITORY_SELECTORS = new Set([ - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_INDEX_FILE", - "GIT_OBJECT_DIRECTORY", - "GIT_ALTERNATE_OBJECT_DIRECTORIES", -]); - -/** - * Unsets repository selectors the server inherited, such as a Git hook's `GIT_DIR`, so Git finds - * the repository from cwd. Spread it before a command's own env, which may still set them. - */ -export const unsetInheritedRepositoryEnv = ( - inherited: NodeJS.ProcessEnv, - platform: NodeJS.Platform, -): NodeJS.ProcessEnv => - Object.fromEntries( - Object.keys(inherited) - .filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name)) - .map((name) => [name, undefined]), - ); - const classifyNonZeroExit = ( stderr: string, classifyFailure: VcsProcessInput["classifyFailure"], diff --git a/packages/provider-testing/package.json b/packages/provider-testing/package.json index 1f0e3e02fd922..12728541e1a32 100644 --- a/packages/provider-testing/package.json +++ b/packages/provider-testing/package.json @@ -25,7 +25,8 @@ } }, "scripts": { - "typecheck": "effect-tsgo tsrs --noEmit" + "typecheck": "effect-tsgo tsrs --noEmit", + "test": "vp test run --config ../../vite.config.ts --dir ." }, "dependencies": { "@effect/platform-node": "catalog:", diff --git a/packages/provider-testing/src/replayWorkspace.test.ts b/packages/provider-testing/src/replayWorkspace.test.ts new file mode 100644 index 0000000000000..8bf82c95eb076 --- /dev/null +++ b/packages/provider-testing/src/replayWorkspace.test.ts @@ -0,0 +1,72 @@ +// @effect-diagnostics nodeBuiltinImport:off - exercises inherited environment in an isolated subprocess with throwaway repositories. +import * as NodeChildProcess from "node:child_process"; +import * as NodeFS from "node:fs"; +import * as NodeOS from "node:os"; +import * as NodePath from "node:path"; +import { expect, it } from "vite-plus/test"; + +it("keeps inherited repository config and refs unchanged when creating a replay workspace", () => { + const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-git-env-")); + const target = NodePath.join(root, "target"); + const gitDir = NodePath.join(target, ".git"); + const env = { + PATH: process.env.PATH ?? process.env.Path, + SystemRoot: process.env.SystemRoot, + TMPDIR: root, + TMP: root, + TEMP: root, + GIT_CONFIG_GLOBAL: NodePath.join(root, "global-git-config"), + GIT_CONFIG_NOSYSTEM: "1", + }; + const git = (...args: string[]) => + NodeChildProcess.execFileSync("git", args, { cwd: target, encoding: "utf8", env }); + try { + NodeFS.mkdirSync(target); + git("init", "--quiet"); + git( + "-c", + "user.name=Sentinel", + "-c", + "user.email=sentinel@example.invalid", + "commit", + "--quiet", + "--allow-empty", + "-m", + "sentinel", + ); + const configPath = NodePath.join(gitDir, "config"); + const config = NodeFS.readFileSync(configPath, "utf8"); + const refs = git("show-ref"); + const setup = new URL("../../shared/src/testing/providerEnv.ts", import.meta.url).href; + const fixture = new URL("./replayWorkspace.ts", import.meta.url).href; + const result = NodeChildProcess.spawnSync( + process.execPath, + [ + "--input-type=module", + "-e", + ` + import ${JSON.stringify(setup)}; + import { makeCheckpointWorkspace } from ${JSON.stringify(fixture)}; + await makeCheckpointWorkspace("inherited-git-env"); + `, + ], + { + encoding: "utf8", + env: { + ...env, + GIT_DIR: gitDir, + GIT_COMMON_DIR: gitDir, + GIT_OBJECT_DIRECTORY: NodePath.join(gitDir, "objects"), + GIT_ALTERNATE_OBJECT_DIRECTORIES: NodePath.join(gitDir, "objects"), + GIT_WORK_TREE: target, + GIT_INDEX_FILE: NodePath.join(root, "inherited-index"), + }, + }, + ); + expect(NodeFS.readFileSync(configPath, "utf8")).toBe(config); + expect(git("show-ref")).toBe(refs); + expect(result.status, result.stderr).toBe(0); + } finally { + NodeFS.rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/packages/shared/package.json b/packages/shared/package.json index 781d1a4e125e1..129c3a8f602e8 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -75,6 +75,10 @@ "types": "./src/git.ts", "import": "./src/git.ts" }, + "./gitEnvironment": { + "types": "./src/gitEnvironment.ts", + "import": "./src/gitEnvironment.ts" + }, "./sourceControl": { "types": "./src/sourceControl.ts", "import": "./src/sourceControl.ts" @@ -375,6 +379,10 @@ "types": "./src/HostProcess.ts", "import": "./src/HostProcess.ts" }, + "./testing/gitEnv": { + "types": "./src/testing/gitEnv.ts", + "import": "./src/testing/gitEnv.ts" + }, "./testing/longTempDir": { "types": "./src/testing/longTempDir.ts", "import": "./src/testing/longTempDir.ts" diff --git a/packages/shared/src/gitEnvironment.ts b/packages/shared/src/gitEnvironment.ts new file mode 100644 index 0000000000000..25fd1326e2f64 --- /dev/null +++ b/packages/shared/src/gitEnvironment.ts @@ -0,0 +1,22 @@ +const REPOSITORY_SELECTORS = new Set([ + "GIT_DIR", + "GIT_WORK_TREE", + "GIT_COMMON_DIR", + "GIT_INDEX_FILE", + "GIT_OBJECT_DIRECTORY", + "GIT_ALTERNATE_OBJECT_DIRECTORIES", +]); + +/** + * Unsets repository selectors the caller inherited, such as a Git hook's `GIT_DIR`, so Git finds + * the repository from cwd. Spread it before a command's own env, which may still set them. + */ +export const unsetInheritedRepositoryEnv = ( + inherited: NodeJS.ProcessEnv, + platform: NodeJS.Platform, +): NodeJS.ProcessEnv => + Object.fromEntries( + Object.keys(inherited) + .filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name)) + .map((name) => [name, undefined]), + ); diff --git a/packages/shared/src/testing/gitEnv.ts b/packages/shared/src/testing/gitEnv.ts new file mode 100644 index 0000000000000..2830fe8acb80e --- /dev/null +++ b/packages/shared/src/testing/gitEnv.ts @@ -0,0 +1,9 @@ +import * as HostProcess from "../HostProcess.ts"; +import { unsetInheritedRepositoryEnv } from "../gitEnvironment.ts"; + +// Fixtures must discover their own repositories even when tests run from a hook or bisect. +for (const key of Object.keys( + unsetInheritedRepositoryEnv(process.env, HostProcess.Platform.defaultValue()), +)) { + delete process.env[key]; +} diff --git a/packages/shared/src/testing/providerEnv.ts b/packages/shared/src/testing/providerEnv.ts index 4961b70141b84..2a0a7d2398758 100644 --- a/packages/shared/src/testing/providerEnv.ts +++ b/packages/shared/src/testing/providerEnv.ts @@ -1,3 +1,5 @@ +import "./gitEnv.ts"; + // Agent shells carry real provider credentials (CLIProxy sets ANTHROPIC_AUTH_TOKEN and // ANTHROPIC_BASE_URL) and the agent's own T3 state. Tests that read the ambient environment // then pass or fail by who runs them. Tests that need one of these variables set it diff --git a/scripts/check-changed.test.ts b/scripts/check-changed.test.ts index 37bcc389fc229..31530c5320f35 100644 --- a/scripts/check-changed.test.ts +++ b/scripts/check-changed.test.ts @@ -226,6 +226,8 @@ describe("check:changed", () => { }); it.each([ + "packages/shared/src/gitEnvironment.ts", + "packages/shared/src/testing/gitEnv.ts", "packages/shared/src/testing/longTempDir.ts", "packages/shared/src/testing/providerEnv.ts", ])("tests every package when the shared test setup file %s changes", (file) => { diff --git a/scripts/mobile-behavior/environment.ts b/scripts/mobile-behavior/environment.ts index 000e8d2f030bb..b660eaad39de8 100644 --- a/scripts/mobile-behavior/environment.ts +++ b/scripts/mobile-behavior/environment.ts @@ -3,6 +3,7 @@ * An isolated T3 server for one device run. Its only provider is `evalbot`, an Fx instance * backed by `fake-fx.ts`, so every reply is scripted. State lives under the run's `--out`. */ +import "@t3tools/shared/testing/gitEnv"; import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; import * as NodeNet from "node:net"; diff --git a/scripts/mobile-showcase-environment.ts b/scripts/mobile-showcase-environment.ts index e7e2c76b6a2f2..74a4496732340 100644 --- a/scripts/mobile-showcase-environment.ts +++ b/scripts/mobile-showcase-environment.ts @@ -1,4 +1,5 @@ // @effect-diagnostics nodeBuiltinImport:off globalTimers:off globalDate:off - This host-side fixture creates an isolated local T3 environment. +import "@t3tools/shared/testing/gitEnv"; import * as NodeChildProcess from "node:child_process"; import * as NodeFSP from "node:fs/promises"; import * as NodePath from "node:path"; diff --git a/scripts/scenarios/shell.ts b/scripts/scenarios/shell.ts index 40e3b224fa694..f664eb977a0f0 100644 --- a/scripts/scenarios/shell.ts +++ b/scripts/scenarios/shell.ts @@ -1,3 +1,4 @@ +import "@t3tools/shared/testing/gitEnv"; import * as Console from "effect/Console"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema";