build: declare workspace dependencies and check them in CI (P0.2)

Packages relied on npm hoisting for imports they never declared, so a
package installed on its own fails (UF-024).

- server: + @waggle/marketplace, @waggle/sdk, @waggle/weaver, zod (MIT),
  @anthropic-ai/sdk (MIT), @ax-llm/ax (Apache-2.0);
  - @waggle/hive-mind-shim-core and stripe (unused)
- agent: + pdf-parse (Apache-2.0); playwright-core (Apache-2.0) as an
  optional peer, since it is loaded only when available
- worker: + @waggle/waggle-dance; web: + @waggle/shared
- launcher: + @waggle/server as a dev dependency (bundled by tsup);
  - stripe; cli: - @waggle/weaver (unused); root: - stripe

scripts/check-workspace-deps.mjs (npm run check:deps) compares the
imports in each community-edition package's src/ with its manifest and
runs in CI. Two imports wait for P1.4 (optimizer, evolution wiring).
This commit is contained in:
shumeiko.it
2026-10-15 19:31:50 +02:00
parent fe2eb0f648
commit d2b6506f8b
12 changed files with 179 additions and 11 deletions

View File

@@ -0,0 +1,126 @@
#!/usr/bin/env node
/**
* Fails when a workspace package imports a package it does not declare.
*
* npm hoists every dependency to the root node_modules, so an undeclared
* import still works inside the monorepo and only breaks once the package is
* installed on its own. This check reads the runtime sources (`src/`) of each
* community-edition package and compares the bare import specifiers with the
* package's dependencies, devDependencies, peerDependencies and
* optionalDependencies.
*
* Usage: node scripts/check-workspace-deps.mjs
*/
import fs from 'node:fs';
import path from 'node:path';
import { builtinModules } from 'node:module';
const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..');
// Packages leaving the community edition are not checked (AGENTS.md, section 2).
const EXCLUDED = new Set([
'apps/www',
'packages/admin-web',
'packages/memory-mcp',
'packages/optimizer',
'packages/wiki-compiler',
]);
const EXCLUDED_PREFIX = 'packages/hive-mind-';
// Known undeclared imports that a work package removes. Each entry needs a reason.
const PENDING = {
'packages/agent': {
'@ax-llm/ax': 'evolution-llm-wiring.ts is removed in P1.4 (ADR-0004)',
},
'packages/server': {
'@waggle/optimizer': 'optimizer-service.ts is removed in P1.4 (ADR-0004)',
},
};
const SOURCE_EXT = /\.(?:[cm]?[jt]sx?)$/;
// Valid npm package names, so specifier-like text inside strings is ignored.
const PACKAGE_NAME = /^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/i;
const BUILTINS = new Set(builtinModules);
function workspaceDirs() {
const dirs = [];
for (const parent of ['apps', 'packages']) {
for (const name of fs.readdirSync(path.join(ROOT, parent))) {
const rel = `${parent}/${name}`;
if (EXCLUDED.has(rel) || rel.startsWith(EXCLUDED_PREFIX)) continue;
if (fs.existsSync(path.join(ROOT, rel, 'package.json'))) dirs.push(rel);
}
}
return dirs.sort();
}
function sourceFiles(dir) {
const out = [];
const walk = (d) => {
for (const entry of fs.readdirSync(d, { withFileTypes: true })) {
if (entry.name === 'node_modules' || entry.name === 'dist') continue;
const full = path.join(d, entry.name);
if (entry.isDirectory()) walk(full);
else if (SOURCE_EXT.test(entry.name) && !/\.(?:test|spec)\./.test(entry.name)) out.push(full);
}
};
if (fs.existsSync(dir)) walk(dir);
return out;
}
function stripComments(code) {
return code
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/(^|[^:'"`\\])\/\/.*$/gm, '$1');
}
const SPECIFIER_PATTERNS = [
/\bfrom\s*['"]([^'"\n]+)['"]/g,
/\bimport\s*['"]([^'"\n]+)['"]/g,
/\bimport\s*\(\s*['"]([^'"\n]+)['"]\s*\)/g,
/\brequire\s*\(\s*['"]([^'"\n]+)['"]\s*\)/g,
];
function packageName(specifier) {
if (specifier.startsWith('.') || specifier.startsWith('/')) return null;
if (specifier.startsWith('node:') || specifier.startsWith('@/') || specifier.startsWith('virtual:')) return null;
const parts = specifier.split('/');
const name = specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
if (BUILTINS.has(name) || !PACKAGE_NAME.test(name)) return null;
return name;
}
let failures = 0;
for (const rel of workspaceDirs()) {
const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, rel, 'package.json'), 'utf8'));
const declared = new Set([
manifest.name,
...Object.keys(manifest.dependencies ?? {}),
...Object.keys(manifest.devDependencies ?? {}),
...Object.keys(manifest.peerDependencies ?? {}),
...Object.keys(manifest.optionalDependencies ?? {}),
]);
const pending = PENDING[rel] ?? {};
const missing = new Map();
for (const file of sourceFiles(path.join(ROOT, rel, 'src'))) {
const code = stripComments(fs.readFileSync(file, 'utf8'));
for (const pattern of SPECIFIER_PATTERNS) {
for (const match of code.matchAll(pattern)) {
const name = packageName(match[1]);
if (!name || declared.has(name) || declared.has(`@types/${name}`) || name in pending) continue;
if (!missing.has(name)) missing.set(name, path.relative(ROOT, file));
}
}
}
if (missing.size > 0) {
failures += missing.size;
console.error(`${rel} (${manifest.name}) imports undeclared packages:`);
for (const [name, file] of missing) console.error(` ${name} (first seen in ${file})`);
}
}
if (failures > 0) {
console.error(`\n${failures} undeclared import(s). Declare them in the package's package.json.`);
process.exit(1);
}
console.log('All workspace imports are declared.');