From ee37f5d1fddcce7b9348902acdcadb7864641dd0 Mon Sep 17 00:00:00 2001 From: leaysgur <6259812+leaysgur@users.noreply.github.com> Date: Mon, 20 Oct 2025 01:02:16 +0000 Subject: [PATCH] fix(oxfmt): Handle default cwd correctly (#14704) Fixes #14684. I will try to resolve any relevant issues in a later PR. (See test TODO comments) --- apps/oxfmt/src/format.rs | 12 ++- .../fixtures/exclude_nested/foo/bar/error.js | 2 + .../fixtures/exclude_nested/foo/bar/ok.js | 1 + .../tests/fixtures/exclude_nested/foo/ok.js | 1 + .../oxfmt/tests/fixtures/exclude_nested/ok.js | 1 + apps/oxfmt/tests/mod.rs | 37 +++++++ ...rror.js --check foo !____bar___@oxfmt.snap | 98 +++++++++++++++++++ ...r.js --check .__foo !____bar___@oxfmt.snap | 62 ++++++++++++ 8 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/error.js create mode 100644 apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/ok.js create mode 100644 apps/oxfmt/tests/fixtures/exclude_nested/foo/ok.js create mode 100644 apps/oxfmt/tests/fixtures/exclude_nested/ok.js create mode 100644 apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check !foo__bar__error.js --check !foo__bar --check !foo --check !____error.js --check foo !foo__bar__error.js --check foo !foo__bar --check foo !____bar__error.js --check foo !____bar___@oxfmt.snap create mode 100644 apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check . !foo --check . !____error.js --check .__foo !____bar__error.js --check .__foo !____error.js --check .__foo !____bar___@oxfmt.snap diff --git a/apps/oxfmt/src/format.rs b/apps/oxfmt/src/format.rs index 1ac42a7f9ce6b..79f71cfae771c 100644 --- a/apps/oxfmt/src/format.rs +++ b/apps/oxfmt/src/format.rs @@ -47,6 +47,9 @@ impl FormatRunner { let FormatCommand { paths, output_options, basic_options, misc_options } = self.options; // Find and load config + // NOTE: Currently, we only load single config file. + // - from `--config` if specified + // - else, search nearest for the nearest `.oxfmtrc.json` from cwd upwards let format_options = match load_config(&cwd, basic_options.config.as_ref()) { Ok(options) => options, Err(err) => { @@ -58,13 +61,14 @@ impl FormatRunner { } }; - // Default to current working directory if no paths are provided - let paths = if paths.is_empty() { vec![cwd.clone()] } else { paths }; - - // Instead of `--ignore-pattern=PAT`, we support `!` prefix in paths + // Instead of `oxlint`'s `--ignore-pattern=PAT`, `oxfmt` supports `!` prefix in paths like Prettier let (exclude_patterns, target_paths): (Vec<_>, Vec<_>) = paths.into_iter().partition(|p| p.to_string_lossy().starts_with('!')); + // Default to cwd if no `target_paths` are provided + // NOTE: Do not specify `.` as cwd here, it behaves differently + let target_paths = if target_paths.is_empty() { vec![cwd.clone()] } else { target_paths }; + // Resolve relative paths against the current working directory let target_paths: Vec = target_paths .into_iter() diff --git a/apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/error.js b/apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/error.js new file mode 100644 index 0000000000000..93819f9b927bb --- /dev/null +++ b/apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/error.js @@ -0,0 +1,2 @@ +// This file should be excluded +class { diff --git a/apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/ok.js b/apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/ok.js new file mode 100644 index 0000000000000..54b82a09ad543 --- /dev/null +++ b/apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/ok.js @@ -0,0 +1 @@ +const a = 1; diff --git a/apps/oxfmt/tests/fixtures/exclude_nested/foo/ok.js b/apps/oxfmt/tests/fixtures/exclude_nested/foo/ok.js new file mode 100644 index 0000000000000..54b82a09ad543 --- /dev/null +++ b/apps/oxfmt/tests/fixtures/exclude_nested/foo/ok.js @@ -0,0 +1 @@ +const a = 1; diff --git a/apps/oxfmt/tests/fixtures/exclude_nested/ok.js b/apps/oxfmt/tests/fixtures/exclude_nested/ok.js new file mode 100644 index 0000000000000..54b82a09ad543 --- /dev/null +++ b/apps/oxfmt/tests/fixtures/exclude_nested/ok.js @@ -0,0 +1 @@ +const a = 1; diff --git a/apps/oxfmt/tests/mod.rs b/apps/oxfmt/tests/mod.rs index 611f1195728bf..ac28455b4dc0c 100644 --- a/apps/oxfmt/tests/mod.rs +++ b/apps/oxfmt/tests/mod.rs @@ -84,3 +84,40 @@ fn vcs_dirs_ignored() { .with_cwd(PathBuf::from("tests/fixtures/vcs_dirs")) .test_and_snapshot_multiple(&[&["--check"]]); } + +#[test] +fn exclude_nested_paths() { + // Test that nested path exclusion works correctly + // See: https://github.com/oxc-project/oxc/issues/14684 + // All these cases should not report parse error from `foo/bar/error.js` + Tester::new() + .with_cwd(PathBuf::from("tests/fixtures/exclude_nested")) + .test_and_snapshot_multiple(&[ + &["--check", "!foo/bar/error.js"], + &["--check", "!foo/bar"], + &["--check", "!foo"], + &["--check", "!**/error.js"], + &["--check", "foo", "!foo/bar/error.js"], + &["--check", "foo", "!foo/bar"], + &["--check", "foo", "!**/bar/error.js"], + &["--check", "foo", "!**/bar/*"], + ]); +} +#[test] +fn exclude_nested_paths_with_dot() { + // All these cases should not report parse error from `foo/bar/error.js` + // TODO: Make the commented cases work as well. + Tester::new() + .with_cwd(PathBuf::from("tests/fixtures/exclude_nested")) + .test_and_snapshot_multiple(&[ + // &["--check", ".", "!foo/bar/error.js"], + // &["--check", ".", "!foo/bar"], + &["--check", ".", "!foo"], + &["--check", ".", "!**/error.js"], + &["--check", "./foo", "!**/bar/error.js"], + &["--check", "./foo", "!**/error.js"], + &["--check", "./foo", "!**/bar/*"], + // &["--check", "./foo", "!foo/bar/error.js"], + // &["--check", "./foo", "!foo/bar"], + ]); +} diff --git a/apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check !foo__bar__error.js --check !foo__bar --check !foo --check !____error.js --check foo !foo__bar__error.js --check foo !foo__bar --check foo !____bar__error.js --check foo !____bar___@oxfmt.snap b/apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check !foo__bar__error.js --check !foo__bar --check !foo --check !____error.js --check foo !foo__bar__error.js --check foo !foo__bar --check foo !____bar__error.js --check foo !____bar___@oxfmt.snap new file mode 100644 index 0000000000000..edf4b89d2bbbf --- /dev/null +++ b/apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check !foo__bar__error.js --check !foo__bar --check !foo --check !____error.js --check foo !foo__bar__error.js --check foo !foo__bar --check foo !____bar__error.js --check foo !____bar___@oxfmt.snap @@ -0,0 +1,98 @@ +--- +source: apps/oxfmt/tests/tester.rs +--- +########## +arguments: --check !foo/bar/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 3 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check !foo/bar +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 2 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check !foo +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 1 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check !**/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 3 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check foo !foo/bar/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 2 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check foo !foo/bar +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 1 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check foo !**/bar/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 2 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check foo !**/bar/* +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 1 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- diff --git a/apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check . !foo --check . !____error.js --check .__foo !____bar__error.js --check .__foo !____error.js --check .__foo !____bar___@oxfmt.snap b/apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check . !foo --check . !____error.js --check .__foo !____bar__error.js --check .__foo !____error.js --check .__foo !____bar___@oxfmt.snap new file mode 100644 index 0000000000000..e98f453c76063 --- /dev/null +++ b/apps/oxfmt/tests/snapshots/tests__fixtures__exclude_nested_--check . !foo --check . !____error.js --check .__foo !____bar__error.js --check .__foo !____error.js --check .__foo !____bar___@oxfmt.snap @@ -0,0 +1,62 @@ +--- +source: apps/oxfmt/tests/tester.rs +--- +########## +arguments: --check . !foo +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 1 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check . !**/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 3 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check ./foo !**/bar/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 2 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check ./foo !**/error.js +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 2 files using 1 threads. +---------- +CLI result: FormatSucceeded +---------- + +########## +arguments: --check ./foo !**/bar/* +working directory: tests/fixtures/exclude_nested +---------- +Checking formatting... + +All matched files use the correct format. +Finished in ms on 1 files using 1 threads. +---------- +CLI result: FormatSucceeded +----------