feat(the-system): resolve governed project roots
fec1b02d085291ca9af82edba8de64eddcf21ae9
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: f088c7a0c2d1
bugabingas pi extensions
fec1b02d085291ca9af82edba8de64eddcf21ae9
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+133 / −0 text lines
new file mode 100644+88 / −0 text lines
import assert from "node:assert/strict";import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import path from "node:path";import { afterEach, describe, it } from "vitest";import { discoverProjectRoot, resolveGovernedPath, SystemDocumentError,} from "../documents.ts";const temporary: string[] = [];const tree = () => { const root = mkdtempSync(path.join(tmpdir(), "the-system-documents-")); temporary.push(root); return root;};const dir = (...parts: string[]) => { const result = path.join(...parts); mkdirSync(result, { recursive: true }); return result;};afterEach(() => { for (const root of temporary.splice(0)) rmSync(root, { recursive: true, force: true });});describe("project root", () => { it.each([ ["simple Git", [".git"], "directory"], ["Git worktree", [".git"], "file"], ["JJ workspace", [".jj"], "directory"], ["colocated JJ/Git", [".jj", ".git"], "directory"], ] as const)("resolves %s", (_name, markers, markerKind) => { const root = tree(); const cwd = dir(root, "packages", "app"); for (const marker of markers) { const target = path.join(root, marker); if (markerKind === "file" && marker === ".git") writeFileSync(target, "gitdir: ../shared\n"); else mkdirSync(target); } assert.equal(discoverProjectRoot(cwd), root); }); it("uses cwd when no VCS marker exists", () => { const cwd = dir(tree(), "plain"); assert.equal(discoverProjectRoot(cwd), path.resolve(cwd)); }); it("fails when nearest JJ and Git roots differ", () => { const gitRoot = tree(); mkdirSync(path.join(gitRoot, ".git")); const jjRoot = dir(gitRoot, "nested"); mkdirSync(path.join(jjRoot, ".jj")); const cwd = dir(jjRoot, "src"); assert.throws( () => discoverProjectRoot(cwd), (error: unknown) => error instanceof SystemDocumentError && error.code === "root.conflict" && error.message === `Conflicting project roots: Git ${gitRoot}; JJ ${jjRoot}`, ); });});describe("governed path containment", () => { it("normalizes a contained relative path", () => { const root = path.resolve("project"); assert.equal( resolveGovernedPath(root, ".system/specs/../plans/a.html"), path.join(root, ".system", "plans", "a.html"), ); }); it.each(["../outside", path.resolve("outside")])("rejects %s", (candidate) => { assert.throws( () => resolveGovernedPath(path.resolve("project"), candidate), (error: unknown) => error instanceof SystemDocumentError && error.code === "path.outside-root", ); }); it("does not confuse a sibling sharing the root prefix for a child", () => { const root = path.resolve("project"); assert.throws( () => resolveGovernedPath(root, path.resolve("project-other/file")), /outside project root/, ); });});new file mode 100644+45 / −0 text lines
import { existsSync } from "node:fs";import path from "node:path";export const SCHEMA_VERSION = 1;export const STYLE_VERSION = 1;export class SystemDocumentError extends Error { constructor( readonly code: string, message: string, ) { super(message); this.name = "SystemDocumentError"; }}const nearestMarker = (cwd: string, marker: string, markerExists: (path: string) => boolean) => { let current = path.resolve(cwd); while (true) { if (markerExists(path.join(current, marker))) return current; const parent = path.dirname(current); if (parent === current) return undefined; current = parent; }};export function discoverProjectRoot(cwd: string, markerExists = existsSync): string { const resolvedCwd = path.resolve(cwd); const jj = nearestMarker(resolvedCwd, ".jj", markerExists); const git = nearestMarker(resolvedCwd, ".git", markerExists); if (jj && git && jj !== git) { throw new SystemDocumentError("root.conflict", `Conflicting project roots: Git ${git}; JJ ${jj}`); } return jj ?? git ?? resolvedCwd;}export function resolveGovernedPath(root: string, candidate: string): string { const normalizedRoot = path.resolve(root); const target = path.resolve(normalizedRoot, candidate); const relative = path.relative(normalizedRoot, target); if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) { throw new SystemDocumentError("path.outside-root", `Path is outside project root: ${candidate}`); } return target;}