Skip to content
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 version resolver #3319

Merged
merged 5 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
9 changes: 8 additions & 1 deletion bundler/helpers/v2/lib/functions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "functions/file_parser"
require "functions/version_resolver"

module Functions
class NotImplementedError < StandardError; end
Expand Down Expand Up @@ -52,7 +53,13 @@ def self.private_registry_versions(gemfile_name:, dependency_name:, dir:,
def self.resolve_version(dependency_name:, dependency_requirements:,
gemfile_name:, lockfile_name:, using_bundler2:,
dir:, credentials:)
raise NotImplementedError, "Bundler 2 adapter does not yet implement #{__method__}"
set_bundler_flags_and_credentials(dir: dir, credentials: credentials, using_bundler2: using_bundler2)
VersionResolver.new(
dependency_name: dependency_name,
dependency_requirements: dependency_requirements,
gemfile_name: gemfile_name,
lockfile_name: lockfile_name
).version_details
end

def self.jfrog_source(dir:, gemfile_name:, credentials:, using_bundler2:)
Expand Down
140 changes: 140 additions & 0 deletions bundler/helpers/v2/lib/functions/version_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
module Functions
class VersionResolver
GEM_NOT_FOUND_ERROR_REGEX = /locked to (?<name>[^\s]+) \(/.freeze

attr_reader :dependency_name, :dependency_requirements,
:gemfile_name, :lockfile_name

def initialize(dependency_name:, dependency_requirements:,
gemfile_name:, lockfile_name:)
@dependency_name = dependency_name
@dependency_requirements = dependency_requirements
@gemfile_name = gemfile_name
@lockfile_name = lockfile_name
end

def version_details
dep = dependency_from_definition

# If the dependency wasn't found in the definition, but *is*
# included in a gemspec, it's because the Gemfile didn't import
# the gemspec. This is unusual, but the correct behaviour if/when
# it happens is to behave as if the repo was gemspec-only.
if dep.nil? && dependency_requirements.any?
return "latest"
end

# Otherwise, if the dependency wasn't found it's because it is a
# subdependency that was removed when attempting to update it.
return nil if dep.nil?

# If the dependency is Bundler itself then we can't trust the
# version that has been returned (it's the version Dependabot is
# running on, rather than the true latest resolvable version).
return nil if dep.name == "bundler"

details = {
version: dep.version,
ruby_version: ruby_version,
fetcher: fetcher_class(dep)
}
if dep.source.instance_of?(::Bundler::Source::Git)
details[:commit_sha] = dep.source.revision
end
details
end

private

# rubocop:disable Metrics/PerceivedComplexity
def dependency_from_definition(unlock_subdependencies: true)
dependencies_to_unlock = [dependency_name]
dependencies_to_unlock += subdependencies if unlock_subdependencies
begin
definition = build_definition(dependencies_to_unlock)
definition.resolve_remotely!
rescue ::Bundler::GemNotFound => e
unlock_yanked_gem(dependencies_to_unlock, e) && retry
rescue ::Bundler::HTTPError => e
# Retry network errors
# Note: in_a_native_bundler_context will also retry `Bundler::HTTPError` errors
# up to three times meaning we'll end up retrying this error up to six times
# TODO: Could we get rid of this retry logic and only rely on
# SharedBundlerHelpers.in_a_native_bundler_context
attempt ||= 1
attempt += 1
raise if attempt > 3 || !e.message.include?("Network error")

retry
end

dep = definition.resolve.find { |d| d.name == dependency_name }
return dep if dep
return if dependency_requirements.any? || !unlock_subdependencies

# If no definition was found and we're updating a sub-dependency,
# try again but without unlocking any other sub-dependencies
dependency_from_definition(unlock_subdependencies: false)
end
# rubocop:enable Metrics/PerceivedComplexity

def subdependencies
# If there's no lockfile we don't need to worry about
# subdependencies
return [] unless lockfile

all_deps = ::Bundler::LockfileParser.new(lockfile).
specs.map(&:name).map(&:to_s).uniq
top_level = build_definition([]).dependencies.
map(&:name).map(&:to_s)

all_deps - top_level
end

def build_definition(dependencies_to_unlock)
# Note: we lock shared dependencies to avoid any top-level
# dependencies getting unlocked (which would happen if they were
# also subdependencies of the dependency being unlocked)
::Bundler::Definition.build(
gemfile_name,
lockfile_name,
gems: dependencies_to_unlock,
lock_shared_dependencies: true
)
end

def unlock_yanked_gem(dependencies_to_unlock, error)
raise unless error.message.match?(GEM_NOT_FOUND_ERROR_REGEX)

gem_name = error.message.match(GEM_NOT_FOUND_ERROR_REGEX).
named_captures["name"]
raise if dependencies_to_unlock.include?(gem_name)

dependencies_to_unlock << gem_name
end

def lockfile
return @lockfile if defined?(@lockfile)

@lockfile =
begin
return unless lockfile_name
return unless File.exist?(lockfile_name)

File.read(lockfile_name)
end
end

def fetcher_class(dep)
return unless dep.source.is_a?(::Bundler::Source::Rubygems)

dep.source.fetchers.first.fetchers.first.class.to_s
end

def ruby_version
return nil unless gemfile_name

@ruby_version ||= build_definition([]).ruby_version&.gem_version
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def index
if ruby_version
requested_version = ruby_version.to_gem_version_with_patchlevel
sources.metadata_source.specs <<
Gem::Specification.new("ruby\0", requested_version)
Gem::Specification.new("Ruby\0", requested_version)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it changed here: rubygems/rubygems@4e950f5

end

sources.metadata_source.specs <<
Gem::Specification.new("ruby\0", "2.5.3p105")
Gem::Specification.new("Ruby\0", "2.5.3p105")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion bundler/helpers/v2/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "json"

$LOAD_PATH.unshift(File.expand_path("./lib", __dir__))
$LOAD_PATH.unshift(File.expand_path("../v1/monkey_patches", __dir__))
$LOAD_PATH.unshift(File.expand_path("./monkey_patches", __dir__))

# Bundler monkey patches
require "definition_ruby_version_patch"
Expand Down
97 changes: 97 additions & 0 deletions bundler/helpers/v2/spec/functions/version_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

require "native_spec_helper"
require "shared_contexts"

RSpec.describe Functions::VersionResolver do
include_context "in a temporary bundler directory"
include_context "stub rubygems compact index"

let(:version_resolver) do
described_class.new(
dependency_name: dependency_name,
dependency_requirements: dependency_requirements,
gemfile_name: "Gemfile",
lockfile_name: "Gemfile.lock"
)
end

let(:dependency_name) { "business" }
let(:dependency_requirements) do
[{
file: "Gemfile",
requirement: requirement_string,
groups: [],
source: source
}]
end
let(:source) { nil }

let(:rubygems_url) { "https://index.rubygems.org/api/v1/" }
let(:old_index_url) { rubygems_url + "dependencies" }

describe "#version_details" do
subject do
in_tmp_folder { version_resolver.version_details }
end

let(:project_name) { "gemfile" }
let(:requirement_string) { " >= 0" }

its([:version]) { is_expected.to eq(Gem::Version.new("1.4.0")) }
its([:fetcher]) { is_expected.to eq("Bundler::Fetcher::CompactIndex") }

context "with a private gemserver source" do
include_context "stub rubygems compact index"

let(:project_name) { "specified_source" }
let(:requirement_string) { ">= 0" }

before do
gemfury_url = "https://repo.fury.io/greysteil/"
gemfury_deps_url = gemfury_url + "api/v1/dependencies"

stub_request(:get, gemfury_url + "versions").
to_return(status: 200, body: fixture("ruby", "gemfury-index"))
stub_request(:get, gemfury_url + "info/business").to_return(status: 404)
stub_request(:get, gemfury_deps_url).to_return(status: 200)
stub_request(:get, gemfury_deps_url + "?gems=business,statesman").
to_return(status: 200, body: fixture("ruby", "gemfury_response"))
stub_request(:get, gemfury_deps_url + "?gems=business").
to_return(status: 200, body: fixture("ruby", "gemfury_response"))
stub_request(:get, gemfury_deps_url + "?gems=statesman").
to_return(status: 200, body: fixture("ruby", "gemfury_response"))
end

its([:version]) { is_expected.to eq(Gem::Version.new("1.9.0")) }
its([:fetcher]) { is_expected.to eq("Bundler::Fetcher::Dependency") }
end

context "with a git source" do
let(:project_name) { "git_source" }

its([:version]) { is_expected.to eq(Gem::Version.new("1.6.0")) }
its([:fetcher]) { is_expected.to be_nil }
end

context "when Bundler's compact index is down" do
before do
stub_request(:get, "https://index.rubygems.org/versions").
to_return(status: 500, body: "We'll be back soon")
stub_request(:get, "https://index.rubygems.org/info/public_suffix").
to_return(status: 500, body: "We'll be back soon")
stub_request(:get, old_index_url).to_return(status: 200)
stub_request(:get, old_index_url + "?gems=business,statesman").
to_return(
status: 200,
body: fixture("ruby",
"rubygems_responses",
"dependencies-default-gemfile")
)
end

its([:version]) { is_expected.to eq(Gem::Version.new("1.4.0")) }
its([:fetcher]) { is_expected.to eq("Bundler::Fetcher::Dependency") }
end
end
end
2 changes: 0 additions & 2 deletions bundler/helpers/v2/spec/functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
depencency_source_latest_git_version: [ :gemfile_name, :dependency_name, :dir, :credentials, :dependency_source_url,
:dependency_source_branch ],
private_registry_versions: [:gemfile_name, :dependency_name, :dir, :credentials ],
resolve_version: [:dependency_name, :dependency_requirements, :gemfile_name, :lockfile_name, :using_bundler2,
:dir, :credentials],
jfrog_source: [:dir, :gemfile_name, :credentials, :using_bundler2],
git_specs: [:dir, :gemfile_name, :credentials, :using_bundler2],
conflicting_dependencies: [:dir, :dependency_name, :target_version, :lockfile_name, :using_bundler2, :credentials]
Expand Down
25 changes: 0 additions & 25 deletions bundler/spec/dependabot/bundler/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1708,29 +1708,4 @@
end
end
end

context "with bundler 2 support enabled" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These specs fail because the v2 version resolver is used and returns errors.

let(:updater) do
described_class.new(
dependency_files: dependency_files,
dependencies: dependencies,
credentials: [{
"type" => "git_source",
"host" => "github.com"
}],
repo_contents_path: repo_contents_path,
options: {
bundler_2_available: true
}
)
end

describe "updated_dependency_files" do
it "fails as the native helper is not yet implemented" do
expect { updater.updated_dependency_files }.
to raise_error(Dependabot::NotImplemented,
"Bundler 2 adapter does not yet implement update_lockfile")
end
end
end
end
Loading