Skip to content

Fix: Exclude not working with absolute path in some cases (#593)#618

Open
dqkqd wants to merge 1 commit intoKampfkarren:mainfrom
dqkqd:issue-593
Open

Fix: Exclude not working with absolute path in some cases (#593)#618
dqkqd wants to merge 1 commit intoKampfkarren:mainfrom
dqkqd:issue-593

Conversation

@dqkqd
Copy link

@dqkqd dqkqd commented Oct 12, 2024

Fix #593

Exclude sometimes doesn't work with absolute path because it tries to match the pattern directly with the path itself.

Since the exclude option is relative, the absolute path to lint should be converted to relative path as well.

exclude = ["**/file.lua"]
exclude = ["src/**"]
exclude = ["external/*", "*.spec.lua"]

For example, if we are in selene-test folder with the following exclude option:

exclude = ["src/**"]

Then, by converting this absolute path /path/to/selene-test/src/file.lua to src/file.lua, exclude can work properly.

The problem is which path we should relate to, initially I thought using config directory (where selene.toml lies) is a good place, but to keep it simple without unnecessary changes, I use the current directory (where selene is invoked) instead.

@dqkqd dqkqd force-pushed the issue-593 branch 5 times, most recently from 4293812 to f606559 Compare October 13, 2024 21:54
Copy link
Collaborator

@chriscerie chriscerie left a comment

Choose a reason for hiding this comment

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

Thanks! Need changelogs for:

  • the fix
  • changing undocumented behavior that exclude paths are now relative to the config file instead of where selene is invoked

We should also properly document that in https://kampfkarren.github.io/selene/usage/configuration.html#excluding-files-from-being-linted

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.

Comment on lines +528 to +529
// There is case where `config = Some("selene.toml")`,
// which returns `""`` when getting parent directory.
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));


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.

Comment on lines +646 to +647
// Directory for matching files pattern.
// If user provides a config file, we use that as our working directory, otherwise fallback to the current 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
// Directory for matching files pattern.
// If user provides a config file, we use that as our working directory, otherwise fallback to the current directory.


// 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?

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.

@chriscerie chriscerie marked this pull request as draft July 13, 2025 03:30
@barrettruth
Copy link

@dqkqd update on this?

@dqkqd
Copy link
Author

dqkqd commented Sep 11, 2025

@barrett-ruth
Oops, I forgot about this, let me see if I can work on this on this weekend.

@dqkqd dqkqd force-pushed the issue-593 branch 3 times, most recently from e7500af to 2dc6b60 Compare September 12, 2025 23:25
@dqkqd
Copy link
Author

dqkqd commented Sep 12, 2025

@chriscerie
Pardon for my rudeness on the first draft, I added a bunch of unnecessary comments and did not present my solution properly.

changing undocumented behavior that exclude paths are now relative to the config file instead of where selene is invoked

I thought that pattern matching on exclude paths should be relative to the config directory because:

  • This allows users to invoke selene from anywhere (e.g. from the sub directory) while keep returning consistent result.
  • It also allows other program (e.g. neovim plugins, vscode) to invoke selene from different folders as well.

However, this might have some edge cases:

  • Users invoking selene directly might be confuse how exclude works. Though this is rare because most of the time people would run selene in the workspace directory.
  • How to handle cases where selene.toml file isn't found in the workspace? Should we fallback to where we invoke selene?

What do you think?
Should I keep implementing the solution using relative path on config directory, or just using the folder where we invoke selene for simplicity.

@chriscerie
Copy link
Collaborator

We should keep existing behavior if changing is not required to fix the issue. Changing it should have its own issue and discussion.

@dqkqd
Copy link
Author

dqkqd commented Sep 13, 2025

Thank @chriscerie

We should keep existing behavior if changing is not required to fix the issue. Changing it should have its own issue and discussion.

Then, I use the current directory (where selene is invoked) to convert the absolute path to relative path.

I updated the PR and the PR description as well. Please have a look.

@dqkqd dqkqd marked this pull request as ready for review September 13, 2025 21:41
@dqkqd dqkqd requested a review from chriscerie September 13, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exclude not working with absolute path in some cases

4 participants