-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ci: validate pnpm override resolution #3005
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
lntutor
wants to merge
4
commits into
Chainlit:main
Choose a base branch
from
lntutor:fix/pnpm-override-validation
base: main
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
4 commits
Select commit
Hold shift + click to select a range
2bef8cc
ci: validate pnpm override resolution
lntutor bca0187
fix(ci): handle pnpm override selector semantics
lntutor 38a6487
Merge branch 'main' into fix/pnpm-override-validation
dokterbob 78a2bf5
Merge branch 'main' into fix/pnpm-override-validation
dokterbob 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import semver from 'semver'; | ||
| import yaml from 'yaml'; | ||
|
|
||
| export const DEFAULT_LOCKFILES = [ | ||
| 'pnpm-lock.yaml', | ||
| 'frontend/pnpm-lock.yaml', | ||
| 'libs/copilot/pnpm-lock.yaml', | ||
| 'libs/react-client/pnpm-lock.yaml' | ||
| ]; | ||
|
|
||
| function parsePackageSelector(selector) { | ||
| const atIndex = selector.lastIndexOf('@'); | ||
| const packageNameEnd = selector.startsWith('@') ? selector.indexOf('/') : 0; | ||
|
|
||
| if (!selector || (selector.startsWith('@') && packageNameEnd <= 1)) { | ||
| throw new Error(`Invalid override selector "${selector}"`); | ||
| } | ||
|
|
||
| const parsedSelector = | ||
| atIndex <= packageNameEnd | ||
| ? { | ||
| packageName: selector, | ||
| sourceRange: '*' | ||
| } | ||
| : { | ||
| packageName: selector.slice(0, atIndex), | ||
| sourceRange: selector.slice(atIndex + 1) || '*' | ||
| }; | ||
|
|
||
| if ( | ||
| !/^(?:@[a-z0-9._~-]+\/)?[a-z0-9._~-]+$/i.test(parsedSelector.packageName) | ||
| ) { | ||
| throw new Error(`Invalid override selector "${selector}"`); | ||
| } | ||
|
|
||
| return parsedSelector; | ||
| } | ||
|
|
||
| export function parseOverrideSelector(selector) { | ||
| try { | ||
| const packageSelector = parsePackageSelector(selector); | ||
|
|
||
| if (semver.validRange(packageSelector.sourceRange)) { | ||
| return packageSelector; | ||
| } | ||
| } catch { | ||
| // Try parsing the selector as a parent-to-dependency edge below. | ||
| } | ||
|
|
||
| for (let edgeIndex = selector.indexOf('>'); edgeIndex >= 0; ) { | ||
| try { | ||
| const parentSelector = parsePackageSelector(selector.slice(0, edgeIndex)); | ||
| const dependencySelector = parsePackageSelector( | ||
| selector.slice(edgeIndex + 1) | ||
| ); | ||
|
|
||
| if ( | ||
| semver.validRange(parentSelector.sourceRange) && | ||
| semver.validRange(dependencySelector.sourceRange) | ||
| ) { | ||
| return { | ||
| ...dependencySelector, | ||
| parentSelector | ||
| }; | ||
| } | ||
| } catch { | ||
| // Keep looking because the range itself may contain a comparator. | ||
| } | ||
|
|
||
| edgeIndex = selector.indexOf('>', edgeIndex + 1); | ||
| } | ||
|
|
||
| throw new Error(`Invalid override selector "${selector}"`); | ||
| } | ||
|
|
||
| export function parsePackageKey(packageKey) { | ||
| const match = packageKey.match(/^((?:@[^/]+\/)?[^@]+)@([^()]+?)(?=\(|$)/); | ||
|
|
||
| if (!match) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| packageName: match[1], | ||
| version: match[2] | ||
| }; | ||
| } | ||
|
|
||
| function parseDependencyVersion(reference) { | ||
| if (typeof reference !== 'string') { | ||
| return null; | ||
| } | ||
|
|
||
| const parsedPackage = parsePackageKey(reference); | ||
|
|
||
| if (parsedPackage && semver.valid(parsedPackage.version)) { | ||
| return parsedPackage.version; | ||
| } | ||
|
|
||
| const version = reference.split('(', 1)[0]; | ||
| return semver.valid(version) ? version : null; | ||
| } | ||
|
|
||
| function getEdgeVersions(snapshots, parentSelector, dependencyName) { | ||
| const versions = new Set(); | ||
|
|
||
| for (const [snapshotKey, snapshot] of Object.entries(snapshots)) { | ||
| const parsedParent = parsePackageKey(snapshotKey); | ||
|
|
||
| if ( | ||
| !parsedParent || | ||
| parsedParent.packageName !== parentSelector.packageName || | ||
| !semver.valid(parsedParent.version) || | ||
| !semver.satisfies(parsedParent.version, parentSelector.sourceRange, { | ||
| includePrerelease: true | ||
| }) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const dependencyReference = | ||
| snapshot.dependencies?.[dependencyName] ?? | ||
| snapshot.optionalDependencies?.[dependencyName]; | ||
| const dependencyVersion = parseDependencyVersion(dependencyReference); | ||
|
|
||
| if (dependencyVersion) { | ||
| versions.add(dependencyVersion); | ||
| } | ||
| } | ||
|
|
||
| return [...versions].sort(semver.compare); | ||
| } | ||
|
|
||
| export function validateLockfile(lockfilePath, lockfileContents) { | ||
| const parsed = yaml.parse(lockfileContents); | ||
| const overrides = parsed.overrides || {}; | ||
| const packages = parsed.packages || {}; | ||
| const snapshots = parsed.snapshots || {}; | ||
| const packageVersions = new Map(); | ||
| const errors = []; | ||
|
|
||
| for (const packageKey of Object.keys(packages)) { | ||
| const parsedPackage = parsePackageKey(packageKey); | ||
|
|
||
| if (!parsedPackage || !semver.valid(parsedPackage.version)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!packageVersions.has(parsedPackage.packageName)) { | ||
| packageVersions.set(parsedPackage.packageName, new Set()); | ||
| } | ||
|
|
||
| packageVersions.get(parsedPackage.packageName).add(parsedPackage.version); | ||
| } | ||
|
|
||
| for (const [selector, targetRange] of Object.entries(overrides)) { | ||
| const { packageName, sourceRange, parentSelector } = | ||
| parseOverrideSelector(selector); | ||
|
|
||
| if (typeof targetRange !== 'string' || !semver.validRange(targetRange)) { | ||
| continue; | ||
| } | ||
|
|
||
| const resolvedVersions = parentSelector | ||
| ? getEdgeVersions(snapshots, parentSelector, packageName) | ||
| : [...(packageVersions.get(packageName) || [])].sort(semver.compare); | ||
|
|
||
| for (const version of resolvedVersions) { | ||
| if (semver.satisfies(version, targetRange, { includePrerelease: true })) { | ||
| continue; | ||
| } | ||
|
|
||
| if (semver.satisfies(version, sourceRange, { includePrerelease: true })) { | ||
| errors.push( | ||
| `${lockfilePath}: resolved ${packageName}@${version} does not satisfy override "${selector}" -> "${targetRange}"` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| export function main(lockfilePaths = DEFAULT_LOCKFILES) { | ||
| const allErrors = []; | ||
|
|
||
| for (const relativePath of lockfilePaths) { | ||
| const absolutePath = path.resolve(relativePath); | ||
| const contents = fs.readFileSync(absolutePath, 'utf8'); | ||
| allErrors.push(...validateLockfile(relativePath, contents)); | ||
| } | ||
|
|
||
| if (allErrors.length > 0) { | ||
| console.error(allErrors.join('\n')); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| console.log( | ||
| `Validated pnpm override targets across ${lockfilePaths.length} lockfiles.` | ||
| ); | ||
| } | ||
|
|
||
| if (import.meta.url === `file://${process.argv[1]}`) { | ||
| const cliPaths = process.argv.slice(2); | ||
| main(cliPaths.length > 0 ? cliPaths : DEFAULT_LOCKFILES); | ||
| } |
Oops, something went wrong.
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.