Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Auto merge of #4477 - bundler:seg-checksum-mismatch-error, r=indirect
Browse files Browse the repository at this point in the history
[Updater] Raise a more helpful error on checksum mismatch

@indirect this will make diagnosing #4472 much easier.
  • Loading branch information
homu authored and segiddins committed Apr 30, 2016
1 parent 10ddd8a commit 5f5b8e8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/bundler/fetcher/compact_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require "pathname"
require "set"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class Bundler::CompactIndexClient
class Cache
attr_reader :directory
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class Bundler::CompactIndexClient
VERSION = "0.1.0"
VERSION = "0.1.0".freeze
end
14 changes: 14 additions & 0 deletions spec/install/gems/compact_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
15 changes: 15 additions & 0 deletions spec/support/artifice/compact_index_checksum_mismatch.rb
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5f5b8e8

Please sign in to comment.