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: Exclude not working with absolute path in some cases (#593) #618

Open
wants to merge 3 commits 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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions selene/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tracy-client = { version = "0.14.1", optional = true }
threadpool = "1.8"
toml.workspace = true
ureq = { version = "2.6.2", features = ["json"], optional = true }
pathdiff = "0.2.2"

[dev-dependencies]
pretty_assertions = "1.3"
Expand Down
39 changes: 33 additions & 6 deletions selene/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ fn start(mut options: opts::Options) {

let (config, config_directory): (CheckerConfig<toml::value::Value>, Option<PathBuf>) =
match options.config {
// User provides a config file. We must read it and report if it is misconfigured.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// User provides a config file. We must read it and report if it is misconfigured.

Some(config_file) => {
let config_contents = match fs::read_to_string(&config_file) {
Ok(contents) => contents,
Expand All @@ -522,10 +523,16 @@ fn start(mut options: opts::Options) {
};

match toml::from_str(&config_contents) {
Ok(config) => (
config,
Path::new(&config_file).parent().map(Path::to_path_buf),
),
Ok(config) => {
// Get config directory to using absolute path.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Get config directory to using absolute path.

// There is case where `config = Some("selene.toml")`,
// which returns `""`` when getting parent directory.
Comment on lines +528 to +529
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does this mean?

let config_directory = Path::new(&config_file)
.canonicalize()
.ok()
.and_then(|path| path.parent().map(|path| path.to_path_buf()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.and_then(|path| path.parent().map(|path| path.to_path_buf()));
.and_then(|path| path.parent().map(Path::to_path_buf));

(config, config_directory)
}
Err(error) => {
error!("Config file not in correct format: {}", error);
std::process::exit(1);
Expand Down Expand Up @@ -636,6 +643,26 @@ fn start(mut options: opts::Options) {

let pool = ThreadPool::new(options.num_threads);

// Directory for matching files pattern.
// If user provides a config file, we use that as our working directory, otherwise fallback to the current directory.
Comment on lines +646 to +647
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Directory for matching files pattern.
// If user provides a config file, we use that as our working directory, otherwise fallback to the current directory.

// The working directory needs to be an absolute path to match with paths in different directories.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't explain why relative path won't work. Can we either clarify that?

let working_directory = config_directory.or_else(|| current_dir.canonicalize().ok());

let is_excluded = |path: &Path| -> bool {
if options.no_exclude {
return false;
}

// File pattern between the path we are checking and current working directory.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// File pattern between the path we are checking and current working directory.

let file_pattern = working_directory.as_ref().and_then(|dir| {
path.canonicalize()
.ok()
.and_then(|path| pathdiff::diff_paths(path, dir))
});

file_pattern.is_some_and(|pattern| exclude_set.is_match(&pattern))
};

for filename in &options.files {
if filename == "-" {
let checker = Arc::clone(&checker);
Expand All @@ -649,7 +676,7 @@ fn start(mut options: opts::Options) {
let checker = Arc::clone(&checker);
let filename = filename.to_owned();

if !options.no_exclude && exclude_set.is_match(&filename) {
if is_excluded(Path::new(&filename)) {
continue;
}

Expand All @@ -671,7 +698,7 @@ fn start(mut options: opts::Options) {
for entry in glob {
match entry {
Ok(path) => {
if !options.no_exclude && exclude_set.is_match(&path) {
if is_excluded(&path) {
continue;
}

Expand Down