Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: stop barrel tests serving a stale dynamic-import map from the warm Vitest cache (#13087)

Adam Eivy8db8daa3cd76
diff --git a/server/lib/dynamicImportGlobGuard.test.js b/server/lib/dynamicImportGlobGuard.test.js
new file mode 100644
index 0000000000..05ef3fad86
--- /dev/null
+++ b/server/lib/dynamicImportGlobGuard.test.js
@@ -0,0 +1,41 @@
+import { describe, it, expect } from 'vitest';
+import { readdirSync, readFileSync, statSync } from 'node:fs';
+import { dirname, join, relative } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+// Vite compiles a template `import(`./dir/${name}.js`)` into a glob map of the
+// files present at TRANSFORM time. Vitest's fs module cache keys that
+// transform on the importing file's own content, so a module added to the
+// directory later makes a warm-cache run throw "Unknown variable dynamic
+// import" while a cold CI run passes (#13087). Callers must import by file URL
+// with `/* @vite-ignore */` (see checkInfraBarrel.test.js) or use literals.
+const SERVER_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
+const SKIP_DIRS = new Set(['node_modules', 'data', 'coverage', '.cache']);
+
+// Matches `import(` + template literal with an interpolation, but not data: URLs
+// (those are not file lookups) and not calls already marked @vite-ignore.
+const FILE_GLOB_IMPORT = /\bimport\(\s*(?!\/\*\s*@vite-ignore)`(?!data:)[^`]*\$\{/;
+
+const listSources = (dir) => readdirSync(dir).flatMap((name) => {
+ if (SKIP_DIRS.has(name) || name.startsWith('.')) return [];
+ const full = join(dir, name);
+ if (statSync(full).isDirectory()) return listSources(full);
+ return name.endsWith('.js') ? [full] : [];
+});
+
+describe('template dynamic imports over a directory (#13087)', () => {
+ it('matcher flags relative-path templates and ignores data URLs and @vite-ignore', () => {
+ expect(FILE_GLOB_IMPORT.test('await import(`./x/${f}.js`)')).toBe(true);
+ expect(FILE_GLOB_IMPORT.test('import(`data:text/javascript;base64,${s}`)')).toBe(false);
+ expect(FILE_GLOB_IMPORT.test('import(/* @vite-ignore */ `./x/${f}.js`)')).toBe(false);
+ expect(FILE_GLOB_IMPORT.test('import(`./literal.js`)')).toBe(false);
+ });
+
+ it('no server source resolves a directory through a template import()', () => {
+ const offenders = listSources(SERVER_ROOT)
+ .filter((file) => file !== fileURLToPath(import.meta.url))
+ .filter((file) => FILE_GLOB_IMPORT.test(readFileSync(file, 'utf8').replace(/^\s*\/\/.*$/gm, '')))
+ .map((file) => relative(SERVER_ROOT, file));
+ expect(offenders).toEqual([]);
+ });
+});
diff --git a/server/lib/editorial/checkInfraBarrel.test.js b/server/lib/editorial/checkInfraBarrel.test.js
index 72a37a8bc9..9b348faa0b 100644
--- a/server/lib/editorial/checkInfraBarrel.test.js
+++ b/server/lib/editorial/checkInfraBarrel.test.js
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import { readdirSync, readFileSync } from 'node:fs';
-import { fileURLToPath } from 'node:url';
+import { fileURLToPath, pathToFileURL } from 'node:url';
import { dirname, join } from 'node:path';
import * as barrel from './checkInfra.js';
@@ -11,6 +11,11 @@ const MODULE_FILES = readdirSync(join(HERE, 'checkInfra'))
.filter((f) => f.endsWith('.js') && !f.endsWith('.test.js'))
.sort();
+// A template `import()` with a literal prefix is compiled by Vite into a glob map frozen at transform
+// time, and the fs module cache keys it on THIS file alone, so a newly added module throws "Unknown
+// variable dynamic import" until the cache is cleared (#13087). A file URL skips that analysis.
+const importModule = (f) => import(/* @vite-ignore */ pathToFileURL(join(HERE, 'checkInfra', f)).href);
+
// Issue #2842 split the 2,635-line checkInfra.js into ./checkInfra/* with the
// original file kept as a re-exporting barrel (the same treatment #1152 gave
// arcPlanner.js). This pins that contract: every module export must be
@@ -26,7 +31,7 @@ describe('checkInfra barrel re-exports (issue #2842)', () => {
});
it.each(MODULE_FILES)('%s exports are reachable from the barrel as the same objects', async (f) => {
- const mod = await import(`./checkInfra/${f.slice(0, -3)}.js`);
+ const mod = await importModule(f);
const keys = Object.keys(mod);
expect(keys.length, `${f} exports nothing`).toBeGreaterThan(0);
for (const key of keys) {
@@ -38,7 +43,7 @@ describe('checkInfra barrel re-exports (issue #2842)', () => {
const seen = new Map();
const collisions = [];
for (const f of MODULE_FILES) {
- const mod = await import(`./checkInfra/${f.slice(0, -3)}.js`);
+ const mod = await importModule(f);
for (const key of Object.keys(mod)) {
if (seen.has(key) && seen.get(key).value !== mod[key]) {
collisions.push(`${key}: ${seen.get(key).file} vs ${f}`);
diff --git a/server/services/layeredIntelligenceBarrel.test.js b/server/services/layeredIntelligenceBarrel.test.js
index 86e00a0e9e..acb05d3630 100644
--- a/server/services/layeredIntelligenceBarrel.test.js
+++ b/server/services/layeredIntelligenceBarrel.test.js
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import { readdirSync, readFileSync } from 'node:fs';
-import { fileURLToPath } from 'node:url';
+import { fileURLToPath, pathToFileURL } from 'node:url';
import { dirname, join } from 'node:path';
import * as barrel from './layeredIntelligence.js';
@@ -11,6 +11,11 @@ const MODULE_FILES = readdirSync(join(HERE, 'layeredIntelligence'))
.filter((f) => f.endsWith('.js') && !f.endsWith('.test.js'))
.sort();
+// A template `import()` with a literal prefix is compiled by Vite into a glob map frozen at transform
+// time, and the fs module cache keys it on THIS file alone, so a newly added module throws "Unknown
+// variable dynamic import" until the cache is cleared (#13087). A file URL skips that analysis.
+const importModule = (f) => import(/* @vite-ignore */ pathToFileURL(join(HERE, 'layeredIntelligence', f)).href);
+
// Issue #2842 split the 2,631-line layeredIntelligence.js into
// ./layeredIntelligence/* with the original file kept as a re-exporting barrel
// (the same treatment #1152 gave arcPlanner.js). This pins that contract so the
@@ -24,7 +29,7 @@ describe('layeredIntelligence barrel re-exports (issue #2842)', () => {
});
it.each(MODULE_FILES)('%s exports are reachable from the barrel as the same objects', async (f) => {
- const mod = await import(`./layeredIntelligence/${f.slice(0, -3)}.js`);
+ const mod = await importModule(f);
const keys = Object.keys(mod);
expect(keys.length, `${f} exports nothing`).toBeGreaterThan(0);
for (const key of keys) {
@@ -36,7 +41,7 @@ describe('layeredIntelligence barrel re-exports (issue #2842)', () => {
const seen = new Map();
const collisions = [];
for (const f of MODULE_FILES) {
- const mod = await import(`./layeredIntelligence/${f.slice(0, -3)}.js`);
+ const mod = await importModule(f);
for (const key of Object.keys(mod)) {
if (seen.has(key) && seen.get(key).value !== mod[key]) {
collisions.push(`${key}: ${seen.get(key).file} vs ${f}`);
diff --git a/server/services/pipeline/seriesAutopilotBarrel.test.js b/server/services/pipeline/seriesAutopilotBarrel.test.js
index bf1dc6be3d..bbb424e52f 100644
--- a/server/services/pipeline/seriesAutopilotBarrel.test.js
+++ b/server/services/pipeline/seriesAutopilotBarrel.test.js
@@ -1,6 +1,6 @@
import { describe, it, expect, vi } from 'vitest';
import { readdirSync, readFileSync } from 'node:fs';
-import { fileURLToPath } from 'node:url';
+import { fileURLToPath, pathToFileURL } from 'node:url';
import { dirname, join } from 'node:path';
import { mockNoPeerSync, mockNoPeers } from '../../lib/mockPathsDataRoot.js';
@@ -18,6 +18,11 @@ const MODULE_FILES = readdirSync(join(HERE, 'seriesAutopilot'))
.filter((f) => f.endsWith('.js') && !f.endsWith('.test.js'))
.sort();
+// A template `import()` with a literal prefix is compiled by Vite into a glob map frozen at transform
+// time, and the fs module cache keys it on THIS file alone, so a newly added module throws "Unknown
+// variable dynamic import" until the cache is cleared (#13087). A file URL skips that analysis.
+const importModule = (f) => import(/* @vite-ignore */ pathToFileURL(join(HERE, 'seriesAutopilot', f)).href);
+
// Issue #2842 split the 2,541-line seriesAutopilot.js into ./seriesAutopilot/*
// with the original file kept as a re-exporting barrel (the same treatment
// #1152 gave arcPlanner.js). This pins that contract so the routes, the CDO
@@ -31,7 +36,7 @@ describe('seriesAutopilot barrel re-exports (issue #2842)', () => {
});
it.each(MODULE_FILES)('%s exports are reachable from the barrel as the same objects', async (f) => {
- const mod = await import(`./seriesAutopilot/${f.slice(0, -3)}.js`);
+ const mod = await importModule(f);
const keys = Object.keys(mod);
expect(keys.length, `${f} exports nothing`).toBeGreaterThan(0);
for (const key of keys) {
@@ -43,7 +48,7 @@ describe('seriesAutopilot barrel re-exports (issue #2842)', () => {
const seen = new Map();
const collisions = [];
for (const f of MODULE_FILES) {
- const mod = await import(`./seriesAutopilot/${f.slice(0, -3)}.js`);
+ const mod = await importModule(f);
for (const key of Object.keys(mod)) {
if (seen.has(key) && seen.get(key).value !== mod[key]) {
collisions.push(`${key}: ${seen.get(key).file} vs ${f}`);