-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix(arborist): honor gypfile:false on lockfile-driven installs #9859
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
lazerg
wants to merge
2
commits into
npm:latest
Choose a base branch
from
lazerg:fix/9837-gypfile-lockfile-nodes
base: latest
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const PackageJson = require('@npmcli/package-json') | ||
|
|
||
| // `gypfile: false` opts a package out of the synthetic `node-gyp rebuild` | ||
| // install script npm adds when it finds a `binding.gyp`. | ||
| // | ||
| // The flag is only on the tree node when the node came from a packument or | ||
| // from disk. Lockfile-derived nodes (`npm ci`, a repeat `npm install`) carry | ||
| // no `gypfile` field at all, so the opt-out is invisible there and has to be | ||
| // read back off the installed package.json. | ||
| // | ||
| // The disk read is only trustworthy when the package.json at `path` is the | ||
| // package the node describes. The strict allow-scripts preflight runs before | ||
| // reify replaces `node_modules`, so `path` can still hold a different version | ||
| // of the package, or a different package altogether. Trusting a stale | ||
| // `gypfile: false` there would let the preflight pass while the later rebuild | ||
| // (which sees the real package) synthesises `node-gyp rebuild` anyway. So the | ||
| // name and the version on disk must match the node before the flag counts. | ||
| // Without both, treat the opt-out as absent, which is what npm did before the | ||
| // fallback existed. | ||
| const hasGypfileOptOut = async (path, pkg, nodeName) => { | ||
| if (pkg.gypfile !== undefined) { | ||
| return pkg.gypfile === false | ||
| } | ||
|
|
||
| const name = pkg.name || nodeName | ||
| const { version } = pkg | ||
| if (!name || !version) { | ||
| return false | ||
| } | ||
|
|
||
| const { content } = await PackageJson.load(path).catch(() => ({ content: {} })) | ||
| if (content.name !== name || content.version !== version) { | ||
| return false | ||
| } | ||
|
|
||
| return content.gypfile === false | ||
| } | ||
|
|
||
| module.exports = { hasGypfileOptOut } |
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
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.
getInstallScripts()also runs during the strict allow-scripts preflight, before reify replaces the existingnode_modulesentry. At that point,node.pathcan refer to a different version.For example, suppose the installed version has
binding.gypandgypfile: false, while the locked version hasbinding.gypwithout the opt-out. This readsfalsefrom the installed version, so strict preflight passes. Rebuild later sees the new package and synthesizesnode-gyp rebuild.Can we check that the package on disk matches the node's name and version before trusting this fallback?
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.
Good catch, that is real. The preflight walks the ideal tree, so
node.pathstill points at whatever version is installed right now.hasGypfileOptOutnow takes the node's package object instead of only thegypfilevalue, and it trusts the disk read only when the package.json at that path carries the same name and version as the node. If either one differs, or the node has no name or version to compare against, the opt-out counts as absent andnode-gyp rebuildis enumerated. That is what npm did before this fallback existed, so the preflight can no longer come out quieter than the later rebuild.Added three cases in
workspaces/arborist/test/install-scripts.js: a version mismatch on disk, a name mismatch, and a node with nothing to match on.