diff --git a/src/agent/environment.ts b/src/agent/environment.ts index 90a60c8..9cc357b 100644 --- a/src/agent/environment.ts +++ b/src/agent/environment.ts @@ -1,4 +1,3 @@ -import { execFile } from "node:child_process"; import { createHash } from "node:crypto"; import { existsSync, readFileSync } from "node:fs"; import { mkdir, readdir, readFile, rename, rm, stat, utimes, writeFile } from "node:fs/promises"; @@ -6,6 +5,7 @@ import { createRequire } from "node:module"; import { homedir } from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; +import { GitRunner } from "../git-runner"; import { type LayerMapAnalyzers, mapDatabasePath } from "../layermap"; import { executableName } from "../platform"; import { discoverJavaRuntime } from "../project-map-process"; @@ -41,22 +41,19 @@ export const cacheDirectory = (env: NodeJS.ProcessEnv = process.env): string => * The project an agent works in: the Git work tree around the directory. Maps are built from * what Git lists, so a directory outside any work tree has none. */ -export const projectDirectory = ( +export const projectDirectory = async ( directory: string, -): Promise> => - new Promise((resolve) => - execFile( - "git", - ["rev-parse", "--show-toplevel"], - { cwd: directory, timeout: 10_000 }, - (error, stdout) => - resolve( - error - ? { directory: path.resolve(directory), git: false } - : { directory: stdout.trim(), git: true }, - ), - ), - ); +): Promise> => { + try { + const { stdout } = await new GitRunner().run(["rev-parse", "--show-toplevel"], { + cwd: directory, + timeoutMs: 10_000, + }); + return { directory: stdout.toString().trim(), git: true }; + } catch { + return { directory: path.resolve(directory), git: false }; + } +}; type StoredAnalyzers = { [K in keyof LayerMapAnalyzers]: Record }; const relativeTo = (directory: string, value: StoredAnalyzers): StoredAnalyzers => diff --git a/test/mcp.test.ts b/test/mcp.test.ts index 84d3c1c..7178d65 100644 --- a/test/mcp.test.ts +++ b/test/mcp.test.ts @@ -7,6 +7,7 @@ import { PassThrough } from "node:stream"; import { test } from "node:test"; import { pathToFileURL } from "node:url"; import { promisify } from "node:util"; +import { projectDirectory } from "../src/agent/environment"; import { serveMcp } from "../src/agent/mcp"; import type { SessionStart } from "../src/agent/projects"; import type { LayerMap } from "../src/layermap"; @@ -392,3 +393,16 @@ test("a session works where its client's roots say, not where the client started await client.close(); } }); + +test("a session's project is found by LayerMap's Git, whatever Git the caller's environment names", async (t) => { + const { root } = await project({ "src/a.ts": "export const a = 1;\n" }); + const outside = await sandbox(); + const directory = await gitRoot(root); + process.env.GIT_DIR = path.join(root, "missing"); + t.after(() => { + delete process.env.GIT_DIR; + }); + + assert.deepEqual(await projectDirectory(path.join(root, "src")), { directory, git: true }); + assert.deepEqual(await projectDirectory(outside), { directory: outside, git: false }); +});