From 72591eb73f589f135d7c1c1e07a98d3820001213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 2 Mar 2023 19:11:19 +0100 Subject: [PATCH] [rubygems/rubygems] Better suggestion when `bundler/setup` fails due to missing gems MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the original `BUNDLE_GEMFILE` is different from the default, then the suggestion wouldn't work as is. Before: ``` $ util/rubocop Could not find rubocop-1.30.1 in locally installed gems Run `bundle install` to install missing gems. $ rubygems git:(better-cmd-suggestion) ✗ bundle install Could not locate Gemfile ``` After: ``` $ util/rubocop Could not find rubocop-1.30.1 in locally installed gems Run `bundle install --gemfile /path/to/rubygems/bundler/tool/bundler/lint_gems.rb` to install missing gems. $ bundle install --gemfile /path/to/rubygems/bundler/tool/bundler/lint_gems.rb Fetching gem metadata from https://rubygems.org/......... Using ast 2.4.2 Using bundler 2.4.7 Using parser 3.1.2.0 Using rainbow 3.1.1 Using parallel 1.22.1 Using regexp_parser 2.5.0 Using rubocop-ast 1.18.0 Using rexml 3.2.5 Using ruby-progressbar 1.11.0 Using unicode-display_width 2.1.0 Fetching rubocop 1.30.1 Installing rubocop 1.30.1 Using rubocop-performance 1.14.2 Bundle complete! 2 Gemfile dependencies, 12 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. $ util/rubocop Inspecting 345 files ......................................................................................................................................................................................................................................................................................................................................................... 345 files inspected, no offenses detected ``` https://github.com/rubygems/rubygems/commit/bf1320d805 --- lib/bundler/setup.rb | 5 ++++- spec/bundler/commands/exec_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/bundler/setup.rb b/lib/bundler/setup.rb index 32e9b2d7c0..801fd5312a 100644 --- a/lib/bundler/setup.rb +++ b/lib/bundler/setup.rb @@ -12,7 +12,10 @@ if Bundler::SharedHelpers.in_bundle? Bundler.ui.error e.message Bundler.ui.warn e.backtrace.join("\n") if ENV["DEBUG"] if e.is_a?(Bundler::GemNotFound) - Bundler.ui.warn "Run `bundle install` to install missing gems." + suggested_cmd = "bundle install" + original_gemfile = Bundler.original_env["BUNDLE_GEMFILE"] + suggested_cmd += " --gemfile #{original_gemfile}" if original_gemfile + Bundler.ui.warn "Run `#{suggested_cmd}` to install missing gems." end exit e.status_code end diff --git a/spec/bundler/commands/exec_spec.rb b/spec/bundler/commands/exec_spec.rb index 5ca11dcba2..099cdb39fa 100644 --- a/spec/bundler/commands/exec_spec.rb +++ b/spec/bundler/commands/exec_spec.rb @@ -916,6 +916,30 @@ Run `bundle install` to install missing gems. end end + context "when Bundler.setup fails and Gemfile is not the default" do + before do + create_file "CustomGemfile", <<-G + source "#{file_uri_for(gem_repo1)}" + gem 'rack', '2' + G + ENV["BUNDLER_FORCE_TTY"] = "true" + ENV["BUNDLE_GEMFILE"] = "CustomGemfile" + ENV["BUNDLER_ORIG_BUNDLE_GEMFILE"] = nil + end + + let(:exit_code) { Bundler::GemNotFound.new.status_code } + let(:expected) { "" } + + it "prints proper suggestion" do + skip "https://github.com/rubygems/rubygems/issues/3351" if Gem.win_platform? + + subject + expect(exitstatus).to eq(exit_code) + expect(err).to include("Run `bundle install --gemfile CustomGemfile` to install missing gems.") + expect(out).to eq(expected) + end + end + context "when the executable exits non-zero via at_exit" do let(:executable) { super() + "\n\nat_exit { $! ? raise($!) : exit(1) }" } let(:exit_code) { 1 }