Repository navigation
bun: Classify transitive dependencies as production or development #16287
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
sgerrand
wants to merge
20
commits into
dependabot:main
Choose a base branch
from
sgerrand:feat/bun-subdependency-types
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
20 commits
Select commit
Hold shift + click to select a range
8026890
feat(bun): read workspace roots and package edges from bun.lock
sgerrand 7ae33f3
feat(bun): classify bun.lock packages as production or development
sgerrand f90b91c
feat(bun): mark transitive dependency types behind an experiment
sgerrand 3be64d1
test(bun): check dependency types against bun why
sgerrand 5ba2fa8
fix(bun): remove redundant T.cast calls flagged by Sorbet
sgerrand 2eaaa17
feat(common): let a top-level dependency be marked reachable from pro…
sgerrand 444cfb9
fix(bun): keep production reachability for direct dev dependencies
sgerrand 28e7773
chore(bun): use typed: strict for new lib files
sgerrand 7ac0c23
perf(bun): cache lockfile dependencies in the file parser
sgerrand d4f161f
chore(bun): bump new bun.lock lib files to typed: strong
sgerrand 8bce54f
fix(bun): keep parsed metadata on full-unlock updated dependencies
sgerrand c06caac
fix(bun): keep subdependency types on full-unlock updated dependencies
sgerrand 3dfb737
fix(bun): check the manifest dependency's own lockfile copy for produ…
sgerrand 2d05971
fix(common): keep reachable_from_production when combining dependencies
sgerrand 72be341
fix(bun): read production reachability from the dependency's own lock…
sgerrand 090b525
test(bun): cover a dev dependency declared in several manifests
sgerrand a68ad76
refactor(common): share metadata combining in Dependency.combine_meta…
sgerrand ce3e132
fix(bun): combine update metadata with the shared common helper
sgerrand 4e28a6f
fix(bun): resolve a manifest dependency's lockfile copy once
sgerrand 85ad8ca
Merge branch 'main' into feat/bun-subdependency-types
sgerrand 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
105 changes: 105 additions & 0 deletions
105
bun/lib/dependabot/bun/file_parser/bun_lock/dependency_type_resolver.rb
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,105 @@ | ||
| # typed: strong | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sorbet-runtime" | ||
| require "dependabot/bun/file_parser/bun_lock" | ||
|
|
||
| module Dependabot | ||
| module Bun | ||
| class FileParser < Dependabot::FileParsers::Base | ||
| class BunLock | ||
| # Works out whether each bun.lock package is a production or a development | ||
| # dependency. Unlike package-lock.json, bun.lock has no "dev" flag, so this | ||
| # walks the graph from every workspace root instead. | ||
| # | ||
| # A package is production if any path to it starts with a dependencies, | ||
| # optionalDependencies or peerDependencies entry on a workspace. It is | ||
| # development only if every path to it starts with a devDependencies entry. | ||
| class DependencyTypeResolver | ||
| extend T::Sig | ||
|
|
||
| # A parent packages key (nil for a workspace root) and the name of a package it pulls in. | ||
| Edge = T.type_alias { [T.nilable(String), String] } | ||
|
|
||
| SCOPE_SEGMENT = %r{\A@[^/]+\z} | ||
|
|
||
| sig { params(workspaces: T::Array[Workspace], records: T::Hash[String, Record]).void } | ||
| def initialize(workspaces:, records:) | ||
| @workspaces = workspaces | ||
| @records = records | ||
| @production_by_key = T.let(nil, T.nilable(T::Hash[String, T::Boolean])) | ||
| end | ||
|
|
||
| # Maps each reachable packages key to true (production) or false (development). | ||
| # Keys that no workspace reaches are left out. | ||
| sig { returns(T::Hash[String, T::Boolean]) } | ||
| def production_by_key | ||
| @production_by_key ||= begin | ||
| result = T.let({}, T::Hash[String, T::Boolean]) | ||
| # Walk production edges first, so a package on both kinds of path stays production. | ||
| walk(root_edges(production: true), result, production: true) | ||
| walk(root_edges(production: false), result, production: false) | ||
| result | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| sig { params(production: T::Boolean).returns(T::Array[Edge]) } | ||
| def root_edges(production:) | ||
| @workspaces.flat_map do |workspace| | ||
| names = production ? workspace.production_names : workspace.development_names | ||
| names.map { |name| [workspace.key_prefix, name] } | ||
| end | ||
| end | ||
|
|
||
| sig { params(queue: T::Array[Edge], result: T::Hash[String, T::Boolean], production: T::Boolean).void } | ||
| def walk(queue, result, production:) | ||
| index = 0 | ||
| while index < queue.length | ||
| parent_key, child_name = T.must(queue[index]) | ||
| index += 1 | ||
|
|
||
| key = resolve_key(parent_key, child_name) | ||
| next if key.nil? || result.key?(key) | ||
|
|
||
| result[key] = production | ||
| T.must(@records[key]).edge_names.each { |name| queue << [key, name] } | ||
| end | ||
| end | ||
|
|
||
| # Bun keys a nested copy of a package by the packages it sits under, such as | ||
| # "debug/ms" or "app/is-number". Look under the closest parent first, then | ||
| # move up one level at a time until reaching the hoisted top-level key. | ||
| sig { params(parent_key: T.nilable(String), child_name: String).returns(T.nilable(String)) } | ||
| def resolve_key(parent_key, child_name) | ||
| segments = key_segments(parent_key) | ||
| until segments.empty? | ||
| candidate = [*segments, child_name].join("/") | ||
| return candidate if @records.key?(candidate) | ||
|
|
||
| segments.pop | ||
| end | ||
|
|
||
| @records.key?(child_name) ? child_name : nil | ||
| end | ||
|
|
||
| # Splits a packages key into package names, keeping scoped names such as "@types/node" whole. | ||
| sig { params(key: T.nilable(String)).returns(T::Array[String]) } | ||
| def key_segments(key) | ||
| return [] if key.nil? || key.empty? | ||
|
|
||
| key.split("/").each_with_object(T.let([], T::Array[String])) do |part, segments| | ||
| last = segments.last | ||
| if last&.match?(SCOPE_SEGMENT) | ||
| segments[-1] = "#{last}/#{part}" | ||
| else | ||
| segments << part | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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.