diff --git a/lib/bundler/fetcher/compact_index.rb b/lib/bundler/fetcher/compact_index.rb index f249ec1835a..81cd11eee0a 100644 --- a/lib/bundler/fetcher/compact_index.rb +++ b/lib/bundler/fetcher/compact_index.rb @@ -61,6 +61,9 @@ def fetch_spec(spec) def available? # Read info file checksums out of /versions, so we can know if gems are up to date fetch_uri.scheme != "file" && compact_index_client.update_and_parse_checksums! + rescue CompactIndexClient::Updater::MisMatchedChecksumError => e + Bundler.ui.warn(e.message) + nil end compact_index_request :available? diff --git a/lib/bundler/vendor/compact_index_client/lib/compact_index_client.rb b/lib/bundler/vendor/compact_index_client/lib/compact_index_client.rb index e7d67d99268..1679c17c8a3 100644 --- a/lib/bundler/vendor/compact_index_client/lib/compact_index_client.rb +++ b/lib/bundler/vendor/compact_index_client/lib/compact_index_client.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require "pathname" require "set" diff --git a/lib/bundler/vendor/compact_index_client/lib/compact_index_client/cache.rb b/lib/bundler/vendor/compact_index_client/lib/compact_index_client/cache.rb index 57ef5518498..a370fa402d8 100644 --- a/lib/bundler/vendor/compact_index_client/lib/compact_index_client/cache.rb +++ b/lib/bundler/vendor/compact_index_client/lib/compact_index_client/cache.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class Bundler::CompactIndexClient class Cache attr_reader :directory diff --git a/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb b/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb index a3cdc551406..630318a9be5 100644 --- a/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb +++ b/lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb @@ -1,9 +1,21 @@ +# frozen_string_literal: true require "stringio" require "zlib" class Bundler::CompactIndexClient class Updater - class MisMatchedChecksumError < Error; end + class MisMatchedChecksumError < Error + def initialize(path, server_checksum, local_checksum) + @path = path + @server_checksum = server_checksum + @local_checksum = local_checksum + end + + def message + "The checksum of /#{@path} does not match the checksum provided by the server! Something is wrong " \ + "(local checksum is #{@local_checksum.inspect}, was expecting #{@server_checksum.inspect})." + end + end def initialize(fetcher) @fetcher = fetcher @@ -31,20 +43,20 @@ def update(local_path, remote_path, retrying = nil) mode = response.is_a?(Net::HTTPPartialContent) ? "a" : "w" local_path.open(mode) {|f| f << content } - return if etag_for(local_path) == response["ETag"] + response_etag = response["ETag"] + return if etag_for(local_path) == response_etag if retrying.nil? local_path.delete update(local_path, remote_path, :retrying) else - raise MisMatchedChecksumError, "Checksum of /#{remote_path} " \ - "does not match the checksum provided by server! Something is wrong." + raise MisMatchedChecksumError.new(remote_path, response_etag, etag_for(local_path)) end end def etag_for(path) sum = checksum_for_file(path) - sum ? '"' << sum << '"' : nil + sum ? %("#{sum}") : nil end def checksum_for_file(path) diff --git a/lib/bundler/vendor/compact_index_client/lib/compact_index_client/version.rb b/lib/bundler/vendor/compact_index_client/lib/compact_index_client/version.rb index f18c522ae89..64520daead4 100644 --- a/lib/bundler/vendor/compact_index_client/lib/compact_index_client/version.rb +++ b/lib/bundler/vendor/compact_index_client/lib/compact_index_client/version.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true class Bundler::CompactIndexClient - VERSION = "0.1.0" + VERSION = "0.1.0".freeze end diff --git a/spec/install/gems/compact_index_spec.rb b/spec/install/gems/compact_index_spec.rb index 13645f0ae6f..c00e73523be 100644 --- a/spec/install/gems/compact_index_spec.rb +++ b/spec/install/gems/compact_index_spec.rb @@ -139,6 +139,20 @@ should_be_installed "rack 1.0.0" end + it "falls back when the versions endpoint has a checksum mismatch" do + gemfile <<-G + source "#{source_uri}" + gem "rack" + G + + bundle! :install, :verbose => true, :artifice => "compact_index_checksum_mismatch" + expect(out).to include("Fetching gem metadata from #{source_uri}") + expect(out).to include <<-'WARN' +The checksum of /versions does not match the checksum provided by the server! Something is wrong (local checksum is "\"d41d8cd98f00b204e9800998ecf8427e\"", was expecting "\"123\""). + WARN + should_be_installed "rack 1.0.0" + end + it "handles host redirects" do gemfile <<-G source "#{source_uri}" diff --git a/spec/support/artifice/compact_index_checksum_mismatch.rb b/spec/support/artifice/compact_index_checksum_mismatch.rb new file mode 100644 index 00000000000..4ac328736cf --- /dev/null +++ b/spec/support/artifice/compact_index_checksum_mismatch.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true +require File.expand_path("../compact_index", __FILE__) + +Artifice.deactivate + +class CompactIndexChecksumMismatch < CompactIndexAPI + get "/versions" do + headers "ETag" => quote("123") + headers "Surrogate-Control" => "max-age=2592000, stale-while-revalidate=60" + content_type "text/plain" + body "" + end +end + +Artifice.activate_with(CompactIndexChecksumMismatch)