repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

feat(the-system): resolve governed project roots

fec1b02d085291ca9af82edba8de64eddcf21ae9

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+133 / −0 text lines

Diff layout

extensions/the-system/__tests__/documents.test.ts

Added
new file mode 100644

+88 / −0 text lines

1import assert from "node:assert/strict";
2import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3import { tmpdir } from "node:os";
4import path from "node:path";
5import { afterEach, describe, it } from "vitest";
6import {
7 discoverProjectRoot,
8 resolveGovernedPath,
9 SystemDocumentError,
10} from "../documents.ts";
12const temporary: string[] = [];
13const tree = () => {
14 const root = mkdtempSync(path.join(tmpdir(), "the-system-documents-"));
15 temporary.push(root);
16 return root;
17};
18const dir = (...parts: string[]) => {
19 const result = path.join(...parts);
20 mkdirSync(result, { recursive: true });
21 return result;
22};
23afterEach(() => {
24 for (const root of temporary.splice(0)) rmSync(root, { recursive: true, force: true });
25});
27describe("project root", () => {
28 it.each([
29 ["simple Git", [".git"], "directory"],
30 ["Git worktree", [".git"], "file"],
31 ["JJ workspace", [".jj"], "directory"],
32 ["colocated JJ/Git", [".jj", ".git"], "directory"],
33 ] as const)("resolves %s", (_name, markers, markerKind) => {
34 const root = tree();
35 const cwd = dir(root, "packages", "app");
36 for (const marker of markers) {
37 const target = path.join(root, marker);
38 if (markerKind === "file" && marker === ".git") writeFileSync(target, "gitdir: ../shared\n");
39 else mkdirSync(target);
40 }
41 assert.equal(discoverProjectRoot(cwd), root);
42 });
44 it("uses cwd when no VCS marker exists", () => {
45 const cwd = dir(tree(), "plain");
46 assert.equal(discoverProjectRoot(cwd), path.resolve(cwd));
47 });
49 it("fails when nearest JJ and Git roots differ", () => {
50 const gitRoot = tree();
51 mkdirSync(path.join(gitRoot, ".git"));
52 const jjRoot = dir(gitRoot, "nested");
53 mkdirSync(path.join(jjRoot, ".jj"));
54 const cwd = dir(jjRoot, "src");
55 assert.throws(
56 () => discoverProjectRoot(cwd),
57 (error: unknown) =>
58 error instanceof SystemDocumentError &&
59 error.code === "root.conflict" &&
60 error.message === `Conflicting project roots: Git ${gitRoot}; JJ ${jjRoot}`,
61 );
62 });
63});
65describe("governed path containment", () => {
66 it("normalizes a contained relative path", () => {
67 const root = path.resolve("project");
68 assert.equal(
69 resolveGovernedPath(root, ".system/specs/../plans/a.html"),
70 path.join(root, ".system", "plans", "a.html"),
71 );
72 });
74 it.each(["../outside", path.resolve("outside")])("rejects %s", (candidate) => {
75 assert.throws(
76 () => resolveGovernedPath(path.resolve("project"), candidate),
77 (error: unknown) => error instanceof SystemDocumentError && error.code === "path.outside-root",
78 );
79 });
81 it("does not confuse a sibling sharing the root prefix for a child", () => {
82 const root = path.resolve("project");
83 assert.throws(
84 () => resolveGovernedPath(root, path.resolve("project-other/file")),
85 /outside project root/,
86 );
87 });
88});

extensions/the-system/documents.ts

Added
new file mode 100644

+45 / −0 text lines

1import { existsSync } from "node:fs";
2import path from "node:path";
3
4export const SCHEMA_VERSION = 1;
5export const STYLE_VERSION = 1;
6
7export class SystemDocumentError extends Error {
8 constructor(
9 readonly code: string,
10 message: string,
11 ) {
12 super(message);
13 this.name = "SystemDocumentError";
14 }
15}
17const nearestMarker = (cwd: string, marker: string, markerExists: (path: string) => boolean) => {
18 let current = path.resolve(cwd);
19 while (true) {
20 if (markerExists(path.join(current, marker))) return current;
21 const parent = path.dirname(current);
22 if (parent === current) return undefined;
23 current = parent;
24 }
25};
27export function discoverProjectRoot(cwd: string, markerExists = existsSync): string {
28 const resolvedCwd = path.resolve(cwd);
29 const jj = nearestMarker(resolvedCwd, ".jj", markerExists);
30 const git = nearestMarker(resolvedCwd, ".git", markerExists);
31 if (jj && git && jj !== git) {
32 throw new SystemDocumentError("root.conflict", `Conflicting project roots: Git ${git}; JJ ${jj}`);
33 }
34 return jj ?? git ?? resolvedCwd;
35}
37export function resolveGovernedPath(root: string, candidate: string): string {
38 const normalizedRoot = path.resolve(root);
39 const target = path.resolve(normalizedRoot, candidate);
40 const relative = path.relative(normalizedRoot, target);
41 if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
42 throw new SystemDocumentError("path.outside-root", `Path is outside project root: ${candidate}`);
43 }
44 return target;
45}