build: stage beside dist/, not inside it

The staging directory was dist/.build-XXXXXX. A build that is killed cannot remove it, the next build removed
only its own, and "files": ["dist"] put the leftover into the npm tarball. .gitignore hid it from git status and
the freshness test skipped every name starting with ".build-", so nothing reported it.

The staging directory is now .dist-staging beside the output (.<name>-staging for --out <dir>) and is cleared at
the start of each build. The freshness test no longer skips anything: a stray file under dist/ fails it.

Checked in a scratch copy: a build stopped with SIGKILL after 1.5 s leaves .dist-staging/ and an unchanged dist/;
npm pack lists 36 files, none from the staging directory; the next build removes it. New test for the same on a
temporary package. npm test: 28 pass in 8.2-9.5 s.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
David 2026-09-28 14:40:36 +00:00
parent c340e11f6f
commit ee311dab01
4 changed files with 30 additions and 11 deletions

View file

@ -44,6 +44,22 @@ function build(dir, ...args) {
return spawnSync(process.execPath, [path.join(dir, "scripts", "build.mjs"), ...args], { encoding: "utf8" });
}
test("a build replaces dist/ whole and clears the staging directory a killed build left beside it", (t) => {
const dir = scratchPackage(t, "export const port = 1883;\n");
fs.mkdirSync(path.join(dir, ".dist-staging", "cjs"), { recursive: true });
fs.writeFileSync(path.join(dir, ".dist-staging", "cjs", "Killed.js"), "");
const run = build(dir);
assert.equal(run.status, 0, run.stdout + run.stderr);
const inputs = /^(scripts|src|tsconfig)/;
assert.deepEqual(Object.keys(snapshot(dir)).filter((name) => !inputs.test(name)).sort(), [
"dist/",
"dist/cjs/", "dist/cjs/index.js",
"dist/esm/", "dist/esm/index.d.ts", "dist/esm/index.js", "dist/esm/package.json",
"dist/types/", "dist/types/index.d.ts",
]);
});
test("a type error in src/: the build exits 1 and dist/ is as the earlier build left it", (t) => {
const dir = scratchPackage(t, "export const port: number = \"1883\";\n");
const before = snapshot(dir);

View file

@ -13,10 +13,9 @@ const pkg = require("alex2node/package.json");
const ROOT = path.join(__dirname, "..");
const DIST = path.join(ROOT, "dist");
// Every file under dir, relative to it, sorted. A staging directory left by a killed build is not part of dist/.
// Every file under dir, relative to it, sorted.
function files(dir, prefix = "") {
return fs.readdirSync(path.join(dir, prefix), { withFileTypes: true })
.filter((entry) => !entry.name.startsWith(".build-"))
.flatMap((entry) => (entry.isDirectory() ? files(dir, path.join(prefix, entry.name)) : [path.join(prefix, entry.name)]))
.sort();
}