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

[Fix #12865] require 'bundler' if possible and rescue Bundler::GemfileNotFound #12866

Merged
merged 1 commit into from Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions 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][])
14 changes: 13 additions & 1 deletion lib/rubocop/lockfile.rb
@@ -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