diff --git a/bench/html-subset-render.cjs b/bench/html-subset-render.cjs
index 54f00eb..c0a72e1 100644
--- a/bench/html-subset-render.cjs
+++ b/bench/html-subset-render.cjs
@@ -8,7 +8,7 @@
const { createHash } = require("node:crypto");
const { readFileSync } = require("node:fs");
const { join } = require("node:path");
-const { loadMarkengineInRealm } = require("../tests/support/loadMarkengine.cjs");
+const { loadMarkengineInRealm, bundleToDisk } = require("../tests/support/loadMarkengine.cjs");
const E = loadMarkengineInRealm("bench/scanEntry.ts");
@@ -26,7 +26,7 @@ const CACHE_DIR = join(__dirname, "..", "node_modules", ".cache", "loadMarkengin
// tests/support/loadMarkengine.cjs writes it). The digest lets the test hold
// every worker to one bundle (D6): a stale or split bundle shows as two digests.
function bundleEvidence() {
- const file = join(CACHE_DIR, `bench_scanEntry_ts.${process.pid}.cjs`);
+ const file = bundleToDisk("bench/scanEntry.ts");
return { file, sha256: createHash("sha256").update(readFileSync(file)).digest("hex") };
}
diff --git a/tests/bundle-cache.test.cjs b/tests/bundle-cache.test.cjs
new file mode 100644
index 0000000..fd7bf1b
--- /dev/null
+++ b/tests/bundle-cache.test.cjs
@@ -0,0 +1,26 @@
+"use strict";
+
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const { basename } = require("node:path");
+const { bundleToDisk, computeSourceTreeHash } = require("./support/loadMarkengine.cjs");
+
+test("bundleToDisk keys the cached file by source tree hash rather than pid", () => {
+ const outfile = bundleToDisk("index.ts");
+ const fileName = basename(outfile);
+
+ // Must match index_ts.<16-char-hash>.cjs
+ const hash = computeSourceTreeHash();
+ assert.equal(hash.length, 16);
+ assert.match(fileName, /^index_ts\.[0-9a-f]{16}\.cjs$/);
+ assert.equal(fileName, `index_ts.${hash}.cjs`);
+
+ // Must not be keyed by process.pid
+ assert.notEqual(fileName, `index_ts.${process.pid}.cjs`);
+});
+
+test("bundleToDisk produces consistent output across invocations", () => {
+ const first = bundleToDisk("blocks.ts");
+ const second = bundleToDisk("blocks.ts");
+ assert.equal(first, second);
+});
diff --git a/tests/support/loadMarkengine.cjs b/tests/support/loadMarkengine.cjs
index fd97dca..52b7eff 100644
--- a/tests/support/loadMarkengine.cjs
+++ b/tests/support/loadMarkengine.cjs
@@ -3,7 +3,8 @@
// Shared loader: bundles the TS engine entry with esbuild to disk cache
// so .cjs tests can exercise the public API safely in the current realm.
const { join, resolve } = require("node:path");
-const { mkdirSync, writeFileSync, existsSync, renameSync } = require("node:fs");
+const { mkdirSync, writeFileSync, existsSync, renameSync, readdirSync, readFileSync, unlinkSync } = require("node:fs");
+const { createHash } = require("node:crypto");
const esbuild = require("esbuild");
const MARKENGINE_ROOT = resolve(__dirname, "..", "..");
@@ -23,6 +24,38 @@ module.exports = {
);
}
+let cachedSourceTreeHash = null;
+
+function computeSourceTreeHash() {
+ if (cachedSourceTreeHash) return cachedSourceTreeHash;
+ const hash = createHash("sha256");
+ function walk(dir) {
+ const entries = readdirSync(dir, { withFileTypes: true });
+ entries.sort((a, b) => a.name.localeCompare(b.name));
+ for (const ent of entries) {
+ if (
+ ent.name === "node_modules" ||
+ ent.name === ".git" ||
+ ent.name === "dist" ||
+ ent.name === ".cache" ||
+ ent.name === "coverage"
+ ) {
+ continue;
+ }
+ const full = join(dir, ent.name);
+ if (ent.isDirectory()) {
+ walk(full);
+ } else if (/\.(tsx?|json)$/.test(ent.name)) {
+ hash.update(ent.name);
+ hash.update(readFileSync(full));
+ }
+ }
+ }
+ walk(MARKENGINE_ROOT);
+ cachedSourceTreeHash = hash.digest("hex").slice(0, 16);
+ return cachedSourceTreeHash;
+}
+
const memoized = new Map();
function bundleToDisk(entry) {
@@ -31,10 +64,11 @@ function bundleToDisk(entry) {
throw new Error("Invalid entry point: path traversal detected");
}
const safeName = entry.replace(/[/\\.]/g, "_");
- const outfile = join(CACHE_DIR, `${safeName}.${process.pid}.cjs`);
+ const treeHash = computeSourceTreeHash();
+ const outfile = join(CACHE_DIR, `${safeName}.${treeHash}.cjs`);
if (!existsSync(outfile)) {
- const tmpFile = `${outfile}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`;
+ const tmpFile = `${outfile}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`;
esbuild.buildSync({
entryPoints: [fullPath],
bundle: true,
@@ -44,7 +78,13 @@ function bundleToDisk(entry) {
outfile: tmpFile,
tsconfig: join(MARKENGINE_ROOT, "tsconfig.json"),
});
- renameSync(tmpFile, outfile);
+ try {
+ renameSync(tmpFile, outfile);
+ } catch {
+ try {
+ unlinkSync(tmpFile);
+ } catch {}
+ }
}
return outfile;
@@ -70,4 +110,4 @@ function loadMarkengineAll() {
return Object.assign({}, ...entries.map((entry) => loadMarkengine(entry)));
}
-module.exports = { loadMarkengine, loadMarkengineAll, loadMarkengineInRealm };
+module.exports = { loadMarkengine, loadMarkengineAll, loadMarkengineInRealm, bundleToDisk, computeSourceTreeHash };