Skip to content

Commit 4e1f536

Browse files
authored
fix(linter): config path resolution when path contains '..' syntax (#10367)
Improved the `find_oxlint_config` function to handle paths containing parent references ( ../.. syntax ) Added unit tests to verify behavior for valid and invalid config paths, including cases with parent directory references.
1 parent e4c80b4 commit 4e1f536

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

apps/oxlint/src/lint.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
env, fs,
33
io::{ErrorKind, Write},
4-
path::{Path, PathBuf},
4+
path::{Path, PathBuf, absolute},
55
time::Instant,
66
};
77

@@ -475,7 +475,23 @@ impl LintRunner {
475475
// when no file is found, the default configuration is returned
476476
fn find_oxlint_config(cwd: &Path, config: Option<&PathBuf>) -> Result<Oxlintrc, String> {
477477
if let Some(config_path) = config {
478-
let full_path = cwd.join(config_path);
478+
let full_path = match absolute(cwd.join(config_path)) {
479+
Ok(path) => path,
480+
Err(e) => {
481+
let handler = GraphicalReportHandler::new();
482+
let mut err = String::new();
483+
handler
484+
.render_report(
485+
&mut err,
486+
&OxcDiagnostic::error(format!(
487+
"Failed to resolve config path {}: {e}",
488+
config_path.display()
489+
)),
490+
)
491+
.unwrap();
492+
return Err(err);
493+
}
494+
};
479495
return match Oxlintrc::from_file(&full_path) {
480496
Ok(config) => Ok(config),
481497
Err(diagnostic) => {
@@ -1076,6 +1092,35 @@ mod test {
10761092
Tester::new().with_cwd("fixtures".into()).test_and_snapshot(args);
10771093
}
10781094

1095+
#[test]
1096+
fn test_config_path_with_parent_references() {
1097+
let cwd = std::env::current_dir().unwrap();
1098+
1099+
// Test case 1: Invalid path that should fail
1100+
let invalid_config = PathBuf::from("child/../../fixtures/linter/eslintrc.json");
1101+
let result = LintRunner::find_oxlint_config(&cwd, Some(&invalid_config));
1102+
assert!(result.is_err(), "Expected config lookup to fail with invalid path");
1103+
1104+
// Test case 2: Valid path that should pass
1105+
let valid_config = PathBuf::from("fixtures/linter/eslintrc.json");
1106+
let result = LintRunner::find_oxlint_config(&cwd, Some(&valid_config));
1107+
assert!(result.is_ok(), "Expected config lookup to succeed with valid path");
1108+
1109+
// Test case 3: Valid path using parent directory (..) syntax that should pass
1110+
let valid_parent_config = PathBuf::from("fixtures/linter/../linter/eslintrc.json");
1111+
let result = LintRunner::find_oxlint_config(&cwd, Some(&valid_parent_config));
1112+
assert!(result.is_ok(), "Expected config lookup to succeed with parent directory syntax");
1113+
1114+
// Verify the resolved path is correct
1115+
if let Ok(config) = result {
1116+
assert_eq!(
1117+
config.path.file_name().unwrap().to_str().unwrap(),
1118+
"eslintrc.json",
1119+
"Config file name should be preserved after path resolution"
1120+
);
1121+
}
1122+
}
1123+
10791124
#[test]
10801125
fn test_cross_modules_with_nested_config() {
10811126
let args = &[];

0 commit comments

Comments
 (0)