-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Build: Detect stale node_modules at build/dev time #77995
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
manzoorwanijk
wants to merge
2
commits into
trunk
Choose a base branch
from
build/check-installed-deps
base: trunk
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
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 was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,100 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Verifies that node_modules is in sync with package-lock.json by comparing | ||
| * `package-lock.json` against `node_modules/.package-lock.json` (npm's hidden | ||
| * lockfile, written on every install to record the actual installed tree). | ||
| * | ||
| * Exits non-zero with a hint to run `npm install` if the trees diverge. | ||
| */ | ||
|
|
||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import os from 'node:os'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const __dirname = path.dirname( fileURLToPath( import.meta.url ) ); | ||
| const ROOT = path.resolve( __dirname, '../..' ); | ||
| const LOCKFILE = path.join( ROOT, 'package-lock.json' ); | ||
| const HIDDEN_LOCKFILE = path.join( ROOT, 'node_modules', '.package-lock.json' ); | ||
|
|
||
| const verbose = process.argv.includes( '--verbose' ); | ||
|
|
||
| function fail( summary, details = '' ) { | ||
| let msg = `\n ⚠️ Dependencies are out of sync: ${ summary }`; | ||
| if ( verbose && details ) { | ||
| msg += os.EOL + details; | ||
| } | ||
| console.error( msg ); | ||
| process.exit( 1 ); | ||
| } | ||
|
|
||
| if ( ! fs.existsSync( HIDDEN_LOCKFILE ) ) { | ||
| fail( | ||
| 'node_modules is missing or incomplete.', | ||
| `\t${ path.relative( ROOT, HIDDEN_LOCKFILE ) } not found.` | ||
| ); | ||
| } | ||
|
|
||
| const lock = JSON.parse( fs.readFileSync( LOCKFILE, 'utf8' ) ); | ||
| const hidden = JSON.parse( fs.readFileSync( HIDDEN_LOCKFILE, 'utf8' ) ); | ||
|
|
||
| const lockPkgs = lock.packages || {}; | ||
| const hiddenPkgs = hidden.packages || {}; | ||
|
|
||
| const reportedMismatches = []; | ||
| const MAX_REPORTED = 5; | ||
| let totalMismatches = 0; | ||
|
|
||
| for ( const [ pkgPath, info ] of Object.entries( lockPkgs ) ) { | ||
| /* | ||
| * Skip entries without an `integrity` field — these are the root project | ||
| * and workspace/link packages (file: or workspace: refs), which aren't | ||
| * fetched from a registry and have no installed counterpart to verify. | ||
| */ | ||
| if ( ! info.integrity ) { | ||
| continue; | ||
| } | ||
|
|
||
| const installed = hiddenPkgs[ pkgPath ]; | ||
|
|
||
| let mismatch; | ||
| if ( ! installed ) { | ||
| /* | ||
| * Optional deps may be skipped by npm on the current platform | ||
| * (e.g. macOS-only fsevents on Linux). Don't flag them as missing. | ||
| * Real drift on an optional dep would still be caught below as | ||
| * an integrity mismatch. | ||
| */ | ||
| if ( info.optional ) { | ||
| continue; | ||
| } | ||
| mismatch = `missing: ${ pkgPath }`; | ||
| } else if ( installed.integrity !== info.integrity ) { | ||
| mismatch = `integrity mismatch: ${ pkgPath }`; | ||
| } | ||
|
|
||
| if ( ! mismatch ) { | ||
| continue; | ||
| } | ||
|
|
||
| totalMismatches++; | ||
| if ( reportedMismatches.length < MAX_REPORTED ) { | ||
| reportedMismatches.push( mismatch ); | ||
| } | ||
| } | ||
|
|
||
| if ( totalMismatches > 0 ) { | ||
| const detailLines = reportedMismatches.map( ( m ) => `\t${ m }` ); | ||
| if ( totalMismatches > reportedMismatches.length ) { | ||
| detailLines.push( | ||
| `\t... and ${ totalMismatches - reportedMismatches.length } more.` | ||
| ); | ||
| } | ||
| fail( | ||
| `Mismatches found: ${ totalMismatches }`, | ||
| detailLines.join( os.EOL ) | ||
| ); | ||
| } | ||
|
|
||
| console.log( '\n ✔ All good.' ); | ||
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,29 @@ | ||
| { | ||
| "name": "@wordpress/validation-tools", | ||
| "version": "0.0.0", | ||
| "description": "Validation scripts used across the Gutenberg monorepo.", | ||
| "private": true, | ||
| "author": "The WordPress Contributors", | ||
| "license": "GPL-2.0-or-later", | ||
| "keywords": [ | ||
| "wordpress", | ||
| "gutenberg", | ||
| "validation" | ||
| ], | ||
| "homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/tools/validation", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/WordPress/gutenberg.git", | ||
| "directory": "tools/validation" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/WordPress/gutenberg/issues" | ||
| }, | ||
| "type": "module", | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "scripts": { | ||
| "check-installed-deps": "node ./check-installed-deps.mjs" | ||
| } | ||
| } |
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.