ruby/lib/bundler/setup.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

34 строки
1.2 KiB
Ruby
Исходник Обычный вид История

# frozen_string_literal: true
require_relative "shared_helpers"
if Bundler::SharedHelpers.in_bundle?
require_relative "../bundler"
if STDOUT.tty? || ENV["BUNDLER_FORCE_TTY"]
begin
Bundler.ui.silence { Bundler.setup }
rescue Bundler::BundlerError => e
Bundler.ui.error e.message
Bundler.ui.warn e.backtrace.join("\n") if ENV["DEBUG"]
if e.is_a?(Bundler::GemNotFound)
default_bundle = Gem.bin_path("bundler", "bundle")
current_bundle = Bundler::SharedHelpers.bundle_bin_path
suggested_bundle = default_bundle == current_bundle ? "bundle" : current_bundle
[rubygems/rubygems] Improve install advice when some gems are not found This problem is quite specific to our dev environment, but I guess the fix could be handy for other situations. After merging a change to treat default gems as regular gems, I get this when trying to run `rubocop` on our repo: ``` $ bin/rubocop --only Performance/RegexpMatch Could not find json-2.6.3 in locally installed gems Run `bundle install --gemfile /Users/deivid/code/rubygems/rubygems/tool/bundler/lint_gems.rb` to install missing gems. ``` However, when running the suggested command, nothing changes and I still get the same error: ``` $ bundle install --gemfile /Users/deivid/code/rubygems/rubygems/tool/bundler/lint_gems.rb Using ast 2.4.2 Using bundler 2.4.10 Using json 2.6.3 Using parallel 1.23.0 Using racc 1.7.1 Using parser 3.2.2.3 Using rainbow 3.1.1 Using regexp_parser 2.8.1 Using rexml 3.2.5 Using rubocop-ast 1.29.0 Using ruby-progressbar 1.13.0 Using unicode-display_width 2.4.2 Using rubocop 1.52.1 Using rubocop-performance 1.14.2 Bundle complete! 2 Gemfile dependencies, 14 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. $ bin/rubocop --only Performance/RegexpMatch Could not find json-2.6.3 in locally installed gems Run `bundle install --gemfile /Users/deivid/code/rubygems/rubygems/tool/bundler/lint_gems.rb` to install missing gems. ``` The problem is that our `bin/rubocop` script uses the development version of Bundler (which has the change causing the problem), but the advice recommands the default version of Bundler, which does not yet have the change. This commit changes the advice to recommend to use the same version of Bundler that run into the problem in the first place. So in the above situation you now get: ``` $ bin/rubocop --only Performance/RegexpMatch Could not find json-2.6.3 in locally installed gems Run `/Users/deivid/code/rubygems/rubygems/bundler/exe/bundle install --gemfile /Users/deivid/code/rubygems/rubygems/tool/bundler/lint_gems.rb` to install missing gems. ``` And running that fixes the problem: ``` $ /Users/deivid//rubygems/rubygems/bundler/exe/bundle install --gemfile /Users/deivid/code/rubygems/rubygems/tool/bundler/lint_gems.rb Fetching gem metadata from https://rubygems.org/......... Fetching json 2.6.3 Installing json 2.6.3 with native extensions Bundle complete! 2 Gemfile dependencies, 14 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. ``` https://github.com/rubygems/rubygems/commit/10a9588c6d
2023-12-13 16:42:21 +03:00
suggested_cmd = "#{suggested_bundle} install"
[rubygems/rubygems] Better suggestion when `bundler/setup` fails due to missing gems 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
2023-03-02 21:11:19 +03:00
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
else
Bundler.ui.silence { Bundler.setup }
end
# We might be in the middle of shelling out to rubygems
# (RUBYOPT=-rbundler/setup), so we need to give rubygems the opportunity of
# not being silent.
Gem::DefaultUserInteraction.ui = nil
end