diff --git a/.github/scripts/ci-changes.cjs b/.github/scripts/ci-changes.cjs index f9c8174f854f2..ae238772e7300 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|gitEnv|gitRepositoryEnv))\.ts$/, + /^packages\/shared\/src\/(HostProcess|gitEnvironment|testing\/(longTempDir|providerEnv|gitEnv))\.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/src/replayWorkspace.ts b/packages/provider-testing/src/replayWorkspace.ts index 1b60b685c788a..c899aac2b9679 100644 --- a/packages/provider-testing/src/replayWorkspace.ts +++ b/packages/provider-testing/src/replayWorkspace.ts @@ -7,7 +7,7 @@ import * as Schema from "effect/Schema"; import * as ChildProcess from "effect/process/ChildProcess"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as HostProcess from "@t3tools/shared/HostProcess"; -import { unsetInheritedGitRepositoryEnv } from "@t3tools/shared/testing/gitRepositoryEnv"; +import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment"; class ReplayFixtureGitCommandError extends Schema.TaggedError()( "ReplayFixtureGitCommandError", @@ -31,7 +31,7 @@ function runGit( > { return Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; - const env = unsetInheritedGitRepositoryEnv(process.env, yield* HostProcess.Platform); + const env = unsetInheritedRepositoryEnv(process.env, yield* HostProcess.Platform); const exitCode = yield* spawner.exitCode( ChildProcess.make("git", args, { cwd, env, extendEnv: true }), ); diff --git a/packages/shared/package.json b/packages/shared/package.json index 68a0b589f8809..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,9 +379,9 @@ "types": "./src/HostProcess.ts", "import": "./src/HostProcess.ts" }, - "./testing/gitRepositoryEnv": { - "types": "./src/testing/gitRepositoryEnv.ts", - "import": "./src/testing/gitRepositoryEnv.ts" + "./testing/gitEnv": { + "types": "./src/testing/gitEnv.ts", + "import": "./src/testing/gitEnv.ts" }, "./testing/longTempDir": { "types": "./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.test.ts b/packages/shared/src/testing/gitEnv.test.ts index 370a5147279f4..caab899775b32 100644 --- a/packages/shared/src/testing/gitEnv.test.ts +++ b/packages/shared/src/testing/gitEnv.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, onTestFinished, vi } from "vite-plus/test"; -import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts"; +import { unsetInheritedRepositoryEnv } from "../gitEnvironment.ts"; describe("git test setup", () => { it("starts tests without the repository selectors git exported to the run", async () => { @@ -21,7 +21,7 @@ describe("git test setup", () => { it("unsets a selector under the spelling Windows passed it in", () => { const inherited = { Git_Dir: "C:\\repo\\.git", GIT_EDITOR: "true", PATH: "C:\\bin" }; - expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "win32"))).toEqual(["Git_Dir"]); - expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "linux"))).toEqual([]); + expect(Object.keys(unsetInheritedRepositoryEnv(inherited, "win32"))).toEqual(["Git_Dir"]); + expect(Object.keys(unsetInheritedRepositoryEnv(inherited, "linux"))).toEqual([]); }); }); diff --git a/packages/shared/src/testing/gitEnv.ts b/packages/shared/src/testing/gitEnv.ts index 9fa63ab9312f2..b182ea571bd36 100644 --- a/packages/shared/src/testing/gitEnv.ts +++ b/packages/shared/src/testing/gitEnv.ts @@ -1,10 +1,10 @@ import * as HostProcess from "../HostProcess.ts"; -import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts"; +import { unsetInheritedRepositoryEnv } from "../gitEnvironment.ts"; // A test run started from `git bisect run`, `git rebase --exec` or a hook inherits GIT_DIR. Every git // command a test or the code under test starts would then write into that repository. for (const name of Object.keys( - unsetInheritedGitRepositoryEnv(process.env, HostProcess.Platform.defaultValue()), + unsetInheritedRepositoryEnv(process.env, HostProcess.Platform.defaultValue()), )) { delete process.env[name]; } diff --git a/packages/shared/src/testing/gitRepositoryEnv.ts b/packages/shared/src/testing/gitRepositoryEnv.ts deleted file mode 100644 index 630f81d9ebb4b..0000000000000 --- a/packages/shared/src/testing/gitRepositoryEnv.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Mirrors the server's set in apps/server/src/vcs/VcsProcess.ts, which shared cannot import. -const REPOSITORY_SELECTORS = new Set([ - "GIT_DIR", - "GIT_WORK_TREE", - "GIT_COMMON_DIR", - "GIT_INDEX_FILE", - "GIT_OBJECT_DIRECTORY", - "GIT_ALTERNATE_OBJECT_DIRECTORIES", -]); - -/** - * `git bisect run`, `git rebase --exec` and hooks export GIT_DIR (hooks also GIT_INDEX_FILE) to what - * they run, so a fixture's git commands would work on that repository instead of the one in their - * cwd. Spread the result into a child's env to unset them under the spelling they were inherited as. - */ -export const unsetInheritedGitRepositoryEnv = ( - inherited: NodeJS.ProcessEnv, - platform: NodeJS.Platform, -): Record => - Object.fromEntries( - Object.keys(inherited) - .filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name)) - .map((name) => [name, undefined]), - ); diff --git a/scripts/check-changed.test.ts b/scripts/check-changed.test.ts index 5874eef8de515..31530c5320f35 100644 --- a/scripts/check-changed.test.ts +++ b/scripts/check-changed.test.ts @@ -226,10 +226,10 @@ 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", - "packages/shared/src/testing/gitEnv.ts", - "packages/shared/src/testing/gitRepositoryEnv.ts", ])("tests every package when the shared test setup file %s changes", (file) => { assert.strictEqual(decide([file], readWorkspace(), false).outputs.test_packages, ""); }); 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";