Skip to content

Commit

Permalink
Merge pull request #12866 from amomchilov/rescue-GemfileNotFound
Browse files Browse the repository at this point in the history
[Fix #12865] `require 'bundler'` if possible and rescue `Bundler::GemfileNotFound`
  • Loading branch information
koic committed Apr 27, 2024
2 parents a8ef19c + 45bbf67 commit 02aebff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_rails_cops_outside_bundler_exec.md
Original file line number Diff line number Diff line change
@@ -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][])
14 changes: 13 additions & 1 deletion lib/rubocop/lockfile.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
# 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.
# @api private
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
Expand Down

0 comments on commit 02aebff

Please sign in to comment.