Skip to content
Open
Show file tree
Hide file tree
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 Sep 13, 2026
7ae33f3
feat(bun): classify bun.lock packages as production or development
sgerrand Sep 13, 2026
f90b91c
feat(bun): mark transitive dependency types behind an experiment
sgerrand Sep 13, 2026
3be64d1
test(bun): check dependency types against bun why
sgerrand Sep 13, 2026
5ba2fa8
fix(bun): remove redundant T.cast calls flagged by Sorbet
sgerrand Sep 13, 2026
2eaaa17
feat(common): let a top-level dependency be marked reachable from pro…
sgerrand Sep 13, 2026
444cfb9
fix(bun): keep production reachability for direct dev dependencies
sgerrand Sep 13, 2026
28e7773
chore(bun): use typed: strict for new lib files
sgerrand Sep 13, 2026
7ac0c23
perf(bun): cache lockfile dependencies in the file parser
sgerrand Sep 13, 2026
d4f161f
chore(bun): bump new bun.lock lib files to typed: strong
sgerrand Sep 13, 2026
8bce54f
fix(bun): keep parsed metadata on full-unlock updated dependencies
sgerrand Sep 13, 2026
c06caac
fix(bun): keep subdependency types on full-unlock updated dependencies
sgerrand Sep 13, 2026
3dfb737
fix(bun): check the manifest dependency's own lockfile copy for produ…
sgerrand Sep 15, 2026
2d05971
fix(common): keep reachable_from_production when combining dependencies
sgerrand Sep 15, 2026
72be341
fix(bun): read production reachability from the dependency's own lock…
sgerrand Sep 15, 2026
090b525
test(bun): cover a dev dependency declared in several manifests
sgerrand Sep 15, 2026
a68ad76
refactor(common): share metadata combining in Dependency.combine_meta…
sgerrand Sep 15, 2026
ce3e132
fix(bun): combine update metadata with the shared common helper
sgerrand Sep 15, 2026
4e28a6f
fix(bun): resolve a manifest dependency's lockfile copy once
sgerrand Sep 15, 2026
85ad8ca
Merge branch 'main' into feat/bun-subdependency-types
sgerrand Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions bun/lib/dependabot/bun/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ def manifest_dependencies

requirement = "*" if requirement == ""
dep = build_dependency(
file: file, type: type, name: name, requirement: requirement
file: file,
type: type,
name: name,
requirement: requirement,
# Bun keys a workspace's own nested copies under its package name.
workspace_name: file.name == MANIFEST_FILENAME ? nil : manifest.name
)
dependency_set << dep if dep
end
Expand All @@ -196,9 +201,13 @@ def lockfile_parser
)
end

# Cached so the lockfile dependency set, and the graph walk behind it, is built once per parser.
sig { returns(Dependabot::FileParsers::Base::DependencySet) }
def lockfile_dependencies
lockfile_parser.parse_set
@lockfile_dependencies ||= T.let(
lockfile_parser.parse_set,
T.nilable(Dependabot::FileParsers::Base::DependencySet)
)
end

sig { returns(Dependabot::FileParsers::Base::DependencySet) }
Expand All @@ -215,6 +224,8 @@ def catalog_dependencies
type: "dependencies",
name: name,
requirement: requirement,
# Catalogs are declared in the root manifest, so there is no workspace copy.
workspace_name: nil,
catalog: catalog_name
)
dependency_set << dep if dep
Expand All @@ -225,15 +236,22 @@ def catalog_dependencies
end

sig do
params(file: DependencyFile, type: String, name: String, requirement: String, catalog: T.nilable(String))
.returns(T.nilable(Dependency))
end
def build_dependency(file:, type:, name:, requirement:, catalog: nil)
lockfile_details = lockfile_parser.lockfile_details(
params(
file: DependencyFile,
type: String,
name: String,
requirement: String,
workspace_name: T.nilable(String),
catalog: T.nilable(String)
).returns(T.nilable(Dependency))
end
def build_dependency(file:, type:, name:, requirement:, workspace_name:, catalog: nil)
manifest_copy = lockfile_parser.manifest_copy(
dependency_name: name,
requirement: requirement,
workspace_name: workspace_name,
manifest_name: file.name
)
lockfile_details = manifest_copy&.details
version = version_for(requirement, lockfile_details)
converted_version = T.let(
if version.nil?
Expand Down Expand Up @@ -267,10 +285,18 @@ def build_dependency(file:, type:, name:, requirement:, catalog: nil)
groups: [type],
source: source_for(name, requirement, lockfile_details),
**catalog_metadata(catalog)
}]
}],
metadata: production_metadata(manifest_copy)
)
end

# A package declared only in devDependencies is still production when the copy it
# resolves to is installed through a production dependency. Only that copy counts.
sig { params(manifest_copy: T.nilable(LockfileParser::ManifestCopy)).returns(T::Hash[Symbol, T::Boolean]) }
def production_metadata(manifest_copy)
manifest_copy&.reachable_from_production ? { reachable_from_production: true } : {}
end

sig { params(catalog: T.nilable(String)).returns(T::Hash[Symbol, T::Hash[Symbol, String]]) }
def catalog_metadata(catalog)
catalog ? { metadata: { catalog: catalog } } : {}
Expand Down
90 changes: 88 additions & 2 deletions bun/lib/dependabot/bun/file_parser/bun_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require "yaml"
require "dependabot/errors"
require "dependabot/experiments"
require "dependabot/bun/file_parser"
require "dependabot/bun/bun_package_manager"
require "dependabot/bun/helpers"
Expand All @@ -15,12 +16,18 @@ class BunLock
extend T::Sig

require_relative "bun_lock/record"
require_relative "bun_lock/workspace"
require_relative "bun_lock/dependency_type_resolver"

DEVELOPMENT_SECTIONS = %w(devDependencies).freeze

sig { params(dependency_file: DependencyFile).void }
def initialize(dependency_file)
@dependency_file = dependency_file
@parsed = T.let(nil, T.nilable(T::Hash[Object, Object]))
@records = T.let(nil, T.nilable(T::Hash[String, Record]))
@workspaces = T.let(nil, T.nilable(T::Array[Workspace]))
@production_by_key_for = T.let(nil, T.nilable(T::Hash[String, T::Boolean]))
end

sig { returns(T::Hash[Object, Object]) }
Expand Down Expand Up @@ -63,6 +70,14 @@ def records
end
end

# The roots of the dependency graph, one per entry in the "workspaces" object.
# Malformed entries are skipped rather than reported, because only dependency
# type classification reads them.
sig { returns(T::Array[Workspace]) }
def workspaces
@workspaces ||= parse_workspaces
end

sig { returns(Dependabot::FileParsers::Base::DependencySet) }
def dependencies
dependency_set = Dependabot::FileParsers::Base::DependencySet.new
Expand All @@ -73,7 +88,9 @@ def dependencies
packages = records
raise_invalid!("expected 'packages' to be an object") unless packages

packages.each_value do |record|
production_by_key = production_by_key_for(packages)

packages.each do |key, record|
name = record.name
next if name.empty?

Expand All @@ -84,13 +101,63 @@ def dependencies
name: name,
version: semver,
package_manager: "bun",
requirements: []
requirements: [],
subdependency_metadata: subdependency_metadata_for(key, production_by_key)
Comment thread
sgerrand marked this conversation as resolved.
Resolved
Comment thread
sgerrand marked this conversation as resolved.
Resolved
)
end

dependency_set
end

sig { params(packages: T::Hash[String, Record]).returns(T.nilable(T::Hash[String, T::Boolean])) }
def production_by_key_for(packages)
return unless Dependabot::Experiments.enabled?(:enable_bun_subdependency_types)

@production_by_key_for ||=
DependencyTypeResolver.new(workspaces: workspaces, records: packages).production_by_key
end
private :production_by_key_for

# The packages key a manifest dependency resolves to in this lockfile: the
# workspace's own nested copy (such as "app/ms") first, then the hoisted copy.
sig { params(dependency_name: String, workspace_name: T.nilable(String)).returns(T.nilable(String)) }
def manifest_key(dependency_name, workspace_name)
packages = records
return unless packages

[workspace_name && "#{workspace_name}/#{dependency_name}", dependency_name]
.compact
.find { |candidate| packages.key?(candidate) }
end

# Whether the copy at a packages key is installed through a production dependency.
# False unless dependency types are enabled, or when no workspace reaches the key.
sig { params(key: String).returns(T::Boolean) }
def production_key?(key)
packages = records
return false unless packages

production_by_key = production_by_key_for(packages)
return false unless production_by_key

production_by_key.fetch(key, false)
end

# Record the type for every package, not only development ones. DependencySet joins
# metadata across copies of a package, so a copy with no entry next to one marked
# { production: false } would make the package look development-only.
sig do
params(key: String, production_by_key: T.nilable(T::Hash[String, T::Boolean]))
.returns(T::Array[T::Hash[Symbol, T::Boolean]])
end
def subdependency_metadata_for(key, production_by_key)
return [] unless production_by_key

# Packages that no workspace reaches keep the previous default of production.
[{ production: production_by_key.fetch(key, true) }]
end
private :subdependency_metadata_for

sig do
params(dependency_name: String, _requirement: T.nilable(String), _manifest_name: String)
.returns(T.nilable(Dependabot::Package::NpmLockfileDetails))
Expand All @@ -101,6 +168,25 @@ def details(dependency_name, _requirement, _manifest_name)

private

sig { returns(T::Array[Workspace]) }
def parse_workspaces
raw_workspaces = parsed["workspaces"]
return [] unless raw_workspaces.is_a?(Hash)

raw_workspaces.filter_map do |raw_path, raw_details|
path = T.cast(raw_path, Object)
details = T.cast(raw_details, Object)
next unless path.is_a?(String) && details.is_a?(Hash)

name = T.cast(details["name"], Object)
Workspace.new(
key_prefix: path.empty? || !name.is_a?(String) ? nil : name,
production_names: Record.installed_names(details),
development_names: Record.section_names(details, DEVELOPMENT_SECTIONS)
)
end
end

sig { returns(T::Hash[Object, Object]) }
def parse_document
# Since bun.lock is a JSONC file, which is a subset of YAML, we can use YAML to parse it
Expand Down
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
Loading
Loading