From a53c9b7a714ae754f1b0ad81ed2c4af8f915676a Mon Sep 17 00:00:00 2001 From: Adrien Date: Wed, 1 Oct 2014 12:33:34 +0200 Subject: [PATCH] Don't swallow LoadErrors when requiring a dashed gem This looks like #1807 when raising a LoadError in a required gem, bundler will rescue it to try and see if it came from his own namespaced require or from the required file. If it comes from the dashed require it will re-raise the original exception in case the gem was dashed but the first require was ok but raised a LoadError after. However, the exception is swallowed if we require a dashed gem without the proper file name, for example gem name : 'foo-bar' and file architecture : 'foo/bar.rb' This PR raise the exception back in case it really comes from inside the namespaced file. --- lib/bundler/runtime.rb | 5 ++--- spec/runtime/require_spec.rb | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb index b09a8814e51..d596c84815a 100644 --- a/lib/bundler/runtime.rb +++ b/lib/bundler/runtime.rb @@ -83,11 +83,10 @@ def require(*groups) begin namespaced_file = dep.name.gsub('-', '/') Kernel.require namespaced_file - rescue LoadError + rescue LoadError => e REGEXPS.find { |r| r =~ e.message } regex_name = $1 - raise e if dep.autorequire || (regex_name && regex_name.gsub('-', '/') != namespaced_file) - raise e if regex_name.nil? + raise if regex_name != namespaced_file end end end diff --git a/spec/runtime/require_spec.rb b/spec/runtime/require_spec.rb index e1c93897a52..5e5e9db22fb 100644 --- a/spec/runtime/require_spec.rb +++ b/spec/runtime/require_spec.rb @@ -98,6 +98,28 @@ expect(err).to eq("ZOMG LOAD ERROR") end + it "doesn't swallow the error when the library has an unrelated error" do + build_lib "loadfuuu", "1.0.0" do |s| + s.write "lib/loadfuuu.rb", "raise LoadError.new(\"cannot load such file -- load-bar\")" + end + + gemfile <<-G + path "#{lib_path}" + gem "loadfuuu" + G + + cmd = <<-RUBY + begin + Bundler.require + rescue LoadError => e + $stderr.puts "ZOMG LOAD ERROR: \#{e.message}" + end + RUBY + run(cmd, :expect_err => true) + + expect(err).to eq("ZOMG LOAD ERROR: cannot load such file -- load-bar") + end + describe "with namespaced gems" do before :each do build_lib "jquery-rails", "1.0.0" do |s| @@ -170,8 +192,9 @@ it "doesn't swallow the error when the library has an unrelated error" do build_lib "load-fuuu", "1.0.0" do |s| - s.write "lib/load-fuuu.rb", "raise LoadError.new(\"cannot load such file -- load-bar\")" + s.write "lib/load/fuuu.rb", "raise LoadError.new(\"cannot load such file -- load-bar\")" end + lib_path('load-fuuu-1.0.0/lib/load-fuuu.rb').rmtree gemfile <<-G path "#{lib_path}"