Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/agent/environment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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";
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";
Expand Down Expand Up @@ -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<Readonly<{ directory: string; git: boolean }>> =>
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<Readonly<{ directory: string; git: boolean }>> => {
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<string, unknown> };
const relativeTo = (directory: string, value: StoredAnalyzers): StoredAnalyzers =>
Expand Down
14 changes: 14 additions & 0 deletions test/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
});
Loading