Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninitialized constant Bundler #12848

Closed
seandilda opened this issue Apr 16, 2024 · 4 comments · Fixed by #12849
Closed

uninitialized constant Bundler #12848

seandilda opened this issue Apr 16, 2024 · 4 comments · Fixed by #12849
Labels

Comments

@seandilda
Copy link

I'm getting the error uninitialized constant Bundler in my rubocop runs with rubocop.
This appears to only happen when using rubocop-rails and rubocop is called from outside of bundle exec.


Expected behavior

Rubocop should run without a backtrace

Actual behavior

root@1ed01ff37ac2:/tmp/rubocop-bug# rubocop --require rubocop-rails --debug
For /tmp/rubocop-bug: Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /tmp/rubocop-bug/Gemfile
.

0 files inspected, no offenses detected
uninitialized constant Bundler

                  rescue ::Bundler::BundlerError
                         ^^^^^^^^^
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/lockfile.rb:67:in `rescue in parser'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/lockfile.rb:64:in `parser'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/lockfile.rb:38:in `gem_versions'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/config.rb:332:in `read_gem_versions_from_target_lockfile'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/config.rb:293:in `gem_versions_in_target'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/config.rb:309:in `read_rails_version_from_bundler_lock_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/config.rb:304:in `target_rails_version_from_bundler_lock_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/config.rb:255:in `target_rails_version'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cop/base.rb:266:in `target_rails_version'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cop/team.rb:182:in `support_target_rails_version?'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cop/team.rb:166:in `block in roundup_relevant_cops'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cop/team.rb:161:in `select'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cop/team.rb:161:in `roundup_relevant_cops'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cop/team.rb:88:in `investigate'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:348:in `block in inspect_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:347:in `each'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:347:in `flat_map'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:347:in `inspect_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:290:in `block in do_inspection_loop'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:324:in `block in iterate_until_no_changes'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:317:in `loop'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:317:in `iterate_until_no_changes'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:286:in `do_inspection_loop'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:167:in `block in file_offenses'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:192:in `file_offense_cache'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:166:in `file_offenses'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:157:in `process_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:138:in `block in each_inspected_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:137:in `each'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:137:in `reduce'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:137:in `each_inspected_file'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:123:in `inspect_files'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/runner.rb:76:in `run'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli/command.rb:11:in `run'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli/environment.rb:18:in `run'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli.rb:118:in `run_command'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli.rb:125:in `execute_runners'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli.rb:51:in `block in run'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli.rb:77:in `profile_if_needed'
/usr/local/bundle/gems/rubocop-1.63.2/lib/rubocop/cli.rb:43:in `run'
/usr/local/bundle/gems/rubocop-1.63.2/exe/rubocop:19:in `block in <top (required)>'
/usr/local/lib/ruby/3.1.0/benchmark.rb:311:in `realtime'
/usr/local/bundle/gems/rubocop-1.63.2/exe/rubocop:19:in `<top (required)>'
/usr/local/bundle/bin/rubocop:25:in `load'
/usr/local/bundle/bin/rubocop:25:in `<main>'
Finished in 0.18401391700172098 seconds
root@1ed01ff37ac2:/tmp/rubocop-bug# 

Steps to reproduce the problem

I ran these commands in a fresh container using the ruby:3.1 docker image

mkdir /tmp/rubocop-bug
cd /tmp/rubocop-bug
gem install rubocop:1.63.2 rubocop-rails:2.24.1
echo 'source "https://rubygems.org"' > Gemfile
echo 'gem "resolv"' >> Gemfile
bundle install
rubocop --require rubocop-rails

RuboCop version

root@1ed01ff37ac2:/tmp/rubocop-bug# rubocop --require rubocop-rails -V
1.63.2 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.1.4) [x86_64-linux]
  - rubocop-rails 2.24.1
root@1ed01ff37ac2:/tmp/rubocop-bug#
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 16, 2024

@koic @amomchilov This issue refuses to go away! 😅

koic added a commit to koic/rubocop that referenced this issue Apr 16, 2024
Fixes rubocop#12848.

This PR fixes an error that occurs in `RuboCop::Lockfile`
when the constant Bundler is uninitialized.
@koic
Copy link
Member

koic commented Apr 16, 2024

@bbatsov I'm sorry. #12847 was not sufficiently fixed. I've opened #12849 to resolve this error.

@koic koic added the bug label Apr 16, 2024
@seandilda
Copy link
Author

Thanks for the quick turn around on a PR.

May I ask why this route was chosen instead of putting in the code to require bundler?
It seems to me that if you have the bundler gem installed, it should use that to parse the lockfile. If I understand the code correctly, it will only check the lockfile if called from bundle exec. This means rubocop quietly behaves differently in the two different environments, which would be very surprising to users.

@koic
Copy link
Member

koic commented Apr 19, 2024

Exactly, but the existence of Gemfile.lock and running via Bundler with bundle exec should be considered separately. In other words, if bundle exec is not used, it is not expected to run through Bundler. I don't see any use cases where this difference in behavior could lead to issues.

bbatsov pushed a commit that referenced this issue Apr 19, 2024
Fixes #12848.

This PR fixes an error that occurs in `RuboCop::Lockfile`
when the constant Bundler is uninitialized.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants