From 3272fd9c6fefb3dbbbb8185207c8a0c012c38e45 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Sat, 7 Nov 2026 22:22:05 -0600 Subject: [PATCH] Let the import scanner accept the plug-in loader's own module import Quick Setup made organization code import the plug-in loader, which loads packages/plugins///index.mjs by a computed path. The scanner now allows exactly that form in the loader file and still refuses it anywhere else. --- packages/cli/tests/business-imports.mjs | 8 +++++++- packages/cli/tests/business-imports.test.mjs | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/cli/tests/business-imports.mjs b/packages/cli/tests/business-imports.mjs index 191ece798..4cc223780 100644 --- a/packages/cli/tests/business-imports.mjs +++ b/packages/cli/tests/business-imports.mjs @@ -36,6 +36,10 @@ const IMPORT=new RegExp([ `${DYNAMIC.source}\\s*['"](\\.{1,2}\\/[^'"]+)['"]\\s*\\)`, ].join('|'),'g'); const DYNAMIC_LITERAL=/^import\s*\(\s*(['"])[^'"\n]*\1\s*\)/; +// The plug-in loader's one computed import, allowed only in that file and only in this exact form. It loads +// packages/plugins///index.mjs after checking that path, and a real copy ships all of +// packages/plugins, so a fixture that never loads a plug-in needs nothing more from it. +const LOADER_FILE='packages/plugins/src/loader.mjs',LOADER_IMPORT=/^import\(pathToFileURL\(expected\)\.href\)/; const REGEX_AFTER='(,=:[!&|?{};+-*%<>~^'; const inert=(literals,index)=>literals.some(([from,to])=>index>=from&&indexreaddirSync(dir,{withFileTypes:true}).flatMap((e)=>e.isDirectory()?sources(join(dir,e.name)):e.name.endsWith('.mjs')?[join(dir,e.name)]:[]); @@ -110,7 +114,9 @@ function specifiers(file,text){ catch(error){throw new Error(`${file} cannot be read for imports: ${error.message}`);} const {code,literals}=scanned,live=(m)=>!inert(literals,m.index); for(const m of code.matchAll(DYNAMIC)){ - if(live(m)&&!DYNAMIC_LITERAL.test(code.slice(m.index,m.index+512)))throw new Error(`${file} has a dynamic import that is not a plain string literal`); + const at=code.slice(m.index,m.index+512); + if(file===LOADER_FILE&&LOADER_IMPORT.test(at))continue; + if(live(m)&&!DYNAMIC_LITERAL.test(at))throw new Error(`${file} has a dynamic import that is not a plain string literal`); } return [...code.matchAll(IMPORT)].filter(live).map((m)=>m[1]??m[2]??m[3]); } diff --git a/packages/cli/tests/business-imports.test.mjs b/packages/cli/tests/business-imports.test.mjs index 0787a1883..dfa4e09b7 100644 --- a/packages/cli/tests/business-imports.test.mjs +++ b/packages/cli/tests/business-imports.test.mjs @@ -29,6 +29,14 @@ test('the real tree gives the plug-in files the loader reads', ()=>{ assert.deepEqual(found,[...found].sort()); }); +test('the loader exception holds only for the loader file and its exact computed import', ()=>{ + const text="const module=await import(pathToFileURL(expected).href);\n"; + const at=(file)=>tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/loader.mjs';\n",[`repo/${file}`]:text}); + assert.deepEqual(businessImportedFiles(at('packages/plugins/src/loader.mjs')),['packages/plugins/src/loader.mjs']); + assert.throws(()=>businessImportedFiles(tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/other.mjs';\n",'repo/packages/plugins/src/other.mjs':text})),/not a plain string literal/); + assert.throws(()=>businessImportedFiles(tree({[`${BIZ}a.mjs`]:"import '../../plugins/src/loader.mjs';\n",'repo/packages/plugins/src/loader.mjs':"await import(x);\n"})),/not a plain string literal/); +}); + test('every static form and a literal dynamic import is found, and the imports of what it finds in turn', ()=>{ const repo=withBusiness([ "import x from '../../other/src/from.mjs';",