An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): importUserModule's fresh read sees an edited ES module .js (#10745)
Christopher Chedeau1a4a85dade42
diff --git a/.changeset/fresh-reload-follows-node.md b/.changeset/fresh-reload-follows-node.mdnew file mode 100644index 0000000000..003a80547f--- /dev/null+++ b/.changeset/fresh-reload-follows-node.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] A command that reads a project module again after changing it, such as `astryx integration add` checking the manifest it just wrote, now reads the change for an ES module `.js` file in a package without a `"type"` field. It read the module as first loaded.++@vjeuxdiff --git a/packages/cli/foundation/doc-compiler/doc-loads.test.mjs b/packages/cli/foundation/doc-compiler/doc-loads.test.mjsindex 6bf62718a2..eb240f6251 100644--- a/packages/cli/foundation/doc-compiler/doc-loads.test.mjs+++ b/packages/cli/foundation/doc-compiler/doc-loads.test.mjs@@ -373,12 +373,11 @@ const RUNNERS = {sites: ['import(<computed>)'],},'foundation/fs/module-loader.mjs': {- runs: 'user modules (config, manifests, handlers, codemods): natively, through jiti, or by require for a fresh CommonJS read',+ runs: 'user modules (config, manifests, handlers, codemods): natively or through jiti, and a fresh read again after dropping a CommonJS module from the require cache',sites: ['.import() ×3','createJiti ×3',- 'import(<computed>)',- 'require(<computed>)',+ 'import(<computed>) ×2','require.cache',],},diff --git a/packages/cli/foundation/fs/module-loader.mjs b/packages/cli/foundation/fs/module-loader.mjsindex c337211439..e255097ab2 100644--- a/packages/cli/foundation/fs/module-loader.mjs+++ b/packages/cli/foundation/fs/module-loader.mjs@@ -40,30 +40,6 @@ function getJiti() {return jitiInstance;}-/**- * Node treats `.js` as ESM only inside the nearest package scope whose- * package.json declares `"type": "module"`; no package.json defaults to- * CommonJS.- * @param {string} file- * @returns {boolean}- */-function isCommonJsFile(file) {- let dir = path.dirname(file);- for (;;) {- const packageJson = path.join(dir, 'package.json');- if (fs.existsSync(packageJson)) {- try {- return JSON.parse(fs.readFileSync(packageJson, 'utf-8')).type !== 'module';- } catch {- return true;- }- }- const parent = path.dirname(dir);- if (parent === dir) return true;- dir = parent;- }-}-let loadsInFlight = 0;/** @type {typeof process.stdout.write | undefined} */let realStdoutWrite;@@ -104,9 +80,9 @@ async function withStdoutOnStderr(load) {/*** Import a user-authored module. `.ts` is loaded via jiti; `.mjs`/`.js` use* native dynamic import for ordinary cached reads. A fresh `.ts` read uses a- * no-cache jiti instance. A fresh CommonJS `.js` read evicts and reloads through- * `require`; an ESM `.js` or `.mjs` read uses a cache-busted file URL. The- * nearest package.json `type` decides `.js`, matching Node's package scopes.+ * no-cache jiti instance. Any other fresh read drops the module from the+ * require cache and imports a cache-busted file URL. Node then chooses its+ * format as for the first read, so a fresh read gives what a first read gives.* Normal loads retain module caching. `fresh` rereads a file that changed* during this process: one a codemod rewrote, or an edited doc file (the doc* importers in ../doc-compiler/import.mjs read each change once).@@ -251,31 +227,64 @@ async function importModule(file, fresh) {if (fresh && file.endsWith('.ts')) {return await createJiti(import.meta.url, {moduleCache: false}).import(file);}- if (fresh && file.endsWith('.js') && isCommonJsFile(file)) {- const resolved = requireFromHere.resolve(file);- delete requireFromHere.cache[resolved];- const value = requireFromHere(file);- if (- value != null &&- typeof value === 'object' &&- value.__esModule === true &&- Object.prototype.hasOwnProperty.call(value, 'default')- ) {- return value;- }- return {- ...(value != null && typeof value === 'object' ? value : {}),- default: value,- };- }if (file.endsWith('.ts')) {return await getJiti().import(file);}const url = pathToFileURL(file);- if (fresh) {- url.searchParams.set('astryx-fresh', String(++freshImportNonce));+ if (!fresh) return await readInTurn(file, () => import(url.href));+ // Node chooses the format again, as for the first import: a package's+ // "type", the extension, or the source's syntax. The query names a new ES+ // module. A CommonJS module also needs its cache entry gone.+ url.searchParams.set('astryx-fresh', String(++freshImportNonce));+ return await readInTurn(file, () => {+ delete requireFromHere.cache[cacheKey(file)];+ return import(url.href);+ });+}++/**+ * The name Node's CommonJS cache gives `file`: its real path, so a link and+ * its target name one entry. A file that does not resolve keeps its path.+ * @param {string} file absolute path+ * @returns {string}+ */+function cacheKey(file) {+ try {+ return requireFromHere.resolve(file);+ } catch {+ return file;}- return await import(url.href);+}++/**+ * Each file's latest read, by its cache name, settled once that read ends.+ * @type {Map<string, Promise<void>>}+ */+const readsInProgress = new Map();++/**+ * Run `read`, an import of `file`, once the imports of `file` started before+ * it have ended. Imports of other files do not wait. A fresh read removes the+ * file from Node's CommonJS cache. A CommonJS import still in progress needs+ * that entry until it ends.+ * @template T+ * @param {string} file absolute path+ * @param {() => Promise<T>} read+ * @returns {Promise<T>}+ */+export function readInTurn(file, read) {+ const key = cacheKey(file);+ const before = readsInProgress.get(key) ?? Promise.resolve();+ const result = before.then(read);+ const ended = result.then(+ () => {},+ () => {},+ );+ readsInProgress.set(key, ended);+ void ended.then(() => {+ if (readsInProgress.get(key) === ended) readsInProgress.delete(key);+ });+ return result;}diff --git a/packages/cli/foundation/fs/module-loader.test.mjs b/packages/cli/foundation/fs/module-loader.test.mjsindex 26dfa18df8..3a96fe7307 100644--- a/packages/cli/foundation/fs/module-loader.test.mjs+++ b/packages/cli/foundation/fs/module-loader.test.mjs@@ -81,8 +81,13 @@ describe('importUserModule', () => {},);- const probeFreshJavaScript = (dir, firstSource, secondSource) => {- const file = path.join(dir, 'fresh.js');+ const probeFreshJavaScript = (+ dir,+ firstSource,+ secondSource,+ name = 'fresh.js',+ ) => {+ const file = path.join(dir, name);fs.writeFileSync(file, firstSource);const loaderUrl = pathToFileURL(path.join(process.cwd(), 'packages/cli/foundation/fs/module-loader.mjs'),@@ -97,7 +102,8 @@ import {importUserModule} from ${JSON.stringify(loaderUrl)};const first = await importUserModule(process.env.TEST_MODULE);fs.writeFileSync(process.env.TEST_MODULE, ${JSON.stringify(secondSource)});const fresh = await importUserModule(process.env.TEST_MODULE, {fresh: true});-process.stdout.write(JSON.stringify({first: first.default?.answer, fresh: fresh.default?.answer}));`,+const sameExports = Object.keys(first).sort().join() === Object.keys(fresh).sort().join();+process.stdout.write(JSON.stringify({first: first.default?.answer, fresh: fresh.default?.answer, sameExports}));`,],{encoding: 'utf-8',@@ -120,7 +126,7 @@ process.stdout.write(JSON.stringify({first: first.default?.answer, fresh: fresh.`module.exports = {answer: 1};\n`,`module.exports = {answer: 2};\n`,),- ).toEqual({first: 1, fresh: 2});+ ).toEqual({first: 1, fresh: 2, sameExports: true});});it('freshly reloads a changed ESM .js module in a real Node process', () => {@@ -136,9 +142,106 @@ process.stdout.write(JSON.stringify({first: first.default?.answer, fresh: fresh.`export default {answer: 1};\n`,`export default {answer: 2};\n`,),- ).toEqual({first: 1, fresh: 2});+ ).toEqual({first: 1, fresh: 2, sameExports: true});});+ it('freshly reloads a changed ES module .js in a package without a "type", in a real Node process', () => {+ const typelessDir = path.join(tmpDir, 'typeless');+ fs.mkdirSync(typelessDir);+ fs.writeFileSync(+ path.join(typelessDir, 'package.json'),+ JSON.stringify({name: 'typeless'}),+ );+ expect(+ probeFreshJavaScript(+ typelessDir,+ `export default {answer: 1};\n`,+ `export default {answer: 2};\n`,+ ),+ ).toEqual({first: 1, fresh: 2, sameExports: true});+ });++ it.each([+ [+ 'fresh.cjs',+ `module.exports = {answer: 1};\n`,+ `module.exports = {answer: 2};\n`,+ ],+ [+ 'fresh.mjs',+ `export default {answer: 1};\n`,+ `export default {answer: 2};\n`,+ ],+ ])(+ 'freshly reloads a changed %s in a real Node process',+ (name, firstSource, secondSource) => {+ const kindDir = path.join(tmpDir, name.replace('.', '-'));+ fs.mkdirSync(kindDir);+ fs.writeFileSync(+ path.join(kindDir, 'package.json'),+ JSON.stringify({name: 'typeless'}),+ );+ expect(+ probeFreshJavaScript(kindDir, firstSource, secondSource, name),+ ).toEqual({first: 1, fresh: 2, sameExports: true});+ },+ );++ it.each([+ ['in a CommonJS package', {type: 'commonjs'}],+ ['in a package without a "type"', {name: 'typeless'}],+ ])(+ 'reads one CommonJS module in two reads at once, first or fresh, %s, in a real Node process',+ (_where, packageJson) => {+ const dir = path.join(tmpDir, 'at-once');+ fs.mkdirSync(dir);+ fs.writeFileSync(+ path.join(dir, 'package.json'),+ JSON.stringify(packageJson),+ );+ const file = path.join(dir, 'm.js');+ const loaderUrl = pathToFileURL(+ path.join(+ process.cwd(),+ 'packages/cli/foundation/fs/module-loader.mjs',+ ),+ ).href;+ const output = execFileSync(+ process.execPath,+ [+ '--input-type=module',+ '-e',+ `import fs from 'node:fs';+import {importUserModule} from ${JSON.stringify(loaderUrl)};+const file = process.env.TEST_MODULE;+const read = async reads => {+ try {+ return (await Promise.all(reads())).map(mod => mod.default.answer).join();+ } catch (error) {+ return error.code ?? error.name;+ }+};+fs.writeFileSync(file, 'module.exports = {answer: 1};\\n');+const firstAndFresh = await read(() => [importUserModule(file), importUserModule(file, {fresh: true})]);+fs.writeFileSync(file, 'module.exports = {answer: 2};\\n');+const twoFresh = await read(() => [importUserModule(file, {fresh: true}), importUserModule(file, {fresh: true})]);+const target = file.replace(/m\\.js$/, 'target.js');+const link = file.replace(/m\\.js$/, 'link.js');+fs.writeFileSync(target, 'module.exports = {answer: 3};\\n');+fs.symlinkSync(target, link);+const linkAndTarget = await read(() => [importUserModule(link), importUserModule(target, {fresh: true})]);+process.stdout.write(JSON.stringify({firstAndFresh, twoFresh, linkAndTarget}));`,+ ],+ {encoding: 'utf-8', env: {...process.env, TEST_MODULE: file}},+ );+ expect(JSON.parse(output)).toEqual({+ firstAndFresh: '1,1',+ twoFresh: '2,2',+ linkAndTarget: '3,3',+ });+ },+ );+it('defaults .js to CommonJS when no package.json exists', () => {const noPackageDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-commonjs-no-package-'),@@ -150,7 +253,7 @@ process.stdout.write(JSON.stringify({first: first.default?.answer, fresh: fresh.`module.exports = {answer: 1};\n`,`module.exports = {answer: 2};\n`,),- ).toEqual({first: 1, fresh: 2});+ ).toEqual({first: 1, fresh: 2, sameExports: true});} finally {fs.rmSync(noPackageDir, {recursive: true, force: true});}