From 9f3b1583a7dc8b70065a891b64d035dc3903ec84 Mon Sep 17 00:00:00 2001 From: Prince Jain Date: Sun, 8 Nov 2026 21:32:06 +0530 Subject: [PATCH] fix(cli): resolve workspaceDir and origin remoteUrl via git in worktrees --- packages/cli-v3/src/config.test.ts | 19 +++ packages/cli-v3/src/config.ts | 3 +- packages/cli-v3/src/utilities/gitMeta.test.ts | 119 +++++++++++++++++- packages/cli-v3/src/utilities/gitMeta.ts | 52 +++++++- 4 files changed, 186 insertions(+), 7 deletions(-) diff --git a/packages/cli-v3/src/config.test.ts b/packages/cli-v3/src/config.test.ts index 12e5451bc74..7b85ca1717f 100644 --- a/packages/cli-v3/src/config.test.ts +++ b/packages/cli-v3/src/config.test.ts @@ -1,3 +1,4 @@ +import { execFileSync } from "node:child_process"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -89,3 +90,21 @@ describe("loadConfig bun runtime warnings", () => { expect(await outputWhileLoading("bun-1.4")).not.toMatch(/deprecated/i); }); }); + +describe("loadConfig workspaceDir", () => { + it("resolves workspaceDir to a linked git worktree nested inside another checkout", async () => { + const repoDir = await createProject(); + + execFileSync("git", ["init", "-b", "main"], { cwd: repoDir }); + execFileSync("git", ["config", "user.name", "Test User"], { cwd: repoDir }); + execFileSync("git", ["config", "user.email", "test@example.com"], { cwd: repoDir }); + execFileSync("git", ["add", "."], { cwd: repoDir }); + execFileSync("git", ["commit", "-m", "initial commit"], { cwd: repoDir }); + + const worktreeDir = join(repoDir, ".worktrees", "release"); + execFileSync("git", ["worktree", "add", "-b", "release", worktreeDir], { cwd: repoDir }); + + const config = await loadConfig({ cwd: worktreeDir, warn: false }); + expect(config.workspaceDir).toBe(worktreeDir); + }); +}); diff --git a/packages/cli-v3/src/config.ts b/packages/cli-v3/src/config.ts index d65c3c8e7a3..559fab1d936 100644 --- a/packages/cli-v3/src/config.ts +++ b/packages/cli-v3/src/config.ts @@ -22,6 +22,7 @@ import { readdir } from "node:fs/promises"; import { basename, dirname, isAbsolute, join, relative } from "node:path"; import { findWorkspaceDir, resolveLockfile, resolvePackageJSON, resolveTSConfig } from "pkg-types"; import { generateCode, loadFile } from "./imports/magicast.js"; +import { getGitTopLevel } from "./utilities/gitMeta.js"; import { logger } from "./utilities/logger.js"; import { additionalFiles, @@ -203,7 +204,7 @@ async function resolveConfig( const packageJsonPath = await resolvePackageJSON(cwd); const tsconfigPath = await safeResolveTsConfig(cwd); const lockfilePath = await resolveLockfile(cwd); - const workspaceDir = await findWorkspaceDir(cwd); + const workspaceDir = (await getGitTopLevel(cwd)) ?? (await findWorkspaceDir(cwd)); const workingDir = result.configFile ? dirname(result.configFile) diff --git a/packages/cli-v3/src/utilities/gitMeta.test.ts b/packages/cli-v3/src/utilities/gitMeta.test.ts index 5e1bb5651c3..c934ccd8f55 100644 --- a/packages/cli-v3/src/utilities/gitMeta.test.ts +++ b/packages/cli-v3/src/utilities/gitMeta.test.ts @@ -1,5 +1,20 @@ -import { describe, expect, it } from "vitest"; -import { getCiBranch, sanitizeGitRemoteUrl } from "./gitMeta.js"; +import { execFileSync } from "node:child_process"; +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { createGitMeta, getCiBranch, getGitTopLevel, sanitizeGitRemoteUrl } from "./gitMeta.js"; + +const tempDirs: string[] = []; + +afterEach(async () => { + vi.unstubAllEnvs(); + await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true }))); +}); + +function runGit(cwd: string, args: string[]): string { + return execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); +} describe("sanitizeGitRemoteUrl", () => { it.each([ @@ -87,3 +102,103 @@ describe("getCiBranch", () => { ).toBe("feature/login"); }); }); + +describe("git worktree support", () => { + async function createRepoWithWorktrees() { + const baseDir = await mkdtemp(join(tmpdir(), "trigger-git-worktree-")); + tempDirs.push(baseDir); + + const repoDir = join(baseDir, "repo"); + await mkdir(repoDir, { recursive: true }); + + runGit(repoDir, ["init", "-b", "main"]); + runGit(repoDir, ["config", "user.name", "Test User"]); + runGit(repoDir, ["config", "user.email", "test@example.com"]); + runGit(repoDir, [ + "remote", + "add", + "origin", + "https://token@example.com/triggerdotdev/trigger.dev.git", + ]); + + await writeFile(join(repoDir, ".gitignore"), ".worktrees/\n"); + await writeFile(join(repoDir, "index.ts"), "export const version = 1;\n"); + runGit(repoDir, ["add", "."]); + runGit(repoDir, ["commit", "-m", "initial main commit"]); + const mainSha = runGit(repoDir, ["rev-parse", "HEAD"]); + + const externalWorktreeDir = join(baseDir, "worktree-external"); + runGit(repoDir, ["worktree", "add", "-b", "release/external", externalWorktreeDir]); + await writeFile(join(externalWorktreeDir, "index.ts"), "export const version = 2;\n"); + runGit(externalWorktreeDir, ["add", "index.ts"]); + runGit(externalWorktreeDir, ["commit", "-m", "external worktree commit"]); + const externalSha = runGit(externalWorktreeDir, ["rev-parse", "HEAD"]); + + const nestedWorktreeDir = join(repoDir, ".worktrees", "release"); + runGit(repoDir, ["worktree", "add", "-b", "release/nested", nestedWorktreeDir]); + await writeFile(join(nestedWorktreeDir, "index.ts"), "export const version = 3;\n"); + runGit(nestedWorktreeDir, ["add", "index.ts"]); + runGit(nestedWorktreeDir, ["commit", "-m", "nested worktree commit"]); + const nestedSha = runGit(nestedWorktreeDir, ["rev-parse", "HEAD"]); + + // Make the outer checkout dirty to verify nested worktrees do not inherit its status + await writeFile(join(repoDir, "index.ts"), "export const version = 99;\n"); + + return { + repoDir, + mainSha, + externalWorktreeDir, + externalSha, + nestedWorktreeDir, + nestedSha, + }; + } + + it("reads sanitized origin remoteUrl and commit metadata from an external linked worktree", async () => { + vi.stubEnv("GITHUB_ACTIONS", ""); + vi.stubEnv("TRIGGER_GITHUB_APP", ""); + + const { externalWorktreeDir, externalSha } = await createRepoWithWorktrees(); + + await expect(getGitTopLevel(externalWorktreeDir)).resolves.toBe(externalWorktreeDir); + await expect(createGitMeta(externalWorktreeDir)).resolves.toEqual({ + source: "local", + remoteUrl: "https://example.com/triggerdotdev/trigger.dev.git", + commitAuthorName: "Test User", + commitMessage: "external worktree commit", + commitRef: "release/external", + commitSha: externalSha, + dirty: false, + }); + }); + + it("resolves nested worktree root and metadata instead of the outer checkout", async () => { + vi.stubEnv("GITHUB_ACTIONS", ""); + vi.stubEnv("TRIGGER_GITHUB_APP", ""); + + const { repoDir, mainSha, nestedWorktreeDir, nestedSha } = await createRepoWithWorktrees(); + const nestedSubdir = join(nestedWorktreeDir, "packages", "app"); + await mkdir(nestedSubdir, { recursive: true }); + + await expect(getGitTopLevel(nestedWorktreeDir)).resolves.toBe(nestedWorktreeDir); + await expect(getGitTopLevel(nestedSubdir)).resolves.toBe(nestedWorktreeDir); + + const outerMeta = await createGitMeta(repoDir); + expect(outerMeta).toMatchObject({ + commitRef: "main", + commitSha: mainSha, + dirty: true, + }); + + const nestedMeta = await createGitMeta(nestedWorktreeDir); + expect(nestedMeta).toEqual({ + source: "local", + remoteUrl: "https://example.com/triggerdotdev/trigger.dev.git", + commitAuthorName: "Test User", + commitMessage: "nested worktree commit", + commitRef: "release/nested", + commitSha: nestedSha, + dirty: false, + }); + }); +}); diff --git a/packages/cli-v3/src/utilities/gitMeta.ts b/packages/cli-v3/src/utilities/gitMeta.ts index 37eeaea3afd..12f151b6221 100644 --- a/packages/cli-v3/src/utilities/gitMeta.ts +++ b/packages/cli-v3/src/utilities/gitMeta.ts @@ -1,5 +1,5 @@ import fs from "fs/promises"; -import { join } from "path"; +import { join, resolve } from "path"; import ini from "ini"; import git from "git-last-commit"; import { x } from "tinyexec"; @@ -15,7 +15,7 @@ export async function createGitMeta(directory: string): Promise { } } +export async function getGitTopLevel(directory: string): Promise { + try { + const result = await x( + "git", + ["--no-optional-locks", "rev-parse", "--show-toplevel", "--show-cdup"], + { + nodeOptions: { + cwd: directory, + }, + } + ); + + if (result.exitCode !== 0) { + return undefined; + } + + const [topLevel, cdup = ""] = result.stdout.trimEnd().split(/\r?\n/); + if (!topLevel) { + return undefined; + } + + return resolve(directory, cdup || "."); + } catch { + return undefined; + } +} + async function parseGitConfig(configPath: string) { try { return ini.parse(await fs.readFile(configPath, "utf8")); @@ -138,8 +165,25 @@ function pluckOriginUrl(gitConfig: { [key: string]: any }): string | undefined { return gitConfig['remote "origin"']?.url; } -async function getOriginUrl(configPath: string): Promise { - const gitConfig = await parseGitConfig(configPath); +async function getOriginUrl(directory: string): Promise { + try { + const result = await x("git", ["--no-optional-locks", "config", "--get", "remote.origin.url"], { + nodeOptions: { + cwd: directory, + }, + }); + + if (result.exitCode === 0) { + const originUrl = result.stdout.trim(); + if (originUrl) { + return originUrl; + } + } + } catch { + // Fall back to parsing .git/config if invoking git fails + } + + const gitConfig = await parseGitConfig(join(directory, ".git/config")); if (!gitConfig) { return null; }