Skip to content

Commit

Permalink
Optimize cache invalidation when running RuboCop as a gem [See ruboco…
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre authored and mergify[bot] committed Sep 3, 2020
1 parent 5b93f78 commit 2b71ae3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
7 changes: 6 additions & 1 deletion 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'
Expand Down Expand Up @@ -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
31 changes: 20 additions & 11 deletions lib/rubocop/result_cache.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b71ae3

Please sign in to comment.