diff --git a/server/src/__tests__/version.test.ts b/server/src/__tests__/version.test.ts index c3e05f95e8e..97055406f03 100644 --- a/server/src/__tests__/version.test.ts +++ b/server/src/__tests__/version.test.ts @@ -1,6 +1,15 @@ -import { describe, expect, it, vi } from "vitest"; +import path from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { parseGitDescribeVersion, resolveServerVersion } from "../version.js"; +const gitDescribeProcess = vi.hoisted(() => vi.fn()); +vi.mock("node:child_process", () => ({ execFileSync: gitDescribeProcess })); + +afterEach(() => { + gitDescribeProcess.mockReset(); + vi.unstubAllEnvs(); +}); + describe("parseGitDescribeVersion", () => { it("reports drift from the nearest release tag as a PEP 440 local version", () => { expect(parseGitDescribeVersion("v2026.626.0-58-g518fc71ce\n")).toBe( @@ -30,6 +39,39 @@ describe("parseGitDescribeVersion", () => { }); describe("resolveServerVersion", () => { + it("uses the build stamp without spawning git outside a checkout", () => { + vi.stubEnv("GIT_DIR", ""); + gitDescribeProcess.mockClear(); + + expect(resolveServerVersion({ + packageRoot: path.resolve("unpacked", "server"), + pathExists: () => false, + realpath: (value) => value, + buildVersion: "v2026.1117.0-5-g012345678", + packageVersion: "0.3.1", + })).toBe("2026.1117.0+5.git.012345678"); + expect(gitDescribeProcess).not.toHaveBeenCalled(); + }); + + it.each(["checkout metadata", "GIT_DIR"])("uses live git when %s is present", (metadata) => { + const root = path.resolve("checkout", "server"); + vi.stubEnv("GIT_DIR", metadata === "GIT_DIR" ? "/custom/git-dir" : ""); + gitDescribeProcess.mockClear().mockReturnValue("v2026.1117.0-3-g012345678-dirty"); + + expect(resolveServerVersion({ + packageRoot: root, + pathExists: (value) => metadata === "checkout metadata" && value === path.join(root, "..", ".git"), + realpath: (value) => value, + buildVersion: "v2026.1117.0-0-g012345678", + packageVersion: "0.3.1", + })).toBe("2026.1117.0+3.git.012345678.dirty"); + expect(gitDescribeProcess).toHaveBeenCalledExactlyOnceWith( + "git", + ["describe", "--tags", "--match", "v*", "--long", "--dirty"], + expect.objectContaining({ cwd: root }), + ); + }); + it("returns the parsed git-derived version when git describe succeeds", () => { expect( resolveServerVersion({ diff --git a/server/src/version.ts b/server/src/version.ts index 6627637b029..18f03f1017d 100644 --- a/server/src/version.ts +++ b/server/src/version.ts @@ -27,12 +27,23 @@ function defaultDebugLog(fields: Record, message: string): void console.debug(message, fields); } -function defaultGitDescribeCommand(): string { +function defaultGitDescribeCommand( + root: string, + pathExists: PathExists = existsSync, + realpath: Realpath = realpathSync, +): string { + if ( + !process.env.GIT_DIR && + !hasGitMetadataBeforeNodeModulesBoundary(safeRealpath(root, realpath), pathExists) + ) { + throw new Error("The package root is not inside a Git checkout"); + } + return execFileSync( "git", ["describe", "--tags", "--match", "v*", "--long", "--dirty"], { - cwd: packageRoot, + cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 1500, @@ -160,7 +171,6 @@ export function resolveServerVersion( } = {}, ): string { const packageVersion = opts.packageVersion ?? pkg.version ?? "0.0.0"; - const gitDescribeCommand = opts.gitDescribeCommand ?? defaultGitDescribeCommand; const debugLog = opts.debugLog ?? defaultDebugLog; const resolvedPackageRoot = opts.packageRoot ?? packageRoot; @@ -178,7 +188,10 @@ export function resolveServerVersion( } try { - const parsedVersion = parseGitDescribeVersion(gitDescribeCommand()); + const output = opts.gitDescribeCommand + ? opts.gitDescribeCommand() + : defaultGitDescribeCommand(resolvedPackageRoot, opts.pathExists, opts.realpath); + const parsedVersion = parseGitDescribeVersion(output); if (parsedVersion) return parsedVersion; debugLog(