diff --git a/changelog/fix_rails_cops_outside_bundler_exec.md b/changelog/fix_rails_cops_outside_bundler_exec.md new file mode 100644 index 00000000000..0f6f99b6fd2 --- /dev/null +++ b/changelog/fix_rails_cops_outside_bundler_exec.md @@ -0,0 +1 @@ +* [#12865](https://github.com/rubocop/rubocop/issues/12865): Fix Rails Cops, which weren't reporting any violations unless running with `bundle exec`. ([@amomchilov][]) diff --git a/lib/rubocop/lockfile.rb b/lib/rubocop/lockfile.rb index 5d53465bf62..0f0ab7bb4eb 100755 --- a/lib/rubocop/lockfile.rb +++ b/lib/rubocop/lockfile.rb @@ -1,5 +1,13 @@ # frozen_string_literal: true +begin + # We might not be running with `bundle exec`, so we need to pull in Bundler ourselves, + # in order to use `Bundler::LockfileParser`. + require 'bundler' +rescue LoadError + nil +end + module RuboCop # Encapsulation of a lockfile for use when checking for gems. # Does not actually resolve gems, just parses the lockfile. @@ -7,7 +15,11 @@ module RuboCop class Lockfile # @param [String, Pathname, nil] lockfile_path def initialize(lockfile_path = nil) - lockfile_path ||= Bundler.default_lockfile if bundler_lock_parser_defined? + lockfile_path ||= begin + ::Bundler.default_lockfile if bundler_lock_parser_defined? + rescue ::Bundler::GemfileNotFound + nil # We might not be a folder with a Gemfile, but that's okay. + end @lockfile_path = lockfile_path end