-
Notifications
You must be signed in to change notification settings - Fork 61
PER-9666: avoid require binding name that breaks the packaged binary
#2306
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
Open
rishigupta1599
wants to merge
4
commits into
master
Choose a base branch
from
fix/cli-command-createRequire-binary-crash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−7
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7be2263
fix(cli-command): avoid `require` binding name that breaks the packag…
rishigupta1599 2d7a653
add test to detect breaking pattern
RaghavsBrowserStack 3a8f27c
Merge branch 'master' into fix/cli-command-createRequire-binary-crash
RaghavsBrowserStack 1bc95ea
update .semgrepignore for the new tesst
RaghavsBrowserStack 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,99 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
|
|
||
| // Regression guard for the packaged-binary crash fixed in | ||
| // fix/cli-command-createRequire-binary-crash. | ||
| // | ||
| // In an ESM source file, declaring the createRequire result with the binding | ||
| // name `require`: | ||
| // | ||
| // const require = createRequire(import.meta.url); | ||
| // | ||
| // is fine under Node ESM, but breaks the `pkg`-built executable. When the file | ||
| // is transpiled to CommonJS for the binary, two Babel transforms collide: | ||
| // - preset-env renames the local `require` binding to `_require`, and | ||
| // - transform-import-meta expands `import.meta.url` into a `require('url')` | ||
| // call that is *also* renamed to `_require`. | ||
| // The result is `_require(...)` evaluated inside its own initializer → | ||
| // `TypeError: _require is not a function`, thrown on startup before any command | ||
| // runs. The fix is simply to bind to a non-`require` name (e.g. `cjsRequire`). | ||
| // | ||
| // This is a static source scan — no build step, no new tooling, just fs + a | ||
| // regex over every package's published source. It runs inside the existing | ||
| // Jasmine node suite. | ||
|
|
||
| // Walk up from the package cwd to the monorepo root (the dir holding lerna.json | ||
| // and packages/), so the scan covers the whole repo regardless of which package | ||
| // the suite happens to run from. | ||
| function findRepoRoot() { | ||
| let dir = process.cwd(); | ||
| for (;;) { | ||
| if (fs.existsSync(path.join(dir, 'lerna.json')) && | ||
| fs.existsSync(path.join(dir, 'packages'))) return dir; | ||
| let parent = path.dirname(dir); | ||
| if (parent === dir) throw new Error('could not locate monorepo root'); | ||
| dir = parent; | ||
| } | ||
| } | ||
|
|
||
| // Collect every source file under packages/<pkg>/src. Build output (dist/build), | ||
| // dependencies, tests and coverage are excluded — only authored source matters. | ||
| function collectSourceFiles(root) { | ||
| const SKIP_DIRS = new Set(['node_modules', 'dist', 'build', 'coverage', 'test', '.nyc_output']); | ||
| const SRC_EXT = new Set(['.js', '.cjs', '.mjs']); | ||
| const files = []; | ||
|
|
||
| const walk = (dir) => { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| if (entry.isDirectory()) { | ||
| if (!SKIP_DIRS.has(entry.name)) walk(path.join(dir, entry.name)); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| } else if (SRC_EXT.has(path.extname(entry.name))) { | ||
| files.push(path.join(dir, entry.name)); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| } | ||
| } | ||
| }; | ||
|
|
||
| const packagesDir = path.join(root, 'packages'); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| for (const pkg of fs.readdirSync(packagesDir, { withFileTypes: true })) { | ||
| if (!pkg.isDirectory()) continue; | ||
| const src = path.join(packagesDir, pkg.name, 'src'); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if (fs.existsSync(src)) walk(src); | ||
| } | ||
|
|
||
| return files; | ||
| } | ||
|
|
||
| // Matches a const/let/var binding literally named `require` assigned from | ||
| // createRequire(...). `\s` spans newlines, so a wrapped declaration is caught. | ||
| const FORBIDDEN = /\b(?:const|let|var)\s+require\s*=\s*createRequire\b/; | ||
|
|
||
| describe('source: no `require = createRequire` binding', () => { | ||
| const root = findRepoRoot(); | ||
| const files = collectSourceFiles(root); | ||
|
|
||
| it('scans a non-trivial number of source files', () => { | ||
| // Guards against the walk silently finding nothing (wrong cwd, refactor). | ||
| expect(files.length).toBeGreaterThan(20); | ||
| }); | ||
|
|
||
| it('never binds createRequire to a name `require` (breaks the packaged binary)', () => { | ||
| const violations = []; | ||
|
|
||
| for (const file of files) { | ||
| const lines = fs.readFileSync(file, 'utf8').split('\n'); | ||
| lines.forEach((line, i) => { | ||
| if (FORBIDDEN.test(line)) { | ||
| violations.push(`${path.relative(root, file)}:${i + 1}: ${line.trim()}`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| expect(violations) | ||
| .withContext( | ||
| 'Bind createRequire to a non-`require` name (e.g. `const cjsRequire = ' + | ||
| 'createRequire(import.meta.url)`). Naming it `require` collides with Babel ' + | ||
| 'transforms and crashes the pkg binary with "_require is not a function":\n' + | ||
| violations.join('\n')) | ||
| .toEqual([]); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.