@devries-cameron20 / t3code-1 / commits / 410109da5d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(testing): test git commands stay out of the outer repository (#38804)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Navid Nadali committed Nov 19, 2026 at 02:41 UTC 410109da5d846c17a40eadb5f92b2df6206b7f6a
15 files changed +184 -6
.github/scripts/ci-changes.cjs
+1 -1
@@ -14,7 +14,7 @@ const INERT = [
14
15 // Shared test setup and vendored mobile dependencies affect every package.
16 const RUNS_EVERYTHING = [
17 - /^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv))\.ts$/,
17 + /^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv|gitEnv|gitRepositoryEnv))\.ts$/,
18 /^apps\/mobile\/deps\//,
19 ];
20
apps/desktop/vite.config.ts
+5 -1
@@ -177,6 +177,7 @@ export default defineConfig({
177 setupFiles: [
178 "../../packages/shared/src/testing/longTempDir.ts",
179 "../../packages/shared/src/testing/providerEnv.ts",
180 + "../../packages/shared/src/testing/gitEnv.ts",
181 ],
182 },
183 },
@@ -187,7 +188,10 @@ export default defineConfig({
188 include: domTests,
189 // Guest preloads run in the page; these tests drive them against a DOM.
190 environment: "jsdom",
190 - setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
191 + setupFiles: [
192 + "../../packages/shared/src/testing/providerEnv.ts",
193 + "../../packages/shared/src/testing/gitEnv.ts",
194 + ],
195 },
196 },
197 ],
apps/mobile/vite.config.ts
+5 -1
@@ -116,6 +116,7 @@ export default defineConfig({
116 setupFiles: [
117 "../../packages/shared/src/testing/longTempDir.ts",
118 "../../packages/shared/src/testing/providerEnv.ts",
119 + "../../packages/shared/src/testing/gitEnv.ts",
120 ],
121 },
122 },
@@ -164,7 +165,10 @@ export default defineConfig({
165 include: reactTests,
166 // Native components mock React Native and render through react-dom in these tests.
167 environment: "jsdom",
167 - setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
168 + setupFiles: [
169 + "../../packages/shared/src/testing/providerEnv.ts",
170 + "../../packages/shared/src/testing/gitEnv.ts",
171 + ],
172 },
173 },
174 ],
apps/web/vite.config.ts
+1
@@ -96,6 +96,7 @@ const unitTestProject = {
96 setupFiles: [
97 "../../packages/shared/src/testing/longTempDir.ts",
98 "../../packages/shared/src/testing/providerEnv.ts",
99 + "../../packages/shared/src/testing/gitEnv.ts",
100 ],
101 },
102 } satisfies TestProjectInlineConfiguration;
packages/client-runtime/vite.config.ts
+5 -1
@@ -5,6 +5,10 @@ export default defineConfig({
5 test: {
6 environment: "node",
7 include: ["src/**/*.test.ts"],
8 - setupFiles: ["../shared/src/testing/longTempDir.ts", "../shared/src/testing/providerEnv.ts"],
8 + setupFiles: [
9 + "../shared/src/testing/longTempDir.ts",
10 + "../shared/src/testing/providerEnv.ts",
11 + "../shared/src/testing/gitEnv.ts",
12 + ],
13 },
14 });
packages/provider-testing/package.json
+4 -1
@@ -25,6 +25,7 @@
25 }
26 },
27 "scripts": {
28 + "test": "vp test run --config ../../vite.config.ts --dir .",
29 "typecheck": "effect-tsgo tsrs --noEmit"
30 },
31 "dependencies": {
@@ -35,6 +36,8 @@
36 "effect": "catalog:"
37 },
38 "devDependencies": {
38 - "@types/node": "catalog:"
39 + "@effect/vitest": "catalog:",
40 + "@types/node": "catalog:",
41 + "vite-plus": "catalog:"
42 }
43 }
packages/provider-testing/src/replayWorkspace.test.ts new
+83
@@ -0,0 +1,83 @@
1 +import * as NodeServices from "@effect/platform-node/NodeServices";
2 +import { assert, it } from "@effect/vitest";
3 +import * as Effect from "effect/Effect";
4 +import * as FileSystem from "effect/FileSystem";
5 +import * as Path from "effect/Path";
6 +import * as ChildProcess from "effect/process/ChildProcess";
7 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
8 +import { vi } from "vite-plus/test";
9 +
10 +import { checkpointWorkspace } from "./replayWorkspace.ts";
11 +
12 +const git = (cwd: string, args: ReadonlyArray<string>) =>
13 + Effect.gen(function* () {
14 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
15 + return yield* spawner.string(ChildProcess.make("git", args, { cwd }));
16 + });
17 +
18 +const repositoryState = (repo: string, worktree: string, worktreeIndex: string) =>
19 + Effect.gen(function* () {
20 + return {
21 + config: yield* git(repo, ["config", "--local", "--list"]),
22 + refs: yield* git(repo, ["for-each-ref"]),
23 + worktrees: yield* git(repo, ["worktree", "list", "--porcelain"]),
24 + worktreeHead: yield* git(worktree, ["rev-parse", "HEAD"]),
25 + worktreeIndex: yield* git(repo, ["hash-object", "--no-filters", worktreeIndex]),
26 + };
27 + });
28 +
29 +it.layer(NodeServices.layer)("checkpointWorkspace", (it) => {
30 + // `git bisect run` and `git rebase --exec` export GIT_DIR to what they run, and hooks add
31 + // GIT_INDEX_FILE. In a linked worktree both are absolute paths into the shared repository.
32 + it.effect.each([
33 + { runner: "git bisect run", selectors: ["GIT_DIR"] },
34 + { runner: "a git hook", selectors: ["GIT_DIR", "GIT_INDEX_FILE"] },
35 + ])("keeps to its own repository when run under $runner", ({ selectors }) =>
36 + Effect.gen(function* () {
37 + yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
38 + // These commits use the contributor's global config, which must not require a signing key.
39 + vi.stubEnv("GIT_CONFIG_COUNT", "1");
40 + vi.stubEnv("GIT_CONFIG_KEY_0", "commit.gpgsign");
41 + vi.stubEnv("GIT_CONFIG_VALUE_0", "false");
42 + const fs = yield* FileSystem.FileSystem;
43 + const path = yield* Path.Path;
44 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-replay-workspace-decoy-" });
45 + const repo = path.join(root, "repo");
46 + const worktree = path.join(root, "worktree");
47 + yield* fs.makeDirectory(repo);
48 + yield* git(repo, ["init"]);
49 + yield* git(repo, [
50 + "-c",
51 + "user.name=Decoy",
52 + "-c",
53 + "user.email=decoy@example.com",
54 + "commit",
55 + "--allow-empty",
56 + "-m",
57 + "decoy",
58 + ]);
59 + yield* git(repo, ["worktree", "add", "--detach", worktree]);
60 + const worktreeGitDir = (yield* git(worktree, ["rev-parse", "--absolute-git-dir"])).trim();
61 + const worktreeIndex = path.join(worktreeGitDir, "index");
62 + const before = yield* repositoryState(repo, worktree, worktreeIndex);
63 +
64 + const inherited: Record<string, string> = {
65 + GIT_DIR: worktreeGitDir,
66 + GIT_INDEX_FILE: worktreeIndex,
67 + };
68 + for (const name of selectors) vi.stubEnv(name, inherited[name]);
69 + const workspace = yield* checkpointWorkspace("decoy-guard", { "src/a.txt": "a\n" });
70 + vi.unstubAllEnvs();
71 +
72 + assert.deepStrictEqual(yield* repositoryState(repo, worktree, worktreeIndex), before);
73 + assert.strictEqual(
74 + yield* git(workspace, ["log", "--format=%s%x00%an", "-1"]),
75 + "initial\u0000T3 Code Test\n",
76 + );
77 + assert.strictEqual(
78 + (yield* git(workspace, ["ls-files"])).trim().split("\n").join(","),
79 + "README.md,src/a.txt",
80 + );
81 + }),
82 + );
83 +});
packages/provider-testing/src/replayWorkspace.ts
+6 -1
@@ -6,6 +6,8 @@ import * as PlatformError from "effect/PlatformError";
6 import * as Schema from "effect/Schema";
7 import * as ChildProcess from "effect/process/ChildProcess";
8 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9 +import * as HostProcess from "@t3tools/shared/HostProcess";
10 +import { unsetInheritedGitRepositoryEnv } from "@t3tools/shared/testing/gitRepositoryEnv";
11
12 class ReplayFixtureGitCommandError extends Schema.TaggedError<ReplayFixtureGitCommandError>()(
13 "ReplayFixtureGitCommandError",
@@ -29,7 +31,10 @@ function runGit(
31 > {
32 return Effect.gen(function* () {
33 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
32 - const exitCode = yield* spawner.exitCode(ChildProcess.make("git", args, { cwd }));
34 + const env = unsetInheritedGitRepositoryEnv(process.env, yield* HostProcess.Platform);
35 + const exitCode = yield* spawner.exitCode(
36 + ChildProcess.make("git", args, { cwd, env, extendEnv: true }),
37 + );
38 if (Number(exitCode) !== 0) {
39 return yield* new ReplayFixtureGitCommandError({
40 command: `git ${args.join(" ")}`,
packages/shared/package.json
+4
@@ -375,6 +375,10 @@
375 "types": "./src/HostProcess.ts",
376 "import": "./src/HostProcess.ts"
377 },
378 + "./testing/gitRepositoryEnv": {
379 + "types": "./src/testing/gitRepositoryEnv.ts",
380 + "import": "./src/testing/gitRepositoryEnv.ts"
381 + },
382 "./testing/longTempDir": {
383 "types": "./src/testing/longTempDir.ts",
384 "import": "./src/testing/longTempDir.ts"
packages/shared/src/testing/gitEnv.test.ts new
+27
@@ -0,0 +1,27 @@
1 +import { describe, expect, it, onTestFinished, vi } from "vite-plus/test";
2 +
3 +import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts";
4 +
5 +describe("git test setup", () => {
6 + it("starts tests without the repository selectors git exported to the run", async () => {
7 + onTestFinished(() => {
8 + vi.unstubAllEnvs();
9 + });
10 + vi.stubEnv("GIT_DIR", "/repo/.git/worktrees/wt");
11 + vi.stubEnv("GIT_INDEX_FILE", "/repo/.git/worktrees/wt/index");
12 + vi.stubEnv("GIT_EDITOR", "true");
13 +
14 + vi.resetModules();
15 + await import("./gitEnv.ts");
16 +
17 + expect(process.env).not.toHaveProperty("GIT_DIR");
18 + expect(process.env).not.toHaveProperty("GIT_INDEX_FILE");
19 + expect(process.env.GIT_EDITOR).toBe("true");
20 + });
21 +
22 + it("unsets a selector under the spelling Windows passed it in", () => {
23 + const inherited = { Git_Dir: "C:\\repo\\.git", GIT_EDITOR: "true", PATH: "C:\\bin" };
24 + expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "win32"))).toEqual(["Git_Dir"]);
25 + expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "linux"))).toEqual([]);
26 + });
27 +});
packages/shared/src/testing/gitEnv.ts new
+10
@@ -0,0 +1,10 @@
1 +import * as HostProcess from "../HostProcess.ts";
2 +import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts";
3 +
4 +// A test run started from `git bisect run`, `git rebase --exec` or a hook inherits GIT_DIR. Every git
5 +// command a test or the code under test starts would then write into that repository.
6 +for (const name of Object.keys(
7 + unsetInheritedGitRepositoryEnv(process.env, HostProcess.Platform.defaultValue()),
8 +)) {
9 + delete process.env[name];
10 +}
packages/shared/src/testing/gitRepositoryEnv.ts new
+24
@@ -0,0 +1,24 @@
1 +// Mirrors the server's set in apps/server/src/vcs/VcsProcess.ts, which shared cannot import.
2 +const REPOSITORY_SELECTORS = new Set([
3 + "GIT_DIR",
4 + "GIT_WORK_TREE",
5 + "GIT_COMMON_DIR",
6 + "GIT_INDEX_FILE",
7 + "GIT_OBJECT_DIRECTORY",
8 + "GIT_ALTERNATE_OBJECT_DIRECTORIES",
9 +]);
10 +
11 +/**
12 + * `git bisect run`, `git rebase --exec` and hooks export GIT_DIR (hooks also GIT_INDEX_FILE) to what
13 + * they run, so a fixture's git commands would work on that repository instead of the one in their
14 + * cwd. Spread the result into a child's env to unset them under the spelling they were inherited as.
15 + */
16 +export const unsetInheritedGitRepositoryEnv = (
17 + inherited: NodeJS.ProcessEnv,
18 + platform: NodeJS.Platform,
19 +): Record<string, undefined> =>
20 + Object.fromEntries(
21 + Object.keys(inherited)
22 + .filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name))
23 + .map((name) => [name, undefined]),
24 + );
pnpm-lock.yaml
+6
@@ -2060,9 +2060,15 @@ importers:
2060 specifier: 4.1.3
2061 version: 4.1.3
2062 devDependencies:
2063 + '@effect/vitest':
2064 + specifier: 4.1.2
2065 + version: 4.1.2(patch_hash=299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7)(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(effect@4.1.3)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.64.1)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
2066 '@types/node':
2067 specifier: 24.12.4
2068 version: 24.12.4
2069 + vite-plus:
2070 + specifier: 1.4.0
2071 + version: 1.4.0(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.64.1)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
2072
2073 packages/secret-provider-core:
2074 dependencies:
scripts/check-changed.test.ts
+2
@@ -228,6 +228,8 @@ describe("check:changed", () => {
228 it.each([
229 "packages/shared/src/testing/longTempDir.ts",
230 "packages/shared/src/testing/providerEnv.ts",
231 + "packages/shared/src/testing/gitEnv.ts",
232 + "packages/shared/src/testing/gitRepositoryEnv.ts",
233 ])("tests every package when the shared test setup file %s changes", (file) => {
234 assert.strictEqual(decide([file], readWorkspace(), false).outputs.test_packages, "");
235 });
vite.config.ts
+1
@@ -149,6 +149,7 @@ export default defineConfig({
149 NodeURL.fileURLToPath(
150 new URL("./packages/shared/src/testing/providerEnv.ts", import.meta.url),
151 ),
152 + NodeURL.fileURLToPath(new URL("./packages/shared/src/testing/gitEnv.ts", import.meta.url)),
153 ],
154 ...(isRepoRootTestRun
155 ? {