-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add script to check transitive deps COMPASS-10569 #633
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
lerouxb
wants to merge
15
commits into
main
Choose a base branch
from
check-transitive-deps
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
15 commits
Select commit
Hold shift + click to select a range
29b655f
check-transitive-deps script
lerouxb 25e2d6e
improvements
lerouxb 967968d
minor cleanup
lerouxb a111bbe
more minor refactoring
lerouxb e25089d
some unit tests and a fix
lerouxb 1395dbc
refactor, more tests
lerouxb ee89317
more refactoring, more tests
lerouxb f1869a1
claude suggestions
lerouxb d5fdc1e
oops. the require.main guard never worked
lerouxb 52711bc
just one deps config
lerouxb 8d74e78
move the rejection handler into the run guard
lerouxb d575415
update this repo's own config
lerouxb 2e0c1dd
eslint
lerouxb 79234dd
add tests for getPackagesInTopologicalOrder because that's now consid…
lerouxb 5e54700
move the helpers and tests so that we don't have to import the script…
lerouxb 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "deps": [ | ||
| "@mongodb-js/*", | ||
| "mongodb", | ||
| "mongodb-*", | ||
| "@mongosh/*", | ||
| "bson" | ||
| ] | ||
| } | ||
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,3 @@ | ||
| #!/usr/bin/env node | ||
| 'use strict'; | ||
| require('../dist/check-transitive-deps.js'); |
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,168 @@ | ||
| #! /usr/bin/env node | ||
| /* eslint-disable no-console */ | ||
|
|
||
| import path from 'path'; | ||
| import { promises as fs } from 'fs'; | ||
| import chalk from 'chalk'; | ||
| import pacote from 'pacote'; | ||
| import { listAllPackages } from './utils/list-all-packages'; | ||
| import minimist from 'minimist'; | ||
| import type { ParsedArgs } from 'minimist'; | ||
| import { | ||
| gatherTransitiveDepsInfo, | ||
| findMisalignments, | ||
| } from './utils/package-helpers'; | ||
|
|
||
| const USAGE = `Check transitive dependencies for version alignment. | ||
|
|
||
| USAGE: check-transitive-deps.js [--deps <list>] [--config <path>] [--ignore-dev-deps] | ||
|
|
||
| Options: | ||
|
|
||
| --deps Comma-separated list of dependencies to track. | ||
| --config Path to config file. Default is .check-transitive-deps.json | ||
| --ignore-dev-deps Ignore devDependencies when scanning both our own packages and tracked dependencies. | ||
|
|
||
| Config file format (.check-transitive-deps.json): | ||
| { | ||
| "deps": ["package-a", "@my-scope/*"] | ||
| } | ||
|
|
||
| Glob patterns are supported: * matches any sequence of characters except /. | ||
| For example, @mongodb-js/* matches all packages in that scope. | ||
|
|
||
| For each transitive dependency, the script prints: | ||
| - Which of our monorepo packages depend on it directly, and at what version range. | ||
| - Which tracked direct dependencies also depend on it, and at what version range. | ||
|
|
||
| This lets you verify that your first-party packages and your tracked dependencies | ||
| all require the same version of a shared transitive dependency. | ||
| `; | ||
|
|
||
| interface Config { | ||
| deps: string[]; | ||
| } | ||
|
|
||
| async function loadConfig(args: ParsedArgs): Promise<Config> { | ||
| const configPath = | ||
| typeof args.config === 'string' | ||
| ? path.resolve(process.cwd(), args.config) | ||
| : path.join(process.cwd(), '.check-transitive-deps.json'); | ||
|
|
||
| let fileConfig: Partial<Config> = {}; | ||
|
|
||
| try { | ||
| fileConfig = JSON.parse(await fs.readFile(configPath, 'utf8')); | ||
| } catch (e: any) { | ||
| if (e.code !== 'ENOENT' || args.config) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| const deps = | ||
| typeof args.deps === 'string' | ||
| ? args.deps.split(',').map((s: string) => s.trim()) | ||
| : Array.isArray(args.deps) | ||
| ? args.deps | ||
| : fileConfig.deps || []; | ||
|
|
||
| return { deps }; | ||
| } | ||
|
|
||
| async function main(args: ParsedArgs) { | ||
| if (args.help) { | ||
| console.log(USAGE); | ||
| return; | ||
| } | ||
|
|
||
| const config = await loadConfig(args); | ||
|
|
||
| if (config.deps.length === 0) { | ||
| console.error('--deps (or deps in config) must be provided and non-empty.'); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| const ignoreDevDeps: boolean = args['ignore-dev-deps'] === true; | ||
|
|
||
| const groups = await gatherTransitiveDepsInfo({ | ||
| ...config, | ||
| ignoreDevDeps, | ||
| packages: listAllPackages(), | ||
| resolveExternal: (name, versionRange) => | ||
| pacote.manifest(`${name}@${versionRange}`), | ||
| }); | ||
|
|
||
| if (groups.size === 0) { | ||
| console.log( | ||
| '%s', | ||
| chalk.green( | ||
| 'No transitive dependencies found matching the provided allow lists.', | ||
| ), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const mismatches = findMisalignments(groups); | ||
|
|
||
| if (mismatches.length === 0) { | ||
| console.log( | ||
| '%s', | ||
| chalk.green( | ||
| 'All transitive dependencies are aligned, nothing to report!', | ||
| ), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| for (const { name, highestVersion, entries } of mismatches) { | ||
| const versionPad = Math.max(...entries.map((e) => e.version.length)); | ||
|
|
||
| console.log( | ||
| '%s %s', | ||
| chalk.bold(name), | ||
| chalk.dim(`highest: ${highestVersion ?? 'unknown'}`), | ||
| ); | ||
| console.log(); | ||
|
|
||
| for (const { version, label, satisfiesHighest: match } of entries) { | ||
| const indicator = | ||
| match === null ? ' ' : match ? chalk.green('✓') : chalk.red('✗'); | ||
| console.log( | ||
| '%s %s%s %s', | ||
| indicator, | ||
| ' '.repeat(versionPad - version.length), | ||
| version, | ||
| chalk.dim(label), | ||
| ); | ||
| } | ||
|
|
||
| console.log(); | ||
| } | ||
|
|
||
| const misaligned = mismatches | ||
| .filter((m) => m.entries.some((e) => e.satisfiesHighest === false)) | ||
| .map((m) => m.name); | ||
|
|
||
| if (misaligned.length > 0) { | ||
| console.log(chalk.bold.red('Misaligned transitive dependencies:')); | ||
| console.log(); | ||
| for (const dep of misaligned) { | ||
| console.log(' %s', dep); | ||
| } | ||
| console.log(); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| process.on('unhandledRejection', (err: Error) => { | ||
| console.error(); | ||
| console.error(err?.stack || err?.message || err); | ||
| process.exitCode = 1; | ||
| }); | ||
|
|
||
| main(minimist(process.argv.slice(2))).catch((err) => | ||
| process.nextTick(() => { | ||
| throw err; | ||
| }), | ||
| ); |
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.
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.
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.
Anything else we want to keep aligned? Running this with just
["*"]is kinda hilarious.