Skip to content

Commit ee37f5d

Browse files
committed
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)
1 parent d39bed0 commit ee37f5d

8 files changed

+210
-4
lines changed

apps/oxfmt/src/format.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ impl FormatRunner {
4747
let FormatCommand { paths, output_options, basic_options, misc_options } = self.options;
4848

4949
// Find and load config
50+
// NOTE: Currently, we only load single config file.
51+
// - from `--config` if specified
52+
// - else, search nearest for the nearest `.oxfmtrc.json` from cwd upwards
5053
let format_options = match load_config(&cwd, basic_options.config.as_ref()) {
5154
Ok(options) => options,
5255
Err(err) => {
@@ -58,13 +61,14 @@ impl FormatRunner {
5861
}
5962
};
6063

61-
// Default to current working directory if no paths are provided
62-
let paths = if paths.is_empty() { vec![cwd.clone()] } else { paths };
63-
64-
// Instead of `--ignore-pattern=PAT`, we support `!` prefix in paths
64+
// Instead of `oxlint`'s `--ignore-pattern=PAT`, `oxfmt` supports `!` prefix in paths like Prettier
6565
let (exclude_patterns, target_paths): (Vec<_>, Vec<_>) =
6666
paths.into_iter().partition(|p| p.to_string_lossy().starts_with('!'));
6767

68+
// Default to cwd if no `target_paths` are provided
69+
// NOTE: Do not specify `.` as cwd here, it behaves differently
70+
let target_paths = if target_paths.is_empty() { vec![cwd.clone()] } else { target_paths };
71+
6872
// Resolve relative paths against the current working directory
6973
let target_paths: Vec<PathBuf> = target_paths
7074
.into_iter()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file should be excluded
2+
class {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a = 1;

apps/oxfmt/tests/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,40 @@ fn vcs_dirs_ignored() {
8484
.with_cwd(PathBuf::from("tests/fixtures/vcs_dirs"))
8585
.test_and_snapshot_multiple(&[&["--check"]]);
8686
}
87+
88+
#[test]
89+
fn exclude_nested_paths() {
90+
// Test that nested path exclusion works correctly
91+
// See: https://github.com/oxc-project/oxc/issues/14684
92+
// All these cases should not report parse error from `foo/bar/error.js`
93+
Tester::new()
94+
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
95+
.test_and_snapshot_multiple(&[
96+
&["--check", "!foo/bar/error.js"],
97+
&["--check", "!foo/bar"],
98+
&["--check", "!foo"],
99+
&["--check", "!**/error.js"],
100+
&["--check", "foo", "!foo/bar/error.js"],
101+
&["--check", "foo", "!foo/bar"],
102+
&["--check", "foo", "!**/bar/error.js"],
103+
&["--check", "foo", "!**/bar/*"],
104+
]);
105+
}
106+
#[test]
107+
fn exclude_nested_paths_with_dot() {
108+
// All these cases should not report parse error from `foo/bar/error.js`
109+
// TODO: Make the commented cases work as well.
110+
Tester::new()
111+
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
112+
.test_and_snapshot_multiple(&[
113+
// &["--check", ".", "!foo/bar/error.js"],
114+
// &["--check", ".", "!foo/bar"],
115+
&["--check", ".", "!foo"],
116+
&["--check", ".", "!**/error.js"],
117+
&["--check", "./foo", "!**/bar/error.js"],
118+
&["--check", "./foo", "!**/error.js"],
119+
&["--check", "./foo", "!**/bar/*"],
120+
// &["--check", "./foo", "!foo/bar/error.js"],
121+
// &["--check", "./foo", "!foo/bar"],
122+
]);
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
source: apps/oxfmt/tests/tester.rs
3+
---
4+
##########
5+
arguments: --check !foo/bar/error.js
6+
working directory: tests/fixtures/exclude_nested
7+
----------
8+
Checking formatting...
9+
10+
All matched files use the correct format.
11+
Finished in <variable>ms on 3 files using 1 threads.
12+
----------
13+
CLI result: FormatSucceeded
14+
----------
15+
16+
##########
17+
arguments: --check !foo/bar
18+
working directory: tests/fixtures/exclude_nested
19+
----------
20+
Checking formatting...
21+
22+
All matched files use the correct format.
23+
Finished in <variable>ms on 2 files using 1 threads.
24+
----------
25+
CLI result: FormatSucceeded
26+
----------
27+
28+
##########
29+
arguments: --check !foo
30+
working directory: tests/fixtures/exclude_nested
31+
----------
32+
Checking formatting...
33+
34+
All matched files use the correct format.
35+
Finished in <variable>ms on 1 files using 1 threads.
36+
----------
37+
CLI result: FormatSucceeded
38+
----------
39+
40+
##########
41+
arguments: --check !**/error.js
42+
working directory: tests/fixtures/exclude_nested
43+
----------
44+
Checking formatting...
45+
46+
All matched files use the correct format.
47+
Finished in <variable>ms on 3 files using 1 threads.
48+
----------
49+
CLI result: FormatSucceeded
50+
----------
51+
52+
##########
53+
arguments: --check foo !foo/bar/error.js
54+
working directory: tests/fixtures/exclude_nested
55+
----------
56+
Checking formatting...
57+
58+
All matched files use the correct format.
59+
Finished in <variable>ms on 2 files using 1 threads.
60+
----------
61+
CLI result: FormatSucceeded
62+
----------
63+
64+
##########
65+
arguments: --check foo !foo/bar
66+
working directory: tests/fixtures/exclude_nested
67+
----------
68+
Checking formatting...
69+
70+
All matched files use the correct format.
71+
Finished in <variable>ms on 1 files using 1 threads.
72+
----------
73+
CLI result: FormatSucceeded
74+
----------
75+
76+
##########
77+
arguments: --check foo !**/bar/error.js
78+
working directory: tests/fixtures/exclude_nested
79+
----------
80+
Checking formatting...
81+
82+
All matched files use the correct format.
83+
Finished in <variable>ms on 2 files using 1 threads.
84+
----------
85+
CLI result: FormatSucceeded
86+
----------
87+
88+
##########
89+
arguments: --check foo !**/bar/*
90+
working directory: tests/fixtures/exclude_nested
91+
----------
92+
Checking formatting...
93+
94+
All matched files use the correct format.
95+
Finished in <variable>ms on 1 files using 1 threads.
96+
----------
97+
CLI result: FormatSucceeded
98+
----------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
source: apps/oxfmt/tests/tester.rs
3+
---
4+
##########
5+
arguments: --check . !foo
6+
working directory: tests/fixtures/exclude_nested
7+
----------
8+
Checking formatting...
9+
10+
All matched files use the correct format.
11+
Finished in <variable>ms on 1 files using 1 threads.
12+
----------
13+
CLI result: FormatSucceeded
14+
----------
15+
16+
##########
17+
arguments: --check . !**/error.js
18+
working directory: tests/fixtures/exclude_nested
19+
----------
20+
Checking formatting...
21+
22+
All matched files use the correct format.
23+
Finished in <variable>ms on 3 files using 1 threads.
24+
----------
25+
CLI result: FormatSucceeded
26+
----------
27+
28+
##########
29+
arguments: --check ./foo !**/bar/error.js
30+
working directory: tests/fixtures/exclude_nested
31+
----------
32+
Checking formatting...
33+
34+
All matched files use the correct format.
35+
Finished in <variable>ms on 2 files using 1 threads.
36+
----------
37+
CLI result: FormatSucceeded
38+
----------
39+
40+
##########
41+
arguments: --check ./foo !**/error.js
42+
working directory: tests/fixtures/exclude_nested
43+
----------
44+
Checking formatting...
45+
46+
All matched files use the correct format.
47+
Finished in <variable>ms on 2 files using 1 threads.
48+
----------
49+
CLI result: FormatSucceeded
50+
----------
51+
52+
##########
53+
arguments: --check ./foo !**/bar/*
54+
working directory: tests/fixtures/exclude_nested
55+
----------
56+
Checking formatting...
57+
58+
All matched files use the correct format.
59+
Finished in <variable>ms on 1 files using 1 threads.
60+
----------
61+
CLI result: FormatSucceeded
62+
----------

0 commit comments

Comments
 (0)