From 9761e9417638849a8de0f70f095d11876984e49e Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Mon, 2 Dec 2024 11:06:20 +0800 Subject: [PATCH] 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. https://github.com/oxc-project/oxc/blob/c61a383e8cc453fb92f85e6047426a86e50daba7/apps/oxlint/src/lint.rs#L60-L78 --- apps/oxlint/fixtures/issue_7566/.oxlintignore | 1 + .../issue_7566/tests/function/main.js | 1 + apps/oxlint/fixtures/issue_7566/tests/main.js | 1 + apps/oxlint/src/lint.rs | 35 +++++++++++++------ 4 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 apps/oxlint/fixtures/issue_7566/.oxlintignore create mode 100644 apps/oxlint/fixtures/issue_7566/tests/function/main.js create mode 100644 apps/oxlint/fixtures/issue_7566/tests/main.js diff --git a/apps/oxlint/fixtures/issue_7566/.oxlintignore b/apps/oxlint/fixtures/issue_7566/.oxlintignore new file mode 100644 index 000000000..d3745c177 --- /dev/null +++ b/apps/oxlint/fixtures/issue_7566/.oxlintignore @@ -0,0 +1 @@ +tests/** diff --git a/apps/oxlint/fixtures/issue_7566/tests/function/main.js b/apps/oxlint/fixtures/issue_7566/tests/function/main.js new file mode 100644 index 000000000..755b6d44f --- /dev/null +++ b/apps/oxlint/fixtures/issue_7566/tests/function/main.js @@ -0,0 +1 @@ +123 == NaN; diff --git a/apps/oxlint/fixtures/issue_7566/tests/main.js b/apps/oxlint/fixtures/issue_7566/tests/main.js new file mode 100644 index 000000000..755b6d44f --- /dev/null +++ b/apps/oxlint/fixtures/issue_7566/tests/main.js @@ -0,0 +1 @@ +123 == NaN; diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 6ca57564f..f5649f919 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -57,16 +57,6 @@ impl Runner for LintRunner { let provided_path_count = paths.len(); 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 // should be given to the ignore file. Many users lint // 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() }); } + // 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 explicit paths were provided, but all have been // filtered, return early. @@ -777,4 +777,19 @@ mod test { ]); assert_eq!(result.number_of_files, 1); } + + // Issue: + #[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); + } }