-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix(cli): ship scripts/build/runtime-env.mjs in npm package (#5227) #5230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync, readdirSync, statSync } from "node:fs"; | ||
| import { dirname, join, resolve, relative } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| /** | ||
| * Regression guard for #5227. | ||
| * | ||
| * The published npm package only ships the paths listed in package.json's | ||
| * `files` whitelist. If a CLI entrypoint under `bin/` imports a module under | ||
| * `scripts/` at runtime, that script MUST be covered by the whitelist — | ||
| * otherwise a global install (`npm install -g omniroute`) fails at startup with | ||
| * "Cannot find module .../scripts/build/runtime-env.mjs". | ||
| * | ||
| * #5227: `bin/cli/commands/serve.mjs` started importing | ||
| * `scripts/build/runtime-env.mjs` (added with the heap auto-calibration fix | ||
| * #5213) but the file was never added to `files`, breaking every global install. | ||
| */ | ||
|
|
||
| const REPO_ROOT = resolve(fileURLToPath(import.meta.url), "../../../"); | ||
|
|
||
| function listMjsFiles(dir: string): string[] { | ||
| const out: string[] = []; | ||
| for (const entry of readdirSync(dir)) { | ||
| const full = join(dir, entry); | ||
| if (statSync(full).isDirectory()) { | ||
| out.push(...listMjsFiles(full)); | ||
| } else if (entry.endsWith(".mjs") || entry.endsWith(".js")) { | ||
| out.push(full); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** Extract every `scripts/...`-resolving specifier statically/dynamically imported by a bin file. */ | ||
| function scriptImports(binFile: string): string[] { | ||
| const src = readFileSync(binFile, "utf8"); | ||
| const specifiers = new Set<string>(); | ||
| // static: from "..." dynamic: import("...") | ||
| const re = /(?:from|import)\s*\(?\s*["']([^"']+)["']/g; | ||
| let m: RegExpExecArray | null; | ||
| while ((m = re.exec(src))) { | ||
| const spec = m[1]; | ||
| if (!spec.startsWith(".")) continue; // only relative imports can reach scripts/ | ||
| const resolved = resolve(dirname(binFile), spec); | ||
| const rel = relative(REPO_ROOT, resolved).split("\\").join("/"); | ||
| if (rel.startsWith("scripts/")) specifiers.add(rel); | ||
| } | ||
| return [...specifiers]; | ||
| } | ||
|
|
||
| /** A repo-relative path is published if it equals a whitelist entry or sits under a directory entry. */ | ||
| function isCovered(relPath: string, files: string[]): boolean { | ||
| return files.some((entry) => { | ||
| if (entry.startsWith("!")) return false; // negations never add coverage | ||
| if (entry === relPath) return true; | ||
| const dirEntry = entry.endsWith("/") ? entry : entry + "/"; | ||
| return relPath.startsWith(dirEntry); | ||
| }); | ||
| } | ||
|
|
||
| test("#5227 — every scripts/* module imported by bin/ is in the npm files whitelist", () => { | ||
| const pkg = JSON.parse(readFileSync(join(REPO_ROOT, "package.json"), "utf8")); | ||
| const files: string[] = pkg.files || []; | ||
| assert.ok(files.length > 0, "package.json must declare a files whitelist"); | ||
|
|
||
| const binDir = join(REPO_ROOT, "bin"); | ||
| const imported = new Set<string>(); | ||
| for (const binFile of listMjsFiles(binDir)) { | ||
| for (const spec of scriptImports(binFile)) imported.add(spec); | ||
| } | ||
|
|
||
| // Sanity: the known runtime imports must be detected, else the scanner is broken. | ||
| assert.ok( | ||
| imported.has("scripts/build/runtime-env.mjs"), | ||
| "scanner should detect serve.mjs's import of scripts/build/runtime-env.mjs" | ||
| ); | ||
|
|
||
| const missing = [...imported].filter((p) => !isCovered(p, files)); | ||
| assert.deepEqual( | ||
| missing, | ||
| [], | ||
| `bin/ imports these scripts/* modules that are NOT in package.json "files" (global install will fail): ${missing.join(", ")}` | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current regex-based import scanner can match commented-out imports (e.g.,
// import ...), which may lead to false-positive test failures if a developer comments out an import during debugging or refactoring. Stripping single-line and multi-line comments from the source code before running the regex will make the test more robust.