diff --git a/lib/rubocop.rb b/lib/rubocop.rb index c3f0c03fb61..e7cf7c78588 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true +require 'English' +before_us = $LOADED_FEATURES.dup require 'rainbow' -require 'English' require 'set' require 'forwardable' require 'regexp_parser' @@ -627,3 +628,7 @@ require_relative 'rubocop/remote_config' require_relative 'rubocop/target_ruby' require_relative 'rubocop/yaml_duplication_checker' + +unless File.exist?("#{__dir__}/../Gemfile") # Check if we are a gem + RuboCop::ResultCache.rubocop_required_features = $LOADED_FEATURES - before_us +end diff --git a/lib/rubocop/result_cache.rb b/lib/rubocop/result_cache.rb index 6ff42276351..2dda0c8a6fc 100644 --- a/lib/rubocop/result_cache.rb +++ b/lib/rubocop/result_cache.rb @@ -30,6 +30,11 @@ def self.cleanup(config_store, verbose, cache_root = nil) end class << self + # @api private + attr_accessor :rubocop_required_features + + ResultCache.rubocop_required_features = [] + private def requires_file_removal?(file_count, config_store) @@ -159,27 +164,31 @@ class << self end # The checksum of the rubocop program running the inspection. - # rubocop:disable Metrics/AbcSize def rubocop_checksum ResultCache.source_checksum ||= begin - lib_root = File.join(File.dirname(__FILE__), '..') - exe_root = File.join(lib_root, '..', 'exe') - - # These are all the files we have `require`d plus everything in the - # exe directory. A change to any of them could affect the cop output - # so we include them in the cache hash. - source_files = $LOADED_FEATURES + Find.find(exe_root).to_a - digest = Digest::SHA1.new - source_files + rubocop_extra_features .select { |path| File.file?(path) } .sort! .each { |path| digest << File.mtime(path).to_s } + digest << RuboCop::Version::STRING << RuboCop::AST::Version::STRING digest.hexdigest end end - # rubocop:enable Metrics/AbcSize + + def rubocop_extra_features + lib_root = File.join(File.dirname(__FILE__), '..') + exe_root = File.join(lib_root, '..', 'exe') + + # These are all the files we have `require`d plus everything in the + # exe directory. A change to any of them could affect the cop output + # so we include them in the cache hash. + source_files = $LOADED_FEATURES + Find.find(exe_root).to_a + source_files -= ResultCache.rubocop_required_features # Rely on gem versions + + source_files + end # Return a hash of the options given at invocation, minus the ones that have # no effect on which offenses and disabled line ranges are found, and thus