From 4ed9d765b88bc4078c056dea681cb1436c88d48a Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Sat, 22 Feb 2025 23:15:29 +0000 Subject: [PATCH] fix(linter): do not use nested configs with `--config` option (#9155) - part of https://github.com/oxc-project/oxc/issues/7408 Ensures that when a config file is explicitly passed via `-c` or `--config`, that file will take precedence over all nested config files, and we will not search for config files within any directories. --- .../nested_config/oxlint-no-console.json | 6 +++ apps/oxlint/src/lint.rs | 15 ++++++- ...-config oxlint-no-console.json@oxlint.snap | 43 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 apps/oxlint/fixtures/nested_config/oxlint-no-console.json create mode 100644 apps/oxlint/src/snapshots/fixtures__nested_config_--experimental-nested-config --config oxlint-no-console.json@oxlint.snap diff --git a/apps/oxlint/fixtures/nested_config/oxlint-no-console.json b/apps/oxlint/fixtures/nested_config/oxlint-no-console.json new file mode 100644 index 0000000000000..f35466e3f8004 --- /dev/null +++ b/apps/oxlint/fixtures/nested_config/oxlint-no-console.json @@ -0,0 +1,6 @@ +{ + "rules": { + "no-console": "error", + "no-debugger": "off" + } +} diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 52f5e09398a2e..1fbb37d2ef2b2 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -175,7 +175,12 @@ impl Runner for LintRunner { let mut nested_oxlintrc = FxHashMap::<&Path, Oxlintrc>::default(); let mut nested_configs = FxHashMap::::default(); - if experimental_nested_config { + let use_nested_config = + // If the `--config` option is explicitly passed, we should not search for nested config files + // as the passed config file takes absolute precedence. + basic_options.config.is_none(); + + if experimental_nested_config && 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` @@ -947,4 +952,12 @@ mod test { let args = &["--experimental-nested-config"]; Tester::new().with_cwd("fixtures/nested_config".into()).test_and_snapshot(args); } + + #[test] + fn test_nested_config_precedence() { + // `--config` takes absolute precedence over nested configs, and will be used for + // linting all files rather than the nested configuration files. + let args = &["--experimental-nested-config", "--config", "oxlint-no-console.json"]; + Tester::new().with_cwd("fixtures/nested_config".into()).test_and_snapshot(args); + } } diff --git a/apps/oxlint/src/snapshots/fixtures__nested_config_--experimental-nested-config --config oxlint-no-console.json@oxlint.snap b/apps/oxlint/src/snapshots/fixtures__nested_config_--experimental-nested-config --config oxlint-no-console.json@oxlint.snap new file mode 100644 index 0000000000000..8af77bcd375ec --- /dev/null +++ b/apps/oxlint/src/snapshots/fixtures__nested_config_--experimental-nested-config --config oxlint-no-console.json@oxlint.snap @@ -0,0 +1,43 @@ +--- +source: apps/oxlint/src/tester.rs +--- +########## +arguments: --experimental-nested-config --config oxlint-no-console.json +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. + ,-[console.ts:1:1] + 1 | console.log("test"); + : ^^^^^^^^^^^ + `---- + help: Delete this console statement. + + x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-console.html\eslint(no-console)]8;;\: eslint(no-console): Unexpected console statement. + ,-[package1-empty-config/console.ts:1:1] + 1 | console.log("test"); + : ^^^^^^^^^^^ + `---- + help: Delete this console statement. + + x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-console.html\eslint(no-console)]8;;\: eslint(no-console): Unexpected console statement. + ,-[package2-no-config/console.ts:1:1] + 1 | console.log("test"); + : ^^^^^^^^^^^ + `---- + help: Delete this console statement. + + 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 4 errors. +Finished in ms on 7 files with 99 rules using 1 threads. +---------- +CLI result: LintFoundErrors +----------