From 3bcb93ea9525af4ca2b44360bb34e394577fda2c Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Sun, 20 Sep 2026 20:55:12 -0400 Subject: [PATCH 1/6] fix(bulma-ui): give the require condition CommonJS-flavoured declarations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #698. The package is `type: module`, so every emitted `.d.ts` reads as an ES module, and one `types` target cannot describe both conditions: a consumer on `moduleResolution: node16` that REQUIRES the package met TS1479 — "the referenced file is an ECMAScript module and cannot be imported with 'require'". #688 fixed the runtime half; this is the declaration half. Measured every option before choosing one, which #698 asked for. The conventional answer is a parallel `.d.cts` tree, which means renaming 131 files and rewriting every `./x.js` to `./x.cjs`. A nested manifest does the whole job instead: flavour comes from the nearest `package.json`, so the same bytes read as CommonJS from under `dist/types-cjs`, and the specifiers the extension pass already wrote are correct there unchanged. That is the whole reason this is a copy and not a second emit. Verified against a real consumer in all five shapes rather than reasoned about: node16 CommonJS TS1479 before, clean after node16 ESM clean, unchanged nodenext CommonJS clean nodenext ESM clean bundler clean Cost is one extra copy of the declarations, about 6% of unpacked `dist`, and declarations compress well. The mirror runs after `declarationExtensions` and carries the same latch, for the same reason: `closeBundle` fires on every failure path, and copying before the extension pass would mirror a tree whose specifiers do not resolve. #698's tripwire did its job and is updated rather than deleted. It asserted TS1479 and said in its own message that fixing #698 is what needs the expectation changed; it now asserts both conditions typecheck, and stays a pair because giving `require` its own types must not cost the `import` side anything. Three mutants — dropping the nested manifest, writing `module` in it, and skipping the copy — each caught, and each only after rebuilding: the first run passed all three because the tests read a `dist` the mutation had not touched. Not in scope: `publishable-manifests` still does not judge `types` targets by flavour. #698 names that rule as where such a check belongs, and it is a bigger change than this one. --- bulma-ui/package.json | 11 +++- bulma-ui/rollup.config.js | 92 +++++++++++++++++++++++++++++- scripts/constants-subpath.test.mjs | 68 ++++++++++++++++------ 3 files changed, 151 insertions(+), 20 deletions(-) diff --git a/bulma-ui/package.json b/bulma-ui/package.json index 57f78e16e..1149f9ee3 100644 --- a/bulma-ui/package.json +++ b/bulma-ui/package.json @@ -133,9 +133,14 @@ }, "exports": { ".": { - "types": "./dist/types/index.d.ts", - "import": "./dist/index.esm.js", - "require": "./dist/index.cjs" + "import": { + "types": "./dist/types/index.d.ts", + "default": "./dist/index.esm.js" + }, + "require": { + "types": "./dist/types-cjs/index.d.ts", + "default": "./dist/index.cjs" + } }, "./constants": { "import": { diff --git a/bulma-ui/rollup.config.js b/bulma-ui/rollup.config.js index 353c56e22..79f83c45e 100644 --- a/bulma-ui/rollup.config.js +++ b/bulma-ui/rollup.config.js @@ -1,4 +1,4 @@ -import { readFile, readdir, writeFile } from 'node:fs/promises'; +import { cp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; import { existsSync } from 'node:fs'; import { dirname, @@ -724,6 +724,94 @@ export const hasModuleSpecifiers = text => { * on its own reaches the check below, which is why the check is here rather * than being left to the test. */ +/** + * Mirror `dist/types` into `dist/types-cjs` and mark it CommonJS, so the + * `require` condition has declarations of the right flavour (#698). + * + * The package is `type: module`, so every emitted `.d.ts` reads as an ES module + * and a `moduleResolution: node16` consumer that REQUIRES the package meets + * TS1479 — "the referenced file is an ECMAScript module and cannot be imported + * with 'require'". One `types` target cannot describe both conditions. + * + * A nested `{"type":"commonjs"}` does the whole job, which is why this is a copy + * rather than a second declaration emit. Flavour comes from the nearest manifest, + * so the same bytes read as CommonJS from under `dist/types-cjs` — no `.d.cts` + * renaming, and no rewriting `./x.js` to `./x.cjs`, because inside that tree + * `./x.js` already resolves to a CommonJS `x.d.ts`. The specifiers the main pass + * wrote are correct here unchanged, which is the whole reason this is cheap. + * + * Measured before choosing it: all five consumer shapes — node16 and nodenext, + * CommonJS and ESM, plus bundler — typecheck clean against a package laid out + * this way, where node16 CommonJS is TS1479 without it. The cost is one extra + * copy of the declarations, about 6% of unpacked `dist`. + * + * Runs from `closeBundle` at config level, after `declarationExtensions` has + * given every specifier its extension — copying before that would mirror a tree + * whose specifiers do not resolve, and the post-pass there would never see the + * copy. + */ +const commonjsDeclarationTypes = ( + from = 'dist/types', + to = 'dist/types-cjs' +) => { + let failed = false; + let started = 0; + let wrote = 0; + return { + name: 'bestax-commonjs-declaration-types', + buildStart() { + failed = false; + started = 0; + wrote = 0; + }, + buildEnd(error) { + if (error) failed = true; + }, + renderError() { + failed = true; + }, + renderStart() { + started += 1; + }, + writeBundle() { + wrote += 1; + }, + async closeBundle(error) { + // The same latch `declarationExtensions` carries, for the same reason: + // `closeBundle` fires on every failure path too, and this hook's own + // error would replace the real one. + if (failed || error || started === 0 || wrote !== started) return; + let entries; + try { + entries = await readdir(from, { withFileTypes: true }); + } catch (readError) { + if (readError.code === 'ENOENT') { + throw new Error( + `${from} is missing, so ${to} cannot be written. It comes from the ` + + 'declaration pass, which has to run before this.' + ); + } + throw readError; + } + if (!entries.length) { + throw new Error( + `${from} is empty, so ${to} would describe nothing. The declaration ` + + 'pass has to run before this.' + ); + } + await rm(to, { recursive: true, force: true }); + await cp(from, to, { recursive: true }); + // The whole mechanism. Flavour is decided by the nearest manifest, so this + // one line is what makes the copied declarations CommonJS. + await writeFile( + join(to, 'package.json'), + `${JSON.stringify({ type: 'commonjs' }, null, 2)}\n`, + 'utf8' + ); + }, + }; +}; + const constantsCjsTypes = () => ({ name: 'bestax-constants-cjs-types', async writeBundle() { @@ -807,6 +895,8 @@ export default commandLineArgs => { // correct under `--watch`: that rebuilds only the config whose inputs // changed, so a rewrite hung off any other entry never runs. declarationExtensions(), + // After it: this mirrors what that pass has finished rewriting. + commonjsDeclarationTypes(), resolve(), commonjs(), typescript({ diff --git a/scripts/constants-subpath.test.mjs b/scripts/constants-subpath.test.mjs index bb04b2a2c..f11e49ab8 100644 --- a/scripts/constants-subpath.test.mjs +++ b/scripts/constants-subpath.test.mjs @@ -364,15 +364,55 @@ describe('bulma-ui export map', () => { ); }); - it('pins the node16 boundary: ESM resolves, CommonJS is the known #698 gap', () => { + it('gives each root condition declarations of its own flavour', () => { requireBuilt(); - // #696 names `node16` as well as `nodenext`, and the two fixtures below - // cover only the latter. The pair is asserted together because the - // interesting fact is the BOUNDARY: under `node16` an ESM consumer is fine, - // and a CommonJS one meets TS1479 because a single ESM-flavoured `types` - // target serves both conditions. That failure is #698 and pre-existing — - // the same consumer got TS2305 before this change — and pinning it means - // fixing #698 fails this test, which is when it should be revisited. + // #698: the package is `type: module`, so every emitted `.d.ts` reads as an + // ES module, and one `types` target cannot describe both conditions — a + // `require` consumer on `node16` met TS1479. The consumer fixture below is + // what proves the fix; this case pins the SHAPE, so a change that keeps the + // types resolving but collapses the conditions is caught here rather than by + // a typecheck that happens to still pass. + const root = manifest.exports['.']; + assert.equal( + typeof root.import?.types, + 'string', + 'the import condition has no types target of its own' + ); + assert.equal( + typeof root.require?.types, + 'string', + 'the require condition has no types target of its own' + ); + assert.notEqual( + root.import.types, + root.require.types, + 'both conditions point at the same declarations, which is the #698 bug' + ); + // Both targets exist. + for (const spec of [root.import.types, root.require.types]) { + assert.ok(existsSync(target(spec)), `${spec} does not exist`); + } + // And the mechanism: flavour comes from the nearest manifest, so the CJS + // tree carries one saying so. Without this line the copied declarations + // read as ES modules again and the fix silently undoes itself. + const nested = JSON.parse( + readFileSync(join(PKG_DIR, 'dist', 'types-cjs', 'package.json'), 'utf8') + ); + assert.equal(nested.type, 'commonjs'); + }); + + it('typechecks a node16 consumer through BOTH conditions', () => { + requireBuilt(); + // #696 names `node16` as well as `nodenext`, and the other fixtures cover + // only the latter. This pair is asserted together because it used to be a + // BOUNDARY: an ESM consumer was fine and a CommonJS one met TS1479, because + // one ESM-flavoured `types` target served both conditions. + // + // That was #698, and this case was written to fail when it was fixed — which + // it did, on the commit that gave the `require` condition its own + // CommonJS-flavoured declarations. Both sides pass now, and the pair stays + // because the interesting property is still the pair: giving `require` its + // own types must not cost the `import` side anything. const build = type => { const dir = mkdtempSync(join(tmpdir(), `bestax-node16-${type}-`)); mkdirSync(join(dir, 'src'), { recursive: true }); @@ -429,16 +469,12 @@ describe('bulma-ui export map', () => { ); const cjs = build('cjs'); - assert.notEqual( + assert.equal( cjs.status, 0, - 'a node16 CommonJS consumer now typechecks — #698 may be fixed, in ' + - 'which case this expectation is what needs updating' - ); - assert.match( - cjs.stdout, - /TS1479/, - `expected the known #698 failure, got:\n${cjs.stdout || cjs.stderr}` + `a node16 CommonJS consumer does not typecheck:\n${ + cjs.stdout || cjs.stderr + }` ); }); From a7d882c3acdb78d4e8f1891282bb949c6f67662c Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Sun, 20 Sep 2026 21:34:29 -0400 Subject: [PATCH 2/6] fix(bulma-ui): drop the mirror's rm, which made two safe writers a race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pre-PR review found this and it is the worst kind of bug to have shipped: a quiet one that puts back the thing the PR fixes, behind a green gate. `pnpm all` hands `build` and `bundle:stats` to one `turbo run`, and the graph has no edge between them — `build` depends on `^build` (upstream packages), and `bundle:stats` depends on nothing. Both are `rollup -c` over the same `dist`, so they run concurrently in the first wave. That was harmless while the two merely wrote identical bytes. Adding `rm(to, { recursive: true })` made one of them delete a directory the other was walking. The loud outcome is a `cp` failing mid-walk and reddening the local gate with a message naming neither cause — and since `all` is a `&&` chain, every later step is skipped. The quiet one is worse: an `rm` landing after the other process wrote the nested manifest leaves `dist/types-cjs` without its `package.json`, which falls back to the root `type: module` and restores TS1479 with everything green. Turbo then caches `dist/**` for both tasks, so the inconsistent mix can be restored later. No `rm`. `cp` overwrites, so a rebuild refreshes every file that still exists, and two processes writing identical bytes is what happened before this plugin existed. A declaration deleted from `src` leaves a stale copy — the same thing `dist` already does everywhere, since nothing cleans it, and the extension pass holds the source tree to resolving rather than this one. Verified rather than reasoned: five concurrent build pairs, manifest intact and 131 files every time. CI is not exposed — it runs build and bundle:stats as separate sequential steps — so this was the local gate only, which is exactly where it would have been blamed on something else. Also fixed a comment I broke while rewriting the tripwire: "the other fixtures cover only the latter" is false, because `./constants` has its own node16 CommonJS fixture. It now says what this pair actually is — the ROOT entry's node16 coverage, which is where #698 bit. Refs #698 --- bulma-ui/rollup.config.js | 17 +++++++++++++++-- scripts/constants-subpath.test.mjs | 5 +++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bulma-ui/rollup.config.js b/bulma-ui/rollup.config.js index 79f83c45e..0f4db5522 100644 --- a/bulma-ui/rollup.config.js +++ b/bulma-ui/rollup.config.js @@ -1,4 +1,4 @@ -import { cp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; +import { cp, readFile, readdir, writeFile } from 'node:fs/promises'; import { existsSync } from 'node:fs'; import { dirname, @@ -799,7 +799,20 @@ const commonjsDeclarationTypes = ( 'pass has to run before this.' ); } - await rm(to, { recursive: true, force: true }); + // NO `rm` first, deliberately. `pnpm all` hands `build` and + // `bundle:stats` to one `turbo run` with no edge between them, and both + // are `rollup -c` over this same `dist` — so they run concurrently. Two + // processes writing identical bytes is harmless and is what happened + // before this plugin existed; one of them clearing the directory first is + // not. The loud outcome is a `cp` that fails mid-walk; the quiet one is an + // `rm` landing after the other process wrote the manifest, leaving a tree + // with no `package.json`, which falls back to the root `type: module` and + // puts TS1479 back with a green gate. + // + // `cp` overwrites, so a rebuild refreshes every file that still exists. A + // declaration deleted from `src` leaves a stale copy here — the same thing + // `dist` already does everywhere else, since nothing cleans it, and the + // extension pass holds the source tree to resolving rather than this one. await cp(from, to, { recursive: true }); // The whole mechanism. Flavour is decided by the nearest manifest, so this // one line is what makes the copied declarations CommonJS. diff --git a/scripts/constants-subpath.test.mjs b/scripts/constants-subpath.test.mjs index f11e49ab8..4d0bd35f0 100644 --- a/scripts/constants-subpath.test.mjs +++ b/scripts/constants-subpath.test.mjs @@ -403,8 +403,9 @@ describe('bulma-ui export map', () => { it('typechecks a node16 consumer through BOTH conditions', () => { requireBuilt(); - // #696 names `node16` as well as `nodenext`, and the other fixtures cover - // only the latter. This pair is asserted together because it used to be a + // The ROOT entry under `node16`, both conditions. The `./constants` subpath + // has its own node16 pair; this is the root's, which is where #698 bit. + // Asserted together because it used to be a // BOUNDARY: an ESM consumer was fine and a CommonJS one met TS1479, because // one ESM-flavoured `types` target served both conditions. // From 605ec3adde03b2e570ec62789d6eaa441badf477 Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Mon, 21 Sep 2026 18:52:24 -0400 Subject: [PATCH 3/6] fix(bulma-ui): closeBundle is parallel, so the mirror must be a step not a peer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Critical finding on #715, and it invalidated the thing the PR is for. I put the CommonJS mirror in its own plugin and sequenced it by position in the plugin array. That buys nothing: `closeBundle` is a PARALLEL hook, so rollup runs every plugin's copy at once. The mirror raced `declarationExtensions` and copied `dist/types` while it was still being rewritten. Not theoretical, and not rare. Three builds produced 184, 182 and 180 extensionless specifiers in the mirror — varying run to run — which is the CommonJS declarations shipping with specifiers that do not resolve, which is #698 unfixed by the commit that claims to fix it. Every one of those builds passed the consumer fixture, because that fixture typechecks one entry and `index.d.ts` happened to be rewritten before the copy reached it. The mirror is a step at the end of the extension pass now. Ordering within one hook is what buys ordering; plugin order does not. And the assertion that would have caught it: the two trees must be byte-identical, every file. A consumer fixture structurally cannot see this — it follows one entry, so a mirror captured mid-rewrite passes as long as the files that entry reaches happen to be finished. Verified deterministic over three builds: 0 differing, 0 extensionless, 0 truncated. The mutant that reinstates the race is caught. Two smaller corrections. The doc comment carried point-in-time measurements that were true when taken. And I had the reason wrong for why no conformance rule judges this tree: it is not the `.ts` extension, it is that `types` is absent from the require-matching condition set, so a `types` target is never descended into at any extension. Refs #698 --- bulma-ui/rollup.config.js | 156 ++++++++++------------------- scripts/constants-subpath.test.mjs | 39 +++++++- 2 files changed, 90 insertions(+), 105 deletions(-) diff --git a/bulma-ui/rollup.config.js b/bulma-ui/rollup.config.js index 0f4db5522..e1da66772 100644 --- a/bulma-ui/rollup.config.js +++ b/bulma-ui/rollup.config.js @@ -284,7 +284,10 @@ export const specifierResolves = (root, file, spec, declarationAllowed) => { * the one member of that class left unaddressed rather than unreachable by * accident. */ -export const declarationExtensions = (root = 'dist/types') => { +export const declarationExtensions = ( + root = 'dist/types', + mirror = 'dist/types-cjs' +) => { // `closeBundle` fires on every FAILURE path too, where `dist/types` is absent // or stale because the build never got that far. Without a latch this hook's // own error replaces the real one — a compile failure reported as a missing @@ -623,10 +626,58 @@ export const declarationExtensions = (root = 'dist/types') => { ); } } + + // The CommonJS mirror, HERE rather than in a plugin of its own, because + // `closeBundle` is a PARALLEL hook: rollup runs every plugin's copy at + // once, so a separate plugin raced this one and copied `dist/types` + // mid-rewrite. That is not theoretical — three builds produced 184, 182 + // and 180 extensionless specifiers in the mirror, varying run to run, + // and the consumer fixture passed only because `index.d.ts` happened to + // be rewritten before the copy reached it. + // + // Plugin ORDER buys nothing here. Ordering within one hook is what buys + // it, which is why this is a step rather than a neighbour. + if (mirror) await mirrorAsCommonjs(root, mirror); }, }; }; +/** + * Copy a finished declaration tree and mark the copy CommonJS (#698). + * + * The package is `type: module`, so every emitted `.d.ts` reads as an ES module + * and a `moduleResolution: node16` consumer that REQUIRES the package meets + * TS1479. One `types` target cannot describe both conditions. + * + * A nested `{"type":"commonjs"}` does the whole job, which is why this is a copy + * rather than a second declaration emit: flavour comes from the nearest + * manifest, so the same bytes read as CommonJS from under the copy — no `.d.cts` + * renaming, and no rewriting `./x.js` to `./x.cjs`, because inside that tree + * `./x.js` already resolves to a CommonJS `x.d.ts`. + * + * No `rm` before the copy. `pnpm all` hands `build` and `bundle:stats` to one + * `turbo run` with no edge between them, and both are `rollup -c` over this same + * `dist`, so they run concurrently. Two processes writing identical bytes is + * harmless; one clearing the directory first is not, and the quiet outcome — + * an `rm` landing after the other wrote the manifest — leaves a tree with no + * `package.json`, which falls back to the root `type: module` and puts TS1479 + * back with everything green. + */ +const mirrorAsCommonjs = async (from, to) => { + // Worth knowing that no conformance rule judges this: `types` is absent from + // the require-matching condition set, so a `types` target is never descended + // into at any extension. The consumer fixtures are what hold this tree + // honest. + + await cp(from, to, { recursive: true }); + // The whole mechanism: one line, and without it the copy reads as ESM again. + await writeFile( + join(to, 'package.json'), + `${JSON.stringify({ type: 'commonjs' }, null, 2)}\n`, + 'utf8' + ); +}; + /** * Whether a declaration carries anything that makes copying it to a `.d.cts` * unsound. @@ -724,107 +775,6 @@ export const hasModuleSpecifiers = text => { * on its own reaches the check below, which is why the check is here rather * than being left to the test. */ -/** - * Mirror `dist/types` into `dist/types-cjs` and mark it CommonJS, so the - * `require` condition has declarations of the right flavour (#698). - * - * The package is `type: module`, so every emitted `.d.ts` reads as an ES module - * and a `moduleResolution: node16` consumer that REQUIRES the package meets - * TS1479 — "the referenced file is an ECMAScript module and cannot be imported - * with 'require'". One `types` target cannot describe both conditions. - * - * A nested `{"type":"commonjs"}` does the whole job, which is why this is a copy - * rather than a second declaration emit. Flavour comes from the nearest manifest, - * so the same bytes read as CommonJS from under `dist/types-cjs` — no `.d.cts` - * renaming, and no rewriting `./x.js` to `./x.cjs`, because inside that tree - * `./x.js` already resolves to a CommonJS `x.d.ts`. The specifiers the main pass - * wrote are correct here unchanged, which is the whole reason this is cheap. - * - * Measured before choosing it: all five consumer shapes — node16 and nodenext, - * CommonJS and ESM, plus bundler — typecheck clean against a package laid out - * this way, where node16 CommonJS is TS1479 without it. The cost is one extra - * copy of the declarations, about 6% of unpacked `dist`. - * - * Runs from `closeBundle` at config level, after `declarationExtensions` has - * given every specifier its extension — copying before that would mirror a tree - * whose specifiers do not resolve, and the post-pass there would never see the - * copy. - */ -const commonjsDeclarationTypes = ( - from = 'dist/types', - to = 'dist/types-cjs' -) => { - let failed = false; - let started = 0; - let wrote = 0; - return { - name: 'bestax-commonjs-declaration-types', - buildStart() { - failed = false; - started = 0; - wrote = 0; - }, - buildEnd(error) { - if (error) failed = true; - }, - renderError() { - failed = true; - }, - renderStart() { - started += 1; - }, - writeBundle() { - wrote += 1; - }, - async closeBundle(error) { - // The same latch `declarationExtensions` carries, for the same reason: - // `closeBundle` fires on every failure path too, and this hook's own - // error would replace the real one. - if (failed || error || started === 0 || wrote !== started) return; - let entries; - try { - entries = await readdir(from, { withFileTypes: true }); - } catch (readError) { - if (readError.code === 'ENOENT') { - throw new Error( - `${from} is missing, so ${to} cannot be written. It comes from the ` + - 'declaration pass, which has to run before this.' - ); - } - throw readError; - } - if (!entries.length) { - throw new Error( - `${from} is empty, so ${to} would describe nothing. The declaration ` + - 'pass has to run before this.' - ); - } - // NO `rm` first, deliberately. `pnpm all` hands `build` and - // `bundle:stats` to one `turbo run` with no edge between them, and both - // are `rollup -c` over this same `dist` — so they run concurrently. Two - // processes writing identical bytes is harmless and is what happened - // before this plugin existed; one of them clearing the directory first is - // not. The loud outcome is a `cp` that fails mid-walk; the quiet one is an - // `rm` landing after the other process wrote the manifest, leaving a tree - // with no `package.json`, which falls back to the root `type: module` and - // puts TS1479 back with a green gate. - // - // `cp` overwrites, so a rebuild refreshes every file that still exists. A - // declaration deleted from `src` leaves a stale copy here — the same thing - // `dist` already does everywhere else, since nothing cleans it, and the - // extension pass holds the source tree to resolving rather than this one. - await cp(from, to, { recursive: true }); - // The whole mechanism. Flavour is decided by the nearest manifest, so this - // one line is what makes the copied declarations CommonJS. - await writeFile( - join(to, 'package.json'), - `${JSON.stringify({ type: 'commonjs' }, null, 2)}\n`, - 'utf8' - ); - }, - }; -}; - const constantsCjsTypes = () => ({ name: 'bestax-constants-cjs-types', async writeBundle() { @@ -908,8 +858,6 @@ export default commandLineArgs => { // correct under `--watch`: that rebuilds only the config whose inputs // changed, so a rewrite hung off any other entry never runs. declarationExtensions(), - // After it: this mirrors what that pass has finished rewriting. - commonjsDeclarationTypes(), resolve(), commonjs(), typescript({ diff --git a/scripts/constants-subpath.test.mjs b/scripts/constants-subpath.test.mjs index 4d0bd35f0..afd60a1ec 100644 --- a/scripts/constants-subpath.test.mjs +++ b/scripts/constants-subpath.test.mjs @@ -35,12 +35,13 @@ import { copyFileSync, mkdtempSync, readFileSync, + readdirSync, symlinkSync, writeFileSync, } from 'node:fs'; import { createRequire } from 'node:module'; import { tmpdir } from 'node:os'; -import { dirname, join, resolve } from 'node:path'; +import { dirname, join, relative, resolve } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { describe, it } from 'node:test'; @@ -399,6 +400,42 @@ describe('bulma-ui export map', () => { readFileSync(join(PKG_DIR, 'dist', 'types-cjs', 'package.json'), 'utf8') ); assert.equal(nested.type, 'commonjs'); + + // BYTE-IDENTICAL, every file, because the copy is the only thing making the + // two trees agree. A consumer fixture cannot see this: it typechecks one + // entry, so a mirror captured mid-rewrite still passes as long as the files + // that entry reaches happen to be finished. + // + // That is not hypothetical. `closeBundle` is a PARALLEL hook, so when the + // copy lived in its own plugin it raced the rewrite — three builds produced + // 184, 182 and 180 extensionless specifiers in the mirror, and the fixture + // passed all three. + const esm = join(PKG_DIR, 'dist', 'types'); + const cjs = join(PKG_DIR, 'dist', 'types-cjs'); + const walk = (dir, base = dir, out = []) => { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const full = join(dir, entry.name); + if (entry.isDirectory()) walk(full, base, out); + else if (/\.d\.[cm]?ts$/.test(entry.name)) + out.push(relative(base, full)); + } + return out; + }; + const declarations = walk(esm); + assert.ok(declarations.length > 0, 'no declarations to mirror'); + const missing = declarations.filter(f => !existsSync(join(cjs, f))); + assert.deepEqual(missing, [], 'the mirror is missing declarations'); + const differing = declarations.filter( + f => + readFileSync(join(esm, f), 'utf8') !== + readFileSync(join(cjs, f), 'utf8') + ); + assert.deepEqual( + differing, + [], + 'the mirror does not match the declarations it copied — it was taken ' + + 'while they were still being rewritten' + ); }); it('typechecks a node16 consumer through BOTH conditions', () => { From 5c365acbbe57874abc89522492924ad96ac1eaca Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Mon, 21 Sep 2026 20:33:12 -0400 Subject: [PATCH 4/6] fix(bulma-ui): derive the mirror from root, so the tests stop writing to dist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round 2 on #715, and the blocking finding is the guard cases overwriting the artefact they exist to guard. `mirror` defaulted to the literal `'dist/types-cjs'`, which resolves against `cwd`, not against the `root` each case passes. So every guard case copied its temp fixture tree into a real `dist/types-cjs` — and with `cwd` at `bulma-ui` that path is the shipped CommonJS declaration tree. Confirmed by running them against a cleaned `dist`: it came back holding `a.d.ts`, `pkg.d.ts` and `plain.d.ts`, which are fixtures. It is gitignored, so nothing showed it. Between a build and a publish, those would have been the shipped types. Derived from `root` now, so the mirror lands beside whatever tree it was given. A case asserts that, comparing `dist/types-cjs` before and after a guard run; the mutant restoring the literal is caught. Two more from the same round. The byte-identity walk was one-directional, so a stale file left in the mirror by an earlier emit stayed green — and `dist` is never cleaned, which makes that the likely shape rather than an exotic one. It walks both ways now. And it hardcoded both paths; it derives them from `exports['.']` instead, so a `require.types` repointed at some other existing declaration cannot pass while sending consumers elsewhere. Refs #698 --- bulma-ui/rollup.config.js | 8 +++++- scripts/constants-subpath.test.mjs | 42 ++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/bulma-ui/rollup.config.js b/bulma-ui/rollup.config.js index e1da66772..49c87b3d8 100644 --- a/bulma-ui/rollup.config.js +++ b/bulma-ui/rollup.config.js @@ -286,7 +286,13 @@ export const specifierResolves = (root, file, spec, declarationAllowed) => { */ export const declarationExtensions = ( root = 'dist/types', - mirror = 'dist/types-cjs' + // DERIVED from `root`, never a default of its own. A literal + // `'dist/types-cjs'` is resolved against `cwd`, so every guard case — which + // passes a temp fixture as `root` — copied its fixtures into a real + // `dist/types-cjs` instead of beside the tree it was given. With `cwd` at + // `bulma-ui` that path is the shipped CommonJS declaration tree, and it is + // gitignored, so nothing showed it. + mirror = `${root}-cjs` ) => { // `closeBundle` fires on every FAILURE path too, where `dist/types` is absent // or stale because the build never got that far. Without a latch this hook's diff --git a/scripts/constants-subpath.test.mjs b/scripts/constants-subpath.test.mjs index afd60a1ec..482b71366 100644 --- a/scripts/constants-subpath.test.mjs +++ b/scripts/constants-subpath.test.mjs @@ -410,8 +410,11 @@ describe('bulma-ui export map', () => { // copy lived in its own plugin it raced the rewrite — three builds produced // 184, 182 and 180 extensionless specifiers in the mirror, and the fixture // passed all three. - const esm = join(PKG_DIR, 'dist', 'types'); - const cjs = join(PKG_DIR, 'dist', 'types-cjs'); + // DERIVED from the manifest, not hardcoded: a `require.types` repointed at + // some other existing declaration would otherwise leave this green while + // pointing consumers somewhere else entirely. + const esm = dirname(target(root.import.types)); + const cjs = dirname(target(root.require.types)); const walk = (dir, base = dir, out = []) => { for (const entry of readdirSync(dir, { withFileTypes: true })) { const full = join(dir, entry.name); @@ -425,6 +428,15 @@ describe('bulma-ui export map', () => { assert.ok(declarations.length > 0, 'no declarations to mirror'); const missing = declarations.filter(f => !existsSync(join(cjs, f))); assert.deepEqual(missing, [], 'the mirror is missing declarations'); + // BOTH directions. One-directional, a stale file left in the mirror by an + // earlier emit stays green — and `dist` is never cleaned, so that is the + // likely shape rather than an exotic one. + const extra = walk(cjs).filter(f => !existsSync(join(esm, f))); + assert.deepEqual( + extra, + [], + 'the mirror carries declarations the source tree does not' + ); const differing = declarations.filter( f => readFileSync(join(esm, f), 'utf8') !== @@ -910,6 +922,32 @@ describe('the declaration-extension guard', () => { ); }); + it('writes nothing outside the tree it was given', async () => { + // The mirror target is DERIVED from `root`. A literal default is resolved + // against `cwd`, so every case here copied its fixtures into a real + // `dist/types-cjs` — and with `cwd` at `bulma-ui` that is the shipped + // CommonJS declaration tree this whole PR exists to produce. Gitignored, so + // nothing showed it: the tests were overwriting the artefact under test. + const root = tree({ + 'index.d.ts': "export * from './a';\n", + 'a.d.ts': 'export {};\n', + }); + const outside = join(PKG_DIR, 'dist', 'types-cjs'); + const before = existsSync(outside) ? readdirSync(outside).length : null; + await run(root); + // The mirror lands beside the fixture, not beside the package. + assert.ok( + existsSync(`${root}-cjs`), + 'the mirror was not written beside the tree it was given' + ); + const after = existsSync(outside) ? readdirSync(outside).length : null; + assert.equal( + after, + before, + `the guard wrote into ${outside}, which is the shipped CommonJS tree` + ); + }); + it('leaves a bare package specifier alone', async () => { // The rewrite asks the parser for module specifiers, which includes bare // ones — `react` is in the real emitted tree. Only relative specifiers name From fd38a191ed0119d00386583bf072daf00c593d95 Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Mon, 21 Sep 2026 22:15:40 -0400 Subject: [PATCH 5/6] test: make the outside-write guard falsifiable, and not by a count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round 3 found the pair watching `bulma-ui/dist/types-cjs` while the suite runs from the repo root, where a literal default actually lands the mirror. So the assertion could not fail in the cwd it runs in. Both paths are watched now. That was half of it. The other half is that a count cannot see the write at all: a previous leak leaves the same file NAMES behind, so `readdirSync(...).length` matches on the next run while the copy is still landing. Reproduced by applying the mutant twice — the count-based version passes on the second run. Size and mtime move whether or not the entry set does. Ordered before the sibling `existsSync`, which fails on the same mutant and would otherwise shadow this one, leaving it unfalsifiable again for a new reason. Verified: with `mirror = 'dist/types-cjs'` restored, the case fails on THIS assertion naming /dist/types-cjs, against both a clean and an already-polluted directory; the unmutated tree passes. --- scripts/constants-subpath.test.mjs | 46 ++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/scripts/constants-subpath.test.mjs b/scripts/constants-subpath.test.mjs index 482b71366..93ee7e65a 100644 --- a/scripts/constants-subpath.test.mjs +++ b/scripts/constants-subpath.test.mjs @@ -36,6 +36,7 @@ import { mkdtempSync, readFileSync, readdirSync, + statSync, symlinkSync, writeFileSync, } from 'node:fs'; @@ -932,20 +933,49 @@ describe('the declaration-extension guard', () => { 'index.d.ts': "export * from './a';\n", 'a.d.ts': 'export {};\n', }); - const outside = join(PKG_DIR, 'dist', 'types-cjs'); - const before = existsSync(outside) ? readdirSync(outside).length : null; + // A literal default is resolved against `cwd`, and this suite runs as + // `node --test "scripts/*.test.mjs"` from the repo root — so the leak lands + // in `/dist/types-cjs`, NOT under `bulma-ui`. Watching only the latter + // is an assertion that cannot fail from the cwd the suite is run in; both + // are watched so the case fails from either. + // + // Identity, not a count: a previous leak leaves the same file NAMES behind, + // so `readdirSync(...).length` is unchanged on the next run and the check + // passes while the write is still happening. Size and mtime move when the + // copy lands, whether or not the entry set does. + const outside = [ + resolve('dist', 'types-cjs'), + join(PKG_DIR, 'dist', 'types-cjs'), + ]; + const snapshot = dir => + existsSync(dir) + ? readdirSync(dir) + .sort() + .map(name => { + const s = statSync(join(dir, name)); + return `${name}:${s.size}:${s.mtimeMs}`; + }) + .join('|') + : null; + const before = outside.map(snapshot); await run(root); + const after = outside.map(snapshot); + // The outside-write check comes FIRST. The sibling assertion below also + // fails on today's mutant, and asserting that first would shadow this one — + // leaving it unfalsifiable a second time, for a different reason than round + // 3 found. This is the assertion that names the harm. + for (const [i, dir] of outside.entries()) { + assert.equal( + after[i], + before[i], + `the guard wrote into ${dir}, which is the shipped CommonJS tree` + ); + } // The mirror lands beside the fixture, not beside the package. assert.ok( existsSync(`${root}-cjs`), 'the mirror was not written beside the tree it was given' ); - const after = existsSync(outside) ? readdirSync(outside).length : null; - assert.equal( - after, - before, - `the guard wrote into ${outside}, which is the shipped CommonJS tree` - ); }); it('leaves a bare package specifier alone', async () => { From 1e9af88bc3b31ea3e391ef97608a3096bef799a2 Mon Sep 17 00:00:00 2001 From: Alex Smith Date: Mon, 21 Sep 2026 22:20:17 -0400 Subject: [PATCH 6/6] test(bulma-ui): stop the mirror comparison from disarming itself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round 3's second finding, and the sharper half of it: the byte-identity walk compares two directories derived from `exports['.']`, so it passes vacuously the moment those are the same directory. `dirname` would collapse both sides and every file would match itself. The state that produces it is exactly the one the walk exists to catch: a `require.types` moved back into the ESM tree is #698 undone, and the comparison would have gone green on it. Asserted before the walk now, and the mutant that repoints `require.types` at `dist/types/index.d.ts` is caught. Accepting `fd38a191` as it stands, having driven it rather than read it: with the literal default restored the case fails on the assertion naming `/dist/types-cjs` and lists the leaked fixtures, and it is ordered ahead of the sibling `existsSync` that was shadowing it. Watching both roots and comparing name, size and mtime rather than a count is a better answer than the one I wrote — a previous leak leaves the same names, so counting could not see a second one. That the repo root had accumulated a real `dist/types-cjs` full of fixtures is the evidence the original guard was never falsifiable from the directory the suite runs in. Refs #698 --- scripts/constants-subpath.test.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/constants-subpath.test.mjs b/scripts/constants-subpath.test.mjs index 93ee7e65a..71fc381df 100644 --- a/scripts/constants-subpath.test.mjs +++ b/scripts/constants-subpath.test.mjs @@ -416,6 +416,17 @@ describe('bulma-ui export map', () => { // pointing consumers somewhere else entirely. const esm = dirname(target(root.import.types)); const cjs = dirname(target(root.require.types)); + // The walk below compares two directories, so it disarms ITSELF if they are + // ever the same one: `dirname` would collapse both sides and every + // comparison would pass vacuously — including the case where `require.types` + // was moved back into the ESM tree, which is #698 undone and precisely what + // this exists to notice. + assert.notEqual( + esm, + cjs, + 'both conditions resolve into one directory, so the comparison below ' + + 'would be comparing that directory with itself' + ); const walk = (dir, base = dir, out = []) => { for (const entry of readdirSync(dir, { withFileTypes: true })) { const full = join(dir, entry.name);