|
1 | 1 | use std::{ |
2 | 2 | env, fs, |
3 | 3 | io::{ErrorKind, Write}, |
4 | | - path::{Path, PathBuf}, |
| 4 | + path::{Path, PathBuf, absolute}, |
5 | 5 | time::Instant, |
6 | 6 | }; |
7 | 7 |
|
@@ -475,7 +475,23 @@ impl LintRunner { |
475 | 475 | // when no file is found, the default configuration is returned |
476 | 476 | fn find_oxlint_config(cwd: &Path, config: Option<&PathBuf>) -> Result<Oxlintrc, String> { |
477 | 477 | 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 | + }; |
479 | 495 | return match Oxlintrc::from_file(&full_path) { |
480 | 496 | Ok(config) => Ok(config), |
481 | 497 | Err(diagnostic) => { |
@@ -1076,6 +1092,35 @@ mod test { |
1076 | 1092 | Tester::new().with_cwd("fixtures".into()).test_and_snapshot(args); |
1077 | 1093 | } |
1078 | 1094 |
|
| 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 | + |
1079 | 1124 | #[test] |
1080 | 1125 | fn test_cross_modules_with_nested_config() { |
1081 | 1126 | let args = &[]; |
|
0 commit comments