H-A-491: the build refuses built files that import an undeclared package
The forge's bundle met "Could not resolve blakejs" in the SDK's seal code. blakejs and tweetnacl are declared dependencies already. The build now runs a check after tsc: every bare import in dist/**/*.js must be a Node built-in, the package itself, or a declared dependency or peerDependency. The builder runs npm run build before npm pack, so a package that breaks this is never published. The check lives under src because the builder's workspace holds only the artifact's paths. Builds: design 47 H2 H-A-491
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"build": "tsc && node src/declared-imports.mjs",
|
||||
"pretest": "tsc --noEmit",
|
||||
"test": "node --test --experimental-strip-types 'test/*.test.ts'",
|
||||
"prepare": "tsc"
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// The package's check that its built files import only what it declares (design 47 H2, H-A-491): every bare import in
|
||||
// dist/**/*.js is a Node built-in, the package itself, or a package its package.json names under dependencies or
|
||||
// peerDependencies. The build runs it after tsc, so a build whose files import an undeclared package fails before
|
||||
// npm pack, and the builder never publishes it: an importer's bundle never meets "Could not resolve".
|
||||
//
|
||||
// It lives under src because the builder's workspace holds only the artifact's paths; tsc does not compile it and the
|
||||
// published files (dist) do not carry it.
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import { builtinModules } from "node:module";
|
||||
import { join, relative } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import ts from "typescript";
|
||||
|
||||
const builtins = new Set(builtinModules);
|
||||
|
||||
// packageOf is the package a bare specifier names: its first segment, or its first two for a scoped one.
|
||||
function packageOf(spec) {
|
||||
const parts = spec.split("/");
|
||||
return spec.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
||||
}
|
||||
|
||||
function jsFiles(dir) {
|
||||
const out = [];
|
||||
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
||||
const p = join(dir, e.name);
|
||||
if (e.isDirectory()) out.push(...jsFiles(p));
|
||||
else if (e.name.endsWith(".js")) out.push(p);
|
||||
}
|
||||
return out.sort();
|
||||
}
|
||||
|
||||
// undeclared answers, for the package at root, each import of its dist/**/*.js that names a package its package.json
|
||||
// does not declare, as "file: specifier"; none when every import resolves to a declared package or a built-in.
|
||||
export function undeclared(root) {
|
||||
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
||||
const declared = new Set([pkg.name, ...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {})]);
|
||||
const found = [];
|
||||
for (const file of jsFiles(join(root, "dist"))) {
|
||||
const { importedFiles } = ts.preProcessFile(readFileSync(file, "utf8"), true, true);
|
||||
for (const { fileName: spec } of importedFiles) {
|
||||
if (spec.startsWith(".") || spec.startsWith("/") || spec.startsWith("node:") || builtins.has(spec)) continue;
|
||||
if (builtins.has(packageOf(spec)) || declared.has(packageOf(spec))) continue;
|
||||
found.push(`${relative(root, file)}: ${spec}`);
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||||
const root = join(fileURLToPath(new URL(".", import.meta.url)), "..");
|
||||
const found = undeclared(root);
|
||||
if (found.length > 0) {
|
||||
console.error("the built files import packages package.json does not declare under dependencies (H-A-491):");
|
||||
for (const f of found) console.error(" " + f);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { undeclared } from "../src/declared-imports.mjs";
|
||||
|
||||
// The package's built files import only what it declares (design 47 H2, H-A-491): the SDK itself passes, and a built
|
||||
// file importing a package package.json leaves out is named, as the forge's bundle met blakejs.
|
||||
test("every bare import of the built files is a declared dependency or a built-in", () => {
|
||||
assert.deepEqual(undeclared(join(import.meta.dirname, "..")), []);
|
||||
|
||||
const root = mkdtempSync(join(tmpdir(), "declared-"));
|
||||
try {
|
||||
mkdirSync(join(root, "dist", "seal"), { recursive: true });
|
||||
writeFileSync(join(root, "package.json"), JSON.stringify({ name: "@novox/x", dependencies: { tweetnacl: "1.0.3" }, devDependencies: { blakejs: "1.2.1" } }));
|
||||
writeFileSync(join(root, "dist", "seal", "index.js"), [
|
||||
'import nacl from "tweetnacl";',
|
||||
'import blakejs from "blakejs";',
|
||||
'import { createHash } from "node:crypto";',
|
||||
'import fs from "fs/promises";',
|
||||
'export * from "./other.js";',
|
||||
'export { a } from "@novox/x/tools";',
|
||||
'const m = await import("@scope/lazy/sub");',
|
||||
].join("\n"));
|
||||
assert.deepEqual(undeclared(root), ["dist/seal/index.js: blakejs", "dist/seal/index.js: @scope/lazy/sub"]);
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user