-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Validate Yarn lockfile selectors #14663
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
Sean McManus (sean-mcmanus)
merged 2 commits into
main
from
seanmcm/devbox2-wsl/agent3/validate-yarn-lock-selectors
Aug 11, 2026
+144
−0
Merged
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,97 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import process from 'node:process'; | ||
| import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
|
|
||
| const dependencySections = ['dependencies', 'devDependencies', 'optionalDependencies']; | ||
|
|
||
| function parseLockfileKey(key) { | ||
| const selectors = []; | ||
| let selectorStart = 0; | ||
| let quoted = false; | ||
| let escaped = false; | ||
|
|
||
| for (let index = 0; index < key.length; index++) { | ||
| const character = key[index]; | ||
| if (escaped) { | ||
| escaped = false; | ||
| } else if (character === '\\' && quoted) { | ||
| escaped = true; | ||
| } else if (character === '"') { | ||
| quoted = !quoted; | ||
| } else if (character === ',' && !quoted) { | ||
| selectors.push(key.slice(selectorStart, index)); | ||
| selectorStart = index + 1; | ||
| } | ||
| } | ||
| selectors.push(key.slice(selectorStart)); | ||
|
|
||
| return selectors.map(selector => { | ||
| const trimmedSelector = selector.trim(); | ||
| return trimmedSelector.startsWith('"') ? JSON.parse(trimmedSelector) : trimmedSelector; | ||
| }); | ||
| } | ||
|
|
||
| function parseLockfileSelectors(lockfile) { | ||
| const selectors = new Set(); | ||
| for (const line of lockfile.split(/\r?\n/)) { | ||
| if (/^[^\s#].*:\s*$/.test(line)) { | ||
| for (const selector of parseLockfileKey(line.replace(/:\s*$/, ''))) { | ||
| selectors.add(selector); | ||
| } | ||
| } | ||
| } | ||
| return selectors; | ||
| } | ||
|
|
||
| function getResolutionPackageName(pattern) { | ||
| const segments = pattern.split('/'); | ||
| const packageName = segments.at(-1); | ||
| const scope = segments.at(-2); | ||
| return scope?.startsWith('@') ? `${scope}/${packageName}` : packageName; | ||
| } | ||
|
|
||
| function getExpectedSelectors(manifest) { | ||
| const selectors = []; | ||
| for (const section of dependencySections) { | ||
| for (const [packageName, range] of Object.entries(manifest[section] ?? {})) { | ||
| selectors.push(`${packageName}@${range}`); | ||
| } | ||
| } | ||
| for (const [pattern, range] of Object.entries(manifest.resolutions ?? {})) { | ||
| selectors.push(`${getResolutionPackageName(pattern)}@${range}`); | ||
| } | ||
| return selectors; | ||
| } | ||
|
|
||
| function findMissingSelectors(manifest, lockfile) { | ||
| const lockfileSelectors = parseLockfileSelectors(lockfile); | ||
| return getExpectedSelectors(manifest) | ||
| .filter(selector => !lockfileSelectors.has(selector)) | ||
| .sort(); | ||
| } | ||
|
|
||
| function validateYarnLock(packageJsonPath, yarnLockPath) { | ||
| const manifest = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); | ||
| const lockfile = fs.readFileSync(yarnLockPath, 'utf8'); | ||
| const missingSelectors = findMissingSelectors(manifest, lockfile); | ||
| if (missingSelectors.length > 0) { | ||
| throw new Error(`yarn.lock is missing selectors required by package.json:\n${missingSelectors.map(selector => ` ${selector}`).join('\n')}\nRun yarn install to update yarn.lock.`); | ||
| } | ||
| } | ||
|
|
||
| const invokedUrl = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : undefined; | ||
| if (invokedUrl === import.meta.url) { | ||
| const extensionRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); | ||
| const packageJsonPath = process.argv[2] ?? path.join(extensionRoot, 'package.json'); | ||
| const yarnLockPath = process.argv[3] ?? path.join(extensionRoot, 'yarn.lock'); | ||
|
|
||
| try { | ||
| validateYarnLock(packageJsonPath, yarnLockPath); | ||
| } catch (error) { | ||
| process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| export { findMissingSelectors, getExpectedSelectors, parseLockfileSelectors, validateYarnLock }; |
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,41 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
| import { findMissingSelectors } from './verifyYarnLock.mjs'; | ||
|
|
||
| test('reports a stale resolution selector', () => { | ||
| const manifest = { resolutions: { 'fast-uri': '^3.1.5' } }; | ||
| const lockfile = `fast-uri@^3.0.1, fast-uri@^3.1.4: | ||
| version "3.1.5" | ||
| `; | ||
|
|
||
| assert.deepEqual(findMissingSelectors(manifest, lockfile), ['fast-uri@^3.1.5']); | ||
| }); | ||
|
|
||
| test('accepts direct, scoped, and nested resolution selectors', () => { | ||
| const manifest = { | ||
| dependencies: { '@scope/direct': '^1.0.0' }, | ||
| devDependencies: { 'gulp-typescript': '^5.0.1' }, | ||
| resolutions: { | ||
| '@scope/resolved': '^2.0.0', | ||
| 'gulp-typescript/**/glob-parent': '^5.1.2', | ||
| 'parent/**/@nested/package': '~3.0.0' | ||
| } | ||
| }; | ||
| const lockfile = `"@nested/package@~3.0.0": | ||
| version "3.0.1" | ||
|
|
||
| "@scope/direct@^1.0.0": | ||
| version "1.0.0" | ||
|
|
||
| "@scope/resolved@^2.0.0": | ||
| version "2.0.0" | ||
|
|
||
| glob-parent@^3.1.0, glob-parent@^5.1.2: | ||
| version "5.1.2" | ||
|
|
||
| gulp-typescript@^5.0.1: | ||
| version "5.0.1" | ||
| `; | ||
|
|
||
| assert.deepEqual(findMissingSelectors(manifest, lockfile), []); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.