-
Notifications
You must be signed in to change notification settings - Fork 1.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
Bundler 2: [Prerelease] Add native helpers for file parsing #3315
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04881e8
Remove .bundle/config file and set config in build
feelepxyz 6da3790
Add v2 monkey patches
feelepxyz e498646
Add bundler v2 file parser
feelepxyz b9bc877
Add git source file parser spec to v2 helpers
feelepxyz 1975d59
Set up matrix builds for bundler1 and bundler2
feelepxyz 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/.bundle/* | ||
!/.bundle/config | ||
/.bundle | ||
/.env | ||
/tmp | ||
/dependabot-*.gem | ||
|
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 |
---|---|---|
|
@@ -10,7 +10,6 @@ fi | |
|
||
helpers_dir="$(dirname "${BASH_SOURCE[0]}")" | ||
cp -r \ | ||
"$helpers_dir/.bundle" \ | ||
"$helpers_dir/lib" \ | ||
"$helpers_dir/run.rb" \ | ||
"$helpers_dir/Gemfile" \ | ||
|
@@ -20,4 +19,5 @@ cd "$install_dir" | |
|
||
# NOTE: Sets `BUNDLED WITH` to match the installed v1 version in Gemfile.lock | ||
# forcing specs and native helpers to run with the same version | ||
BUNDLER_VERSION=2 bundle install | ||
BUNDLER_VERSION=2 bundle config set --local path ".bundle" | ||
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. Nice! |
||
BUNDLER_VERSION=2 bundle install --without test |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module Functions | ||
class FileParser | ||
def initialize(lockfile_name:) | ||
@lockfile_name = lockfile_name | ||
end | ||
|
||
attr_reader :lockfile_name | ||
|
||
def parsed_gemfile(gemfile_name:) | ||
Bundler::Definition.build(gemfile_name, nil, {}). | ||
dependencies.select(&:current_platform?). | ||
reject { |dep| dep.source.is_a?(Bundler::Source::Gemspec) }. | ||
map(&method(:serialize_bundler_dependency)) | ||
end | ||
|
||
def parsed_gemspec(gemspec_name:) | ||
Bundler.load_gemspec_uncached(gemspec_name). | ||
dependencies. | ||
map(&method(:serialize_bundler_dependency)) | ||
end | ||
|
||
private | ||
|
||
def lockfile | ||
return @lockfile if defined?(@lockfile) | ||
|
||
@lockfile = | ||
feelepxyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
begin | ||
return unless lockfile_name && File.exist?(lockfile_name) | ||
|
||
File.read(lockfile_name) | ||
end | ||
end | ||
|
||
def parsed_lockfile | ||
return unless lockfile | ||
|
||
@parsed_lockfile ||= Bundler::LockfileParser.new(lockfile) | ||
end | ||
|
||
def source_from_lockfile(dependency_name) | ||
parsed_lockfile&.specs.find { |s| s.name == dependency_name }&.source | ||
end | ||
|
||
def source_for(dependency) | ||
source = dependency.source | ||
if lockfile && default_rubygems?(source) | ||
# If there's a lockfile and the Gemfile doesn't have anything | ||
# interesting to say about the source, check that. | ||
source = source_from_lockfile(dependency.name) | ||
end | ||
raise "Bad source: #{source}" unless sources.include?(source.class) | ||
|
||
return nil if default_rubygems?(source) | ||
|
||
details = { type: source.class.name.split("::").last.downcase } | ||
if source.is_a?(Bundler::Source::Git) | ||
details.merge!(git_source_details(source)) | ||
end | ||
if source.is_a?(Bundler::Source::Rubygems) | ||
details[:url] = source.remotes.first.to_s | ||
end | ||
details | ||
end | ||
|
||
# TODO: Remove default `master` branch | ||
def git_source_details(source) | ||
{ | ||
url: source.uri, | ||
branch: source.branch || "master", | ||
ref: source.ref || "master" | ||
} | ||
end | ||
|
||
def default_rubygems?(source) | ||
return true if source.nil? | ||
return false unless source.is_a?(Bundler::Source::Rubygems) | ||
|
||
source.remotes.any? { |r| r.to_s.include?("rubygems.org") } | ||
end | ||
|
||
def serialize_bundler_dependency(dependency) | ||
{ | ||
name: dependency.name, | ||
requirement: dependency.requirement, | ||
groups: dependency.groups, | ||
source: source_for(dependency), | ||
type: dependency.type | ||
} | ||
end | ||
|
||
# Can't be a constant because some of these don't exist in bundler | ||
# 1.15, which used to cause issues on Heroku (causing exception on boot). | ||
# TODO: Check if this will be an issue with multiple bundler versions | ||
def sources | ||
[ | ||
NilClass, | ||
Bundler::Source::Rubygems, | ||
Bundler::Source::Git, | ||
Bundler::Source::Path, | ||
Bundler::Source::Gemspec, | ||
Bundler::Source::Metadata | ||
] | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
bundler/helpers/v2/monkey_patches/definition_bundler_version_patch.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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/definition" | ||
|
||
# Ignore the Bundler version specified in the Gemfile (since the only Bundler | ||
# version available to us is the one we're using). | ||
module BundlerDefinitionBundlerVersionPatch | ||
def expanded_dependencies | ||
@expanded_dependencies ||= | ||
expand_dependencies(dependencies + metadata_dependencies, @remote). | ||
reject { |d| d.name == "bundler" } | ||
end | ||
end | ||
|
||
Bundler::Definition.prepend(BundlerDefinitionBundlerVersionPatch) |
20 changes: 20 additions & 0 deletions
20
bundler/helpers/v2/monkey_patches/definition_ruby_version_patch.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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/definition" | ||
|
||
module BundlerDefinitionRubyVersionPatch | ||
def index | ||
@index ||= super.tap do | ||
if ruby_version | ||
requested_version = ruby_version.to_gem_version_with_patchlevel | ||
sources.metadata_source.specs << | ||
Gem::Specification.new("ruby\0", requested_version) | ||
end | ||
|
||
sources.metadata_source.specs << | ||
Gem::Specification.new("ruby\0", "2.5.3p105") | ||
end | ||
end | ||
end | ||
|
||
Bundler::Definition.prepend(BundlerDefinitionRubyVersionPatch) |
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/source" | ||
|
||
module Bundler | ||
class Source | ||
class Git | ||
class GitProxy | ||
private | ||
|
||
# Bundler allows ssh authentication when talking to GitHub but there's | ||
# no way for Dependabot to do so (it doesn't have any ssh keys). | ||
# Instead, we convert all `git@github.com:` URLs to use HTTPS. | ||
def configured_uri_for(uri) | ||
uri = uri.gsub(%r{git@(.*?):/?}, 'https://\1/') | ||
if /https?:/ =~ uri | ||
remote = Bundler::URI(uri) | ||
config_auth = Bundler.settings[remote.to_s] || Bundler.settings[remote.host] | ||
remote.userinfo ||= config_auth | ||
remote.to_s | ||
else | ||
uri | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Bundler | ||
class Source | ||
class Git < Path | ||
private | ||
|
||
def serialize_gemspecs_in(destination) | ||
original_load_paths = $LOAD_PATH.dup | ||
reduced_load_paths = original_load_paths. | ||
reject { |p| p.include?("/gems/") } | ||
|
||
$LOAD_PATH.shift until $LOAD_PATH.empty? | ||
reduced_load_paths.each { |p| $LOAD_PATH << p } | ||
|
||
if destination.relative? | ||
destination = destination.expand_path(Bundler.root) | ||
end | ||
Dir["#{destination}/#{@glob}"].each do |spec_path| | ||
# Evaluate gemspecs and cache the result. Gemspecs | ||
# in git might require git or other dependencies. | ||
# The gemspecs we cache should already be evaluated. | ||
spec = Bundler.load_gemspec(spec_path) | ||
next unless spec | ||
|
||
Bundler.rubygems.set_installed_by_version(spec) | ||
Bundler.rubygems.validate(spec) | ||
File.open(spec_path, "wb") { |file| file.write(spec.to_ruby) } | ||
end | ||
$LOAD_PATH.shift until $LOAD_PATH.empty? | ||
original_load_paths.each { |p| $LOAD_PATH << p } | ||
end | ||
end | ||
end | ||
end |
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.
@brrygrdn @jurre looks like you can add hashes as the suite argument, thoughts on doing something like this?
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.
Yep, I dig it!
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.
🎉 Nice
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.
Ah, the only downside of this approach is it seems to mess with our configured required builds, we'll need to flip the required builds from the old set to the new ones, merge this and then force all other PRs to merge master.
Not a big deal overall, as I think this is ultimately a better approach, just a one-time pain to switch over.
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.
Good catch! Will fix that up 👍