Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions apps/oxfmt/src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,24 @@ pub fn print_and_flush(writer: &mut dyn Write, message: &str) {
writer.flush().unwrap();
}

/// Normalize a relative path by stripping `./` prefix and joining with `cwd`.
/// This ensures consistent path format and avoids issues with relative paths.
/// Normalize a relative path by:
/// - stripping `./` prefix,
/// - joining with `cwd`,
/// - and canonicalizing to resolve `..` components
///
/// This ensures consistent absolute path format,
/// which is required for gitignore-based pattern matching
/// (e.g., `ignorePatterns` resolution).
pub fn normalize_relative_path(cwd: &Path, path: &Path) -> PathBuf {
if path.is_absolute() {
return path.to_path_buf();
}

if let Ok(stripped) = path.strip_prefix("./") { cwd.join(stripped) } else { cwd.join(path) }
let joined = if let Ok(stripped) = path.strip_prefix("./") {
cwd.join(stripped)
} else {
cwd.join(path)
};

fs::canonicalize(&joined).unwrap_or(joined)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`config_ignore_patterns > explicit --config with parent-relative path resolves ignorePatterns correctly 1`] = `
"--------------------
arguments: --check --config ../.oxfmtrc.json .
working directory: config_ignore_patterns/fixtures/nested_cwd/sub
exit code: 1
--- STDOUT ---------
Checking formatting...

src/test.js (<variable>ms)

Format issues found in above 1 files. Run without \`--check\` to fix.
Finished in <variable>ms on 1 files using 1 threads.
--- STDERR ---------

--------------------"
`;

exports[`config_ignore_patterns > nested cwd - ignorePatterns resolved relative to .oxfmtrc.json location 1`] = `
"--------------------
arguments: --check .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@
const snapshot = await runAndSnapshot(nestedCwd, [["--check", "."]]);
expect(snapshot).toMatchSnapshot();
});

// Same structure as above, but explicitly specifying `--config ../.oxfmtrc.json`
// instead of relying on automatic upward config search.
//
// When `--config` contains `..`, `normalize_relative_path` joins
// cwd + `../.oxfmtrc.json` without resolving `..`, leaving the path as
// `.../sub/../.oxfmtrc.json`. This causes `GitignoreBuilder`'s root
// to be `.../sub/..` which doesn't match file paths via `strip_prefix`,
// making `ignorePatterns` ineffective.
it("explicit --config with parent-relative path resolves ignorePatterns correctly", async () => {
const nestedCwd = join(import.meta.dirname, "fixtures", "nested_cwd", "sub");
const snapshot = await runAndSnapshot(nestedCwd, [
["--check", "--config", "../.oxfmtrc.json", "."],
]);
expect(snapshot).toMatchSnapshot();

Check failure on line 51 in apps/oxfmt/test/cli/config_ignore_patterns/config_ignore_patterns.test.ts

View workflow job for this annotation

GitHub Actions / Test NAPI (windows)

test/cli/config_ignore_patterns/config_ignore_patterns.test.ts > config_ignore_patterns > explicit --config with parent-relative path resolves ignorePatterns correctly

Error: Snapshot `config_ignore_patterns > explicit --config with parent-relative path resolves ignorePatterns correctly 1` mismatched - Expected + Received "-------------------- arguments: --check --config ../.oxfmtrc.json . working directory: config_ignore_patterns/fixtures/nested_cwd/sub - exit code: 1 + exit code: 2 --- STDOUT --------- Checking formatting... src/test.js (<variable>ms) - - Format issues found in above 1 files. Run without `--check` to fix. - Finished in <variable>ms on 1 files using 1 threads. - --- STDERR --------- - + --- STDERR --------- + + × Failed to format file with external formatter: <cwd>/generated/error.html + │ JS formatFile promise rejected for file: 'error.html', parser: 'html': GenericFailure, SyntaxError: Opening tag "link" not terminated. (1:1) + │ > 1 | <link rel=variable>mstylesheet href=/variable>mstyles.css /> + │ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + │ 2 | + Error occurred when checking code style in the above files. --------------------" ❯ test/cli/config_ignore_patterns/config_ignore_patterns.test.ts:51:22
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel=stylesheet href=/styles.css />

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<link rel=stylesheet href=/styles.css />

This file was deleted.

Loading