forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…d two env vars Fixes rubocop#10842 and follow up rubocop#10849. This PR makes server mode aware of `CacheRootDirectory` config option value, `RUBOCOP_CACHE_ROOT`, and `XDG_CACHE_HOME` environment variables. `RuboCop::ConfigLoader` and `RuboCop::ResultCache` classes to get these values has many dependencies. So, it is not suitable for use in server mode where speed is required. This solution is to extract `ConfigPathFinder` class from `ConfigLoader` class and `CacheRoot` class from `ResultCache` class. This allows server mode to depend on lightweight `ConfigPathFinder` and `CacheRoot` classes.
- Loading branch information
Showing
11 changed files
with
318 additions
and
71 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/fix_make_server_mode_aware_of_cache_root_directory.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10842](https://github.com/rubocop/rubocop/issues/10842): Make server mode aware of `CacheRootDirectory` config option value, `RUBOCOP_CACHE_ROOT`, and `XDG_CACHE_HOME` environment variables. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
# This class represents the cache config of the caching RuboCop runs. | ||
# @api private | ||
class CacheConfig | ||
def self.root_dir | ||
root = ENV.fetch('RUBOCOP_CACHE_ROOT', nil) | ||
root ||= yield | ||
root ||= if ENV.key?('XDG_CACHE_HOME') | ||
# Include user ID in the path to make sure the user has write | ||
# access. | ||
File.join(ENV.fetch('XDG_CACHE_HOME'), Process.uid.to_s) | ||
else | ||
# On FreeBSD, the /home path is a symbolic link to /usr/home | ||
# and the $HOME environment variable returns the /home path. | ||
# | ||
# As $HOME is a built-in environment variable, FreeBSD users | ||
# always get a warning message. | ||
# | ||
# To avoid raising warn log messages on FreeBSD, we retrieve | ||
# the real path of the home folder. | ||
File.join(File.realpath(Dir.home), '.cache') | ||
end | ||
|
||
File.join(root, 'rubocop_cache') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'file_finder' | ||
|
||
module RuboCop | ||
# This class has methods related to finding configuration path. | ||
# @api private | ||
class ConfigFinder | ||
DOTFILE = '.rubocop.yml' | ||
XDG_CONFIG = 'config.yml' | ||
RUBOCOP_HOME = File.realpath(File.join(File.dirname(__FILE__), '..', '..')) | ||
DEFAULT_FILE = File.join(RUBOCOP_HOME, 'config', 'default.yml') | ||
|
||
class << self | ||
include FileFinder | ||
|
||
attr_writer :project_root | ||
|
||
def find_config_path(target_dir) | ||
find_project_dotfile(target_dir) || find_user_dotfile || find_user_xdg_config || | ||
DEFAULT_FILE | ||
end | ||
|
||
# Returns the path RuboCop inferred as the root of the project. No file | ||
# searches will go past this directory. | ||
def project_root | ||
@project_root ||= find_project_root | ||
end | ||
|
||
private | ||
|
||
def find_project_root | ||
pwd = Dir.pwd | ||
gems_file = find_last_file_upwards('Gemfile', pwd) || find_last_file_upwards('gems.rb', pwd) | ||
return unless gems_file | ||
|
||
File.dirname(gems_file) | ||
end | ||
|
||
def find_project_dotfile(target_dir) | ||
find_file_upwards(DOTFILE, target_dir, project_root) | ||
end | ||
|
||
def find_user_dotfile | ||
return unless ENV.key?('HOME') | ||
|
||
file = File.join(Dir.home, DOTFILE) | ||
|
||
return file if File.exist?(file) | ||
end | ||
|
||
def find_user_xdg_config | ||
xdg_config_home = expand_path(ENV.fetch('XDG_CONFIG_HOME', '~/.config')) | ||
xdg_config = File.join(xdg_config_home, 'rubocop', XDG_CONFIG) | ||
|
||
return xdg_config if File.exist?(xdg_config) | ||
end | ||
|
||
def expand_path(path) | ||
File.expand_path(path) | ||
rescue ArgumentError | ||
# Could happen because HOME or ID could not be determined. Fall back on | ||
# using the path literally in that case. | ||
path | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.