-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Support Bun with bun.lock
#11209
Closed
+1,331
−40
Closed
Support Bun with bun.lock
#11209
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5ba3eff
Support Bun with `bun.lock`
Electroid 5ee76da
Address linting errors
Electroid b112dac
Address linting errors
Electroid ad96da2
Fix test failures
Electroid d84c9ff
Fix lint error
Electroid 4c0f1b0
Rebase fix
Electroid 4788565
Fix sorbet error
Electroid 56641d4
Merge branch 'main' into feat-bun-lock
Electroid 6f686fd
Merge branch 'main' into feat-bun-lock
Electroid 27b9e70
Merge branch 'main' into feat-bun-lock
markhallen 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 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 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 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 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
148 changes: 148 additions & 0 deletions
148
npm_and_yarn/lib/dependabot/npm_and_yarn/file_parser/bun_lock.rb
This file contains 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,148 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "yaml" | ||
require "dependabot/errors" | ||
require "dependabot/npm_and_yarn/helpers" | ||
require "sorbet-runtime" | ||
|
||
module Dependabot | ||
module NpmAndYarn | ||
class FileParser < Dependabot::FileParsers::Base | ||
class BunLock | ||
extend T::Sig | ||
|
||
sig { params(dependency_file: DependencyFile).void } | ||
def initialize(dependency_file) | ||
@dependency_file = dependency_file | ||
end | ||
|
||
sig { returns(T::Hash[String, T.untyped]) } | ||
def parsed | ||
# Since bun.lock is a JSONC file, which is a subset of YAML, we can use YAML to parse it | ||
content = YAML.load(T.must(@dependency_file.content)) | ||
raise_invalid!("expected to be an object") unless content.is_a?(Hash) | ||
|
||
version = content["lockfileVersion"] | ||
raise_invalid!("expected 'lockfileVersion' to be an integer") unless version.is_a?(Integer) | ||
raise_invalid!("expected 'lockfileVersion' to be >= 0") unless version >= 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps stick to version |
||
unless version.zero? | ||
raise_invalid!(<<~ERROR | ||
unsupported 'lockfileVersion' = #{version}, please open an issue with Dependabot to support this: | ||
https://github.com/dependabot/dependabot/issues/new | ||
ERROR | ||
) | ||
end | ||
|
||
@content ||= T.let(content, T.untyped) | ||
rescue Psych::SyntaxError => e | ||
raise_invalid!("malformed JSONC at line #{e.line}, column #{e.column}") | ||
end | ||
|
||
sig { returns(Integer) } | ||
def version | ||
parsed["lockfileVersion"] | ||
end | ||
|
||
sig { returns(Dependabot::FileParsers::Base::DependencySet) } | ||
def dependencies | ||
dependency_set = Dependabot::FileParsers::Base::DependencySet.new | ||
|
||
# bun.lock v0 format: | ||
# https://github.com/oven-sh/bun/blob/c130df6c589fdf28f9f3c7f23ed9901140bc9349/src/install/bun.lock.zig#L595-L605 | ||
|
||
packages = parsed["packages"] | ||
raise_invalid!("expected 'packages' to be an object") unless packages.is_a?(Hash) | ||
|
||
packages.each do |key, details| | ||
raise_invalid!("expected 'packages.#{key}' to be an array") unless details.is_a?(Array) | ||
|
||
resolution = details.first | ||
raise_invalid!("expected 'packages.#{key}[0]' to be a string") unless resolution.is_a?(String) | ||
|
||
name, version = resolution.split(/(?<=\w)\@/) | ||
next if name.empty? | ||
|
||
semver = Version.semver_for(version) | ||
next unless semver | ||
|
||
dependency_set << Dependency.new( | ||
name: name, | ||
version: semver.to_s, | ||
package_manager: "npm_and_yarn", | ||
requirements: [] | ||
) | ||
end | ||
|
||
dependency_set | ||
end | ||
|
||
sig do | ||
params(dependency_name: String, requirement: T.untyped, _manifest_name: String) | ||
.returns(T.nilable(T::Hash[String, T.untyped])) | ||
end | ||
def details(dependency_name, requirement, _manifest_name) | ||
packages = parsed["packages"] | ||
return unless packages.is_a?(Hash) | ||
|
||
candidates = | ||
packages | ||
.select { |name, _| name == dependency_name } | ||
.values | ||
|
||
# If there's only one entry for this dependency, use it, even if | ||
# the requirement in the lockfile doesn't match | ||
if candidates.one? | ||
parse_details(candidates.first) | ||
else | ||
candidate = candidates.find do |label, _| | ||
label.scan(/(?<=\w)\@(?:npm:)?([^\s,]+)/).flatten.include?(requirement) | ||
end&.last | ||
parse_details(candidate) | ||
end | ||
end | ||
|
||
private | ||
|
||
sig { params(message: String).void } | ||
def raise_invalid!(message) | ||
raise Dependabot::DependencyFileNotParseable.new(@dependency_file.path, "Invalid bun.lock file: #{message}") | ||
end | ||
|
||
sig do | ||
params(entry: T.nilable(T::Array[T.untyped])).returns(T.nilable(T::Hash[String, T.untyped])) | ||
end | ||
def parse_details(entry) | ||
return unless entry.is_a?(Array) | ||
|
||
# Either: | ||
# - "{name}@{version}", registry, details, integrity | ||
# - "{name}@{resolution}", details | ||
resolution = entry.first | ||
return unless resolution.is_a?(String) | ||
|
||
name, version = resolution.split(/(?<=\w)\@/) | ||
semver = Version.semver_for(version) | ||
|
||
if semver | ||
registry, details, integrity = entry[1..3] | ||
{ | ||
"name" => name, | ||
"version" => semver.to_s, | ||
"registry" => registry, | ||
"details" => details, | ||
"integrity" => integrity | ||
} | ||
else | ||
details = entry[1] | ||
{ | ||
"name" => name, | ||
"resolution" => version, | ||
"details" => details | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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.
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.