Skip to content

Commit

Permalink
Merge pull request #914 from appsignal/rescue-gem-config-load
Browse files Browse the repository at this point in the history
Fix gem proxy config on Ruby <3.2 with psych 5
  • Loading branch information
tombruijn authored Jan 20, 2023
2 parents 576a92c + e1e598a commit 87040a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "fix"
---

Skip the `.gemrc` config during installation if it raises an error loading it. This can be caused when the psych gem version 5 is installed on Ruby < 3.2. Use the `HTTP_PROXY` environment variable instead to configure the HTTP proxy that should be used during installation.
31 changes: 23 additions & 8 deletions ext/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def report
"version" => "#{rbconfig["RUBY_PROGRAM_VERSION"]}-p#{rbconfig["PATCHLEVEL"]}"
},
"download" => {
"checksum" => "unverified",
"http_proxy" => http_proxy
"checksum" => "unverified"
},
"build" => {
"time" => Time.now.utc,
Expand All @@ -54,7 +53,11 @@ def report
d["libc"] = ldd_version if ldd_version
end
}
}
}.tap do |r|
proxy, error = http_proxy
r["download"]["http_proxy"] = proxy
r["download"]["http_proxy_error"] = error if error
end
end
end

Expand Down Expand Up @@ -128,10 +131,11 @@ def download_archive(type)
report["download"]["download_url"] = download_url

begin
proxy, _error = http_proxy
args = [
download_url,
:ssl_ca_cert => CA_CERT_PATH,
:proxy => http_proxy
:proxy => proxy
]
if URI.respond_to?(:open) # rubocop:disable Style/GuardClause
return URI.open(*args)
Expand Down Expand Up @@ -186,14 +190,25 @@ def store_download_version_on_report
end

def http_proxy
proxy = try_http_proxy_value(Gem.configuration[:http_proxy])
return proxy if proxy
proxy, error =
begin
[try_http_proxy_value(Gem.configuration[:http_proxy]), nil]
rescue => error
# Ignore this setting if the `.gemrc` file can't be read. This raises an
# error on Rubies with psych 4 in the standard library, but also have
# psych 5 installed: Ruby < 3.2.
# https://github.com/appsignal/appsignal-ruby/issues/904
[nil, error]
end
return [proxy, error] if proxy

proxy = try_http_proxy_value(ENV["http_proxy"])
return proxy if proxy
return [proxy, error] if proxy

proxy = try_http_proxy_value(ENV["HTTP_PROXY"])
return proxy if proxy
return [proxy, error] if proxy

[nil, error]
end

def try_http_proxy_value(value)
Expand Down

0 comments on commit 87040a6

Please sign in to comment.