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

Bust erblilnt cache on rubocop config changes via linter checksum #373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/erb_lint/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def clear
FileUtils.rm_r(@cache_dir)
end

def set_runner_checksum(checksum)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def set_runner_checksum(checksum)
def runner_checksum=(checksum)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks for the feedback @etiennebarrie . Will do.

@runner_checksum = checksum
end

private

attr_reader :config, :hits, :new_results
Expand All @@ -76,7 +80,7 @@ def checksum(filename, file_content)
mode = File.stat(filename).mode

digester.update(
"#{mode}#{config.to_hash}#{ERBLint::VERSION}#{file_content}",
"#{mode}#{config.to_hash}#{ERBLint::VERSION}#{@runner_checksum}#{file_content}",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will bust all caches on release since Runner#checksum will return Digest::SHA1.new.hexdigest even if there are no linters with checksum methods. That method can be refactored to return nil in the default / empty case if desired.

)
digester.hexdigest
rescue Errno::ENOENT
Expand Down
3 changes: 3 additions & 0 deletions lib/erb_lint/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def run(args = ARGV)
reporter.preview

runner = ERBLint::Runner.new(file_loader, @config, @options[:disable_inline_configs])

@cache.set_runner_checksum(runner.checksum) if cache?

file_content = nil

lint_files.each do |filename|
Expand Down
6 changes: 6 additions & 0 deletions lib/erb_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def clear_offenses
@offenses = []
end

# Allows linters to specify their own checksums to bust caches, etc.
# for dependencies and things not represented in the erblint config.
def checksum
nil
end

private

def update_offense_status(processed_source)
Expand Down
9 changes: 9 additions & 0 deletions lib/erb_lint/linters/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def autocorrect(processed_source, offense)
end
end

def checksum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this something more intention-revealing. Rails calls something similar cache_key, that might work.

digester = Digest::SHA1.new
gemfile_lock = File.read("Gemfile.lock") if File.exist?("Gemfile.lock")
digester.update(
"#{gemfile_lock}#{@rubocop_config.to_hash}"
)
digester.hexdigest
end

private

def descendant_nodes(processed_source)
Expand Down
9 changes: 9 additions & 0 deletions lib/erb_lint/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def restore_offenses(offenses)
@offenses.concat(offenses)
end

def checksum
digester = Digest::SHA1.new
@linters.each do |linter|
linter_checksum = linter.checksum
digester.update(linter_checksum) if linter_checksum
end
digester.hexdigest
end

private

def enable_inline_configs?
Expand Down
Loading