commit 5376032212bfd70e182d47e5eb99b90c376a2c4d
parent a6101da02f546d5ff6bbf20f51a4c1f5f5fef8f7
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 2 Nov 2026 13:37:25 -0500
umtool paths: a script standing in no checkout reads the corpus of the checkout its modules are in -- REPO_ROOT walked the cwd and fell back to the cwd's PARENT, so benchPrepPlan run from a project directory looked for windows in <folder>/transcripts/channels and planned every clip no-window (2 Nov, in-her-words); moduleRepoRoot comes before that fallback
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 74 insertions(+), 5 deletions(-)
diff --git a/umtool/lib/paths.mjs b/umtool/lib/paths.mjs
@@ -8,6 +8,7 @@ import { existsSync, realpathSync } from "node:fs";
import { lstat, realpath } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
+import { fileURLToPath } from "node:url";
import { SONG_DATA, SONG_REPORTS } from "../song/paths.mjs";
import { ARTICLES_KEY } from "./articles/key.mjs";
@@ -173,12 +174,14 @@ export function mediaMirror(abs, { reportsRoot = REPORTS_ROOT, mediaRoot = MEDIA
* The checkout this runs in: the nearest directory at or above `start` holding
* pnpm-workspace.yaml, or `start`'s parent when there is none.
*
- * Walked from the CWD, not from import.meta.url: the Next app imports this
+ * Walked from the CWD first, not from import.meta.url: the Next app imports this
* module (eleven API routes), and Turbopack rewrites module URLs into
* .next/server/chunks, so a path derived from one points at the build output
* -- the SONG_CODE rule in lib/paths.ts. `next dev`, `next start` and the CLI
* all run with cwd inside the checkout (the app at umtool/, which is what the
- * fallback assumes). report-to-video/cues.mjs keeps its import.meta.url walk:
+ * fallback assumes). A process standing in NO checkout -- a script run from a
+ * project directory -- gets the checkout this module is in (moduleRepoRoot),
+ * never the cwd's parent. report-to-video/cues.mjs keeps its import.meta.url walk:
* only its CLI entry points (build-video, resolve-windows) read that default.
*
* EVERY PATH OP ON A cwd-DERIVED VALUE CARRIES `turbopackIgnore`. Turbopack
@@ -193,16 +196,50 @@ export function mediaMirror(abs, { reportsRoot = REPORTS_ROOT, mediaRoot = MEDIA
* at run time are unchanged. scripts/next-build-trace.test.mjs holds the line.
*/
export function findRepoRoot(start) {
+ return findCheckout(start) ?? path.resolve(/* turbopackIgnore: true */ start, "..");
+}
+
+/** The nearest directory at or above `start` holding pnpm-workspace.yaml, or null. */
+export function findCheckout(start) {
let dir = path.resolve(/* turbopackIgnore: true */ start);
for (;;) {
if (existsSync(path.join(/* turbopackIgnore: true */ dir, "pnpm-workspace.yaml"))) return dir;
const up = path.dirname(/* turbopackIgnore: true */ dir);
- if (up === dir) return path.resolve(/* turbopackIgnore: true */ start, "..");
+ if (up === dir) return null;
dir = up;
}
}
/**
+ * The checkout THIS MODULE is in -- `<repo>/umtool/lib/paths.mjs` -- or null
+ * (a bundle, where import.meta.url is not the file on disk: the Next server's
+ * cwd walk answers there).
+ *
+ * The last word before the cwd's parent, for a process that is neither a
+ * umtool/bin entry nor standing in a checkout: a script that imports
+ * lib/report/benchPrep.mjs and runs from a project directory under
+ * REPORTS_DIR. Before this, the cwd walk found no pnpm-workspace.yaml there
+ * and fell back to the cwd's PARENT, so CHANNELS_DIR was
+ * `<folder>/transcripts/channels` -- nothing -- and every clip planned
+ * `no-window`; the same script run from umtool/ found each window (2 Nov,
+ * emberparadox/in-her-words). The answer must not depend on where the caller
+ * stands.
+ *
+ * @param {string} [url] the module URL (a test passes its own)
+ * @returns {string | null}
+ */
+export function moduleRepoRoot(url = import.meta.url) {
+ try {
+ if (!String(url).startsWith("file:")) return null;
+ const real = realpathSync(/* turbopackIgnore: true */ fileURLToPath(url));
+ const repo = path.resolve(/* turbopackIgnore: true */ path.dirname(/* turbopackIgnore: true */ real), "..", "..");
+ return existsSync(/* turbopackIgnore: true */ path.join(/* turbopackIgnore: true */ repo, "pnpm-workspace.yaml")) ? repo : null;
+ } catch {
+ return null;
+ }
+}
+
+/**
* The checkout a umtool CLI belongs to -- `<repo>/umtool/bin/<cli>.mjs` as the
* entry script (`process.argv[1]`) -- or null for anything else (the Next
* server, a test runner, report-to-video's own CLIs).
@@ -230,7 +267,11 @@ export function cliRepoRoot(entry) {
return existsSync(path.join(/* turbopackIgnore: true */ repo, "pnpm-workspace.yaml")) ? repo : null;
}
-export const REPO_ROOT = cliRepoRoot(process.argv[1]) ?? findRepoRoot(process.cwd());
+// The entry script's checkout (a umtool CLI), else the one the cwd is in (the
+// Next server, a test runner), else this module's own (a script run from
+// anywhere -- a project directory), else the cwd's parent.
+export const REPO_ROOT =
+ cliRepoRoot(process.argv[1]) ?? findCheckout(process.cwd()) ?? moduleRepoRoot() ?? findRepoRoot(process.cwd());
export const CHANNELS_DIR = path.resolve(
/* turbopackIgnore: true */
diff --git a/umtool/lib/paths.test.mjs b/umtool/lib/paths.test.mjs
@@ -8,7 +8,8 @@ import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node
import { tmpdir } from "node:os";
import path from "node:path";
import test from "node:test";
-import { cliRepoRoot } from "./paths.mjs";
+import { fileURLToPath } from "node:url";
+import { cliRepoRoot, findCheckout, moduleRepoRoot } from "./paths.mjs";
const PATHS = new URL("./paths.mjs", import.meta.url);
@@ -67,3 +68,30 @@ test("a CLI run from outside its checkout reads that checkout's sites and channe
);
assert.equal(set.sites, away);
});
+
+test("a script run from outside every checkout reads the corpus of the checkout its modules are in, not the cwd's parent", (t) => {
+ // Not a umtool/bin entry, so cliRepoRoot is null; the cwd has no
+ // pnpm-workspace.yaml above it (a project directory under REPORTS_DIR).
+ // Before moduleRepoRoot the cwd's PARENT was the "checkout", and every clip
+ // planned no-window (2 Nov, emberparadox/in-her-words).
+ const away = mkdtempSync(path.join(tmpdir(), "umtool-away-"));
+ t.after(() => rmSync(away, { recursive: true, force: true }));
+ const project = path.join(away, "folder", "project");
+ mkdirSync(project, { recursive: true });
+ const probe = path.join(away, "probe.mjs");
+ writeFileSync(
+ probe,
+ `const p = await import(${JSON.stringify(PATHS.href)});\n` +
+ "console.log(JSON.stringify({ repo: p.REPO_ROOT, channels: p.CHANNELS_DIR }));\n",
+ );
+ const env = { ...process.env };
+ for (const k of ["SITES_DIR", "CHANNELS_DIR", "TRANSCRIPTS_DIR"]) delete env[k];
+ const repo = path.resolve(path.dirname(fileURLToPath(PATHS)), "..", "..");
+ for (const cwd of [project, away, path.join(repo, "umtool")]) {
+ const got = JSON.parse(execFileSync(process.execPath, [probe], { cwd, env, encoding: "utf8" }));
+ assert.deepEqual(got, { repo, channels: path.join(repo, "transcripts", "channels") }, `from ${cwd}`);
+ }
+ assert.equal(moduleRepoRoot(PATHS.href), repo);
+ assert.equal(moduleRepoRoot("https://example.invalid/paths.mjs"), null);
+ assert.equal(findCheckout(project), null);
+});