Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Merge batch 11-03 b2215: #2840 (#2842)

0xken15f398a95594d6
diff --git a/backend/src/lib/devImports.test-support.ts b/backend/src/lib/devImports.test-support.ts
index 0b5b565c0..60c20637f 100644
--- a/backend/src/lib/devImports.test-support.ts
+++ b/backend/src/lib/devImports.test-support.ts
@@ -1,12 +1,24 @@
/**
- * The devDependencies a source file imports (productionBuild.test.ts): `from 'x'` (import or export), a bare `import 'x'`, and
- * `import('x')`, each with any subpath. `import type` is skipped: the build erases it. A text scan, so an import written in a
- * comment counts too: shipped files keep test-only package names out altogether.
+ * What a shipped backend source file imports, for productionBuild.test.ts: every specifier it loads, in each form, `from 'x'`
+ * (import or export), a bare `import 'x'`, `import('x')` (quotes or a plain template literal), `require('x')`, TypeScript's
+ * `import x = require('x')` and `createRequire(...)('x')`. Type-only imports and exports, one line or several, are left
+ * out first: the build erases them. A text scan, so a specifier written in a comment counts too; shipped files keep test-only
+ * names out altogether.
*/
+const TYPE_ONLY = /\b(?:import|export)\s+type\s*\{[^}]*\}\s*from\s*(['"])[^'"\n]+\1|\bimport\s+type\s+[\w$]+\s+from\s*(['"])[^'"\n]+\2/g
+const LOADS = /(?:\bfrom\s*|\bimport\s*\(?\s*|\brequire\s*\(\s*|\bcreateRequire\s*\([^()]*\)\s*\(\s*)(['"`])([^'"`\n$]+)\1/g
+
+export function importedSpecifiers(source: string): string[] {
+ return [...source.replace(TYPE_ONLY, '').matchAll(LOADS)].map(match => match[2]!)
+}
+
+/** The devDependencies among them, each by its package name (a subpath counts as its package). */
export function devImports(source: string, devDependencies: readonly string[]): string[] {
- const specifiers = [...source.matchAll(/(?:\bfrom\s*|\bimport\s*\(?\s*)(['"])([^'"\n]+)\1/g)]
- .filter(match => !/\bimport\s+type\b[^;\n]*$/.test(source.slice(source.lastIndexOf('\n', match.index) + 1, match.index)))
- .map(match => match[2]!)
const packageOf = (specifier: string) => specifier.split('/').slice(0, specifier.startsWith('@') ? 2 : 1).join('/')
- return [...new Set(specifiers.map(packageOf).filter(name => devDependencies.includes(name)))]
+ return [...new Set(importedSpecifiers(source).map(packageOf).filter(name => devDependencies.includes(name)))]
+}
+
+/** The test and test-support files among them, which build.mjs does not ship (*.test.ts, *.test-support.ts). */
+export function testFileImports(source: string): string[] {
+ return [...new Set(importedSpecifiers(source).filter(specifier => /\.test(?:-support)?(?:\.[jt]s)?$/.test(specifier)))]
}
diff --git a/backend/src/lib/productionBuild.test.ts b/backend/src/lib/productionBuild.test.ts
index 770c65c99..ffc03a9ba 100644
--- a/backend/src/lib/productionBuild.test.ts
+++ b/backend/src/lib/productionBuild.test.ts
@@ -2,7 +2,7 @@ import { readdirSync, readFileSync } from 'node:fs'
import { join, relative } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
-import { devImports } from './devImports.test-support.js'
+import { devImports, testFileImports } from './devImports.test-support.js'
// build.mjs ships every .ts under src except *.test.ts and *.test-support.ts into dist, and production installs no
// devDependency. Code that imports one (Vitest, the in-memory MongoDB, ...) must carry one of those names (reviews of #2804
@@ -21,6 +21,11 @@ describe('the production build', () => {
expect(testOnly).toEqual([])
})
+ it('ships no file that imports a test or test-support file, which the build leaves out', () => {
+ const shipped = files(src).filter(path => !path.endsWith('.test.ts') && !path.endsWith('.test-support.ts'))
+ expect(shipped.flatMap(path => testFileImports(readFileSync(path, 'utf8')).map(name => `${relative(src, path)}: ${name}`))).toEqual([])
+ })
+
it('finds every form of import of a devDependency, and none that is erased or another package', () => {
const dev = ['vitest', 'mongodb-memory-server-core', 'mongodb', '@vitest/coverage-v8']
expect(devImports(`import { it } from 'vitest'`, dev)).toEqual(['vitest'])
@@ -31,5 +36,23 @@ describe('the production build', () => {
expect(devImports(`import type { Db } from 'mongodb'`, dev)).toEqual([])
expect(devImports(`import { type Db } from 'mongodb-extra'\nimport { PrismaClient } from '@prisma/client'`, dev)).toEqual([])
expect(devImports(`// import 'vitest' in a comment is still flagged: keep test-only words out of shipped files`, dev)).toEqual(['vitest'])
+ // require-style forms and a template-literal import (review of #2836)
+ expect(devImports(`const v = require('vitest')`, dev)).toEqual(['vitest'])
+ expect(devImports(`const m = createRequire(import.meta.url)('mongodb')`, dev)).toEqual(['mongodb'])
+ expect(devImports(`import m = require('mongodb')`, dev)).toEqual(['mongodb'])
+ expect(devImports('const m = await import(`mongodb-memory-server-core`)', dev)).toEqual(['mongodb-memory-server-core'])
+ // type-only imports and exports the build erases, over several lines too
+ expect(devImports(`import type {\n Db,\n MongoClient,\n} from 'mongodb'`, dev)).toEqual([])
+ expect(devImports(`export type { Db } from 'mongodb'\nexport type {\n Suite,\n} from 'vitest'`, dev)).toEqual([])
+ expect(devImports(`import type {\n Db,\n} from 'mongodb'\nimport { it } from 'vitest'`, dev)).toEqual(['vitest'])
+ })
+
+ it('finds an import of a test or test-support file in every form', () => {
+ expect(testFileImports(`import { MemoryStamps } from '../superTabs/cache.test-support.js'`)).toEqual(['../superTabs/cache.test-support.js'])
+ expect(testFileImports(`const f = await import('./fixtures.test-support')`)).toEqual(['./fixtures.test-support'])
+ expect(testFileImports(`export * from './shards.test.js'`)).toEqual(['./shards.test.js'])
+ expect(testFileImports(`const x = require('./x.test-support.js')`)).toEqual(['./x.test-support.js'])
+ expect(testFileImports(`import type { Seed } from './seed.test-support.js'`)).toEqual([])
+ expect(testFileImports(`import { tested } from './tested.js'\nimport { testSupport } from './test-support-names.js'`)).toEqual([])
})
})