fix(apps/oxlint): incorrect matching in .oxlintignore (#7566)

The issue was discovered while updating test cases in `rolldown`.

In the `.oxlintignore` file, we have:
```ignore
tests/**
```

When running the command by `lint-staged`:

```bash
oxlint -c .oxlintrc.json --ignore-path=.oxlintignore --deny-warnings "tests/function/main.js"
```

The file `main.js` gets linted despite being ignored. This happens
because, before using `Gitignore::new`, we converted paths to absolute
paths, causing the pattern `tests/**` to not match
`D:/rolldown/tests/function/main.js` correctly.


c61a383e8c/apps/oxlint/src/lint.rs (L60-L78)
This commit is contained in:
dalaoshu 2024-12-02 11:06:20 +08:00 committed by GitHub
parent e62153bdd7
commit 9761e94176
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 28 additions and 10 deletions

View file

@ -0,0 +1 @@
tests/**

View file

@ -0,0 +1 @@
123 == NaN;

View file

@ -0,0 +1 @@
123 == NaN;

View file

@ -57,16 +57,6 @@ impl Runner for LintRunner {
let provided_path_count = paths.len(); let provided_path_count = paths.len();
let now = Instant::now(); let now = Instant::now();
// append cwd to all paths
paths = paths
.into_iter()
.map(|x| {
let mut path_with_cwd = self.cwd.clone();
path_with_cwd.push(x);
path_with_cwd
})
.collect();
// The ignore crate whitelists explicit paths, but priority // The ignore crate whitelists explicit paths, but priority
// should be given to the ignore file. Many users lint // should be given to the ignore file. Many users lint
// automatically and pass a list of changed files explicitly. // automatically and pass a list of changed files explicitly.
@ -77,6 +67,16 @@ impl Runner for LintRunner {
paths.retain(|p| if p.is_dir() { true } else { !ignore.matched(p, false).is_ignore() }); paths.retain(|p| if p.is_dir() { true } else { !ignore.matched(p, false).is_ignore() });
} }
// Append cwd to all paths
paths = paths
.into_iter()
.map(|x| {
let mut path_with_cwd = self.cwd.clone();
path_with_cwd.push(x);
path_with_cwd
})
.collect();
if paths.is_empty() { if paths.is_empty() {
// If explicit paths were provided, but all have been // If explicit paths were provided, but all have been
// filtered, return early. // filtered, return early.
@ -777,4 +777,19 @@ mod test {
]); ]);
assert_eq!(result.number_of_files, 1); assert_eq!(result.number_of_files, 1);
} }
// Issue: <https://github.com/oxc-project/oxc/pull/7566>
#[test]
fn ignore_path_with_relative_files() {
let args = &[
"--ignore-path",
"fixtures/issue_7566/.oxlintignore",
"fixtures/issue_7566/tests/main.js",
"fixtures/issue_7566/tests/function/main.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 0);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}
} }