diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index b5e438eaf028e..5bba7feb52696 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -176,15 +176,19 @@ impl Runner for LintRunner { if use_nested_config { // get all of the unique directories among the paths to use for search for - // oxlint config files in those directories - // e.g. `/some/file.js` and `/some/other/file.js` would both result in `/some` + // oxlint config files in those directories and their ancestors + // e.g. `/some/file.js` will check `/some` and `/` + // `/some/other/file.js` will check `/some/other`, `/some`, and `/` let mut directories = FxHashSet::default(); for path in &paths { let path = Path::new(path); - if let Some(directory) = path.parent() { + // Start from the file's parent directory and walk up the tree + let mut current = path.parent(); + while let Some(dir) = current { // NOTE: Initial benchmarking showed that it was faster to iterate over the directories twice // rather than constructing the configs in one iteration. It's worth re-benchmarking that though. - directories.insert(directory); + directories.insert(dir); + current = dir.parent(); } } for directory in directories { @@ -1009,6 +1013,14 @@ mod test { Tester::new().with_cwd("fixtures/extends_config".into()).test_and_snapshot(args); } + #[test] + fn test_nested_config_subdirectory() { + // This tests the specific scenario from issue #10156 + // where a file is located in a subdirectory of a directory with a config file + let args = &["package3-deep-config"]; + Tester::new().with_cwd("fixtures/nested_config".into()).test_and_snapshot(args); + } + #[test] fn test_nested_config_explicit_config_precedence() { // `--config` takes absolute precedence over nested configs, and will be used for diff --git a/apps/oxlint/src/snapshots/fixtures__nested_config_package3-deep-config@oxlint.snap b/apps/oxlint/src/snapshots/fixtures__nested_config_package3-deep-config@oxlint.snap new file mode 100644 index 0000000000000..209908d048c3c --- /dev/null +++ b/apps/oxlint/src/snapshots/fixtures__nested_config_package3-deep-config@oxlint.snap @@ -0,0 +1,22 @@ +--- +source: apps/oxlint/src/tester.rs +--- +########## +arguments: package3-deep-config +working directory: fixtures/nested_config +---------- + + x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-console.html\eslint(no-console)]8;;\: eslint(no-console): Unexpected console statement. + ,-[package3-deep-config/src/components/component.js:2:3] + 1 | export function Component() { + 2 | console.log("hello"); + : ^^^^^^^^^^^ + 3 | } + `---- + help: Delete this console statement. + +Found 0 warnings and 1 error. +Finished in ms on 1 file using 1 threads. +---------- +CLI result: LintFoundErrors +----------