From 7b33647cd1dffd169c75da95f0491a22479ef4af Mon Sep 17 00:00:00 2001 From: Agis Anastasopoulos Date: Wed, 28 Oct 2015 15:30:22 +0200 Subject: [PATCH] Handle invalid RubyGems config files Fixes #4042. --- lib/bundler.rb | 11 +++++++++++ lib/bundler/friendly_errors.rb | 4 ++++ lib/bundler/rubygems_integration.rb | 4 ++++ spec/bundler/friendly_errors_spec.rb | 27 +++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/lib/bundler.rb b/lib/bundler.rb index f4373662ba5..b00887392ab 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -120,6 +120,17 @@ def message status_code(23) end + class YAMLSyntaxError < BundlerError + attr_reader :orig_exception + + def initialize(orig_exception, msg) + super(msg) + @orig_exception = orig_exception + end + + status_code(25) + end + class << self attr_writer :bundle_path diff --git a/lib/bundler/friendly_errors.rb b/lib/bundler/friendly_errors.rb index e60f73f7dfe..f6f8fd5623e 100644 --- a/lib/bundler/friendly_errors.rb +++ b/lib/bundler/friendly_errors.rb @@ -5,6 +5,10 @@ module Bundler def self.with_friendly_errors yield + rescue YAMLSyntaxError => e + Bundler.ui.error e.message + Bundler.ui.trace e.orig_exception + exit e.status_code rescue Bundler::Dsl::DSLError => e Bundler.ui.error e.message exit e.status_code diff --git a/lib/bundler/rubygems_integration.rb b/lib/bundler/rubygems_integration.rb index f4c2a12d9d1..648616acf03 100644 --- a/lib/bundler/rubygems_integration.rb +++ b/lib/bundler/rubygems_integration.rb @@ -1,6 +1,7 @@ require "monitor" require "rubygems" require "rubygems/config_file" +require "bundler/psyched_yaml" module Bundler class RubygemsIntegration @@ -67,6 +68,9 @@ def configuration Bundler.ui.error "#{e.class}: #{e.message}" Bundler.ui.trace e raise + rescue YamlSyntaxError => e + raise YAMLSyntaxError.new(e, "Your RubyGems configuration, which is " \ + "usually located in ~/.gemrc, contains invalid YAML syntax.") end def ruby_engine diff --git a/spec/bundler/friendly_errors_spec.rb b/spec/bundler/friendly_errors_spec.rb index ee0667cb2f5..2eca85eb84d 100644 --- a/spec/bundler/friendly_errors_spec.rb +++ b/spec/bundler/friendly_errors_spec.rb @@ -3,6 +3,33 @@ require "bundler/friendly_errors" describe Bundler, "friendly errors" do + context "with invalid YAML in .gemrc" do + before do + File.open(Gem.configuration.config_file_name, "w") do |f| + f.write "invalid: yaml: hah" + end + end + + after do + FileUtils.rm(Gem.configuration.config_file_name) + end + + it "reports a relevant friendly error message", :ruby => ">= 1.9" do + gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + G + + bundle :install, :env => { "DEBUG" => true } + + expect(out).to include("Your RubyGems configuration") + expect(out).to include("invalid YAML syntax") + expect(out).to include("Psych::SyntaxError") + expect(out).not_to include("ERROR REPORT TEMPLATE") + expect(exitstatus).to eq(25) if exitstatus + end + end + it "rescues Thor::AmbiguousTaskError and raises SystemExit" do expect { Bundler.with_friendly_errors do