|
1 | | -use std::path::{Path, PathBuf}; |
| 1 | +use std::path::{Component, Path, PathBuf}; |
2 | 2 | use std::sync::Arc; |
3 | 3 |
|
4 | 4 | use globset::Glob; |
@@ -28,8 +28,8 @@ impl ServerLinter { |
28 | 28 | let nested_configs = Self::create_nested_configs(root_uri, options); |
29 | 29 | let root_path = root_uri.to_file_path().unwrap(); |
30 | 30 | let relative_config_path = options.config_path.clone(); |
31 | | - let oxlintrc = if relative_config_path.is_some() { |
32 | | - let config = root_path.join(relative_config_path.unwrap()); |
| 31 | + let oxlintrc = if let Some(relative_config_path) = relative_config_path { |
| 32 | + let config = normalize_path(root_path.join(relative_config_path)); |
33 | 33 | if config.try_exists().is_ok_and(|exists| exists) { |
34 | 34 | if let Ok(oxlintrc) = Oxlintrc::from_file(&config) { |
35 | 35 | oxlintrc |
@@ -206,19 +206,55 @@ impl ServerLinter { |
206 | 206 | } |
207 | 207 | } |
208 | 208 |
|
| 209 | +/// Normalize a path by removing `.` and resolving `..` components, |
| 210 | +/// without touching the filesystem. |
| 211 | +pub fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf { |
| 212 | + let mut result = PathBuf::new(); |
| 213 | + |
| 214 | + for component in path.as_ref().components() { |
| 215 | + match component { |
| 216 | + Component::ParentDir => { |
| 217 | + result.pop(); |
| 218 | + } |
| 219 | + Component::CurDir => { |
| 220 | + // Skip current directory component |
| 221 | + } |
| 222 | + Component::Normal(c) => { |
| 223 | + result.push(c); |
| 224 | + } |
| 225 | + Component::RootDir | Component::Prefix(_) => { |
| 226 | + result.push(component.as_os_str()); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + result |
| 232 | +} |
| 233 | + |
209 | 234 | #[cfg(test)] |
210 | 235 | mod test { |
211 | | - use std::{path::PathBuf, str::FromStr}; |
| 236 | + use std::{ |
| 237 | + path::{Path, PathBuf}, |
| 238 | + str::FromStr, |
| 239 | + }; |
212 | 240 |
|
213 | 241 | use rustc_hash::FxHashMap; |
214 | 242 | use tower_lsp_server::lsp_types::Uri; |
215 | 243 |
|
216 | 244 | use crate::{ |
217 | 245 | Options, |
218 | | - linter::server_linter::ServerLinter, |
| 246 | + linter::server_linter::{ServerLinter, normalize_path}, |
219 | 247 | tester::{Tester, get_file_uri}, |
220 | 248 | }; |
221 | 249 |
|
| 250 | + #[test] |
| 251 | + fn test_normalize_path() { |
| 252 | + assert_eq!( |
| 253 | + normalize_path(Path::new("/root/directory/./.oxlintrc.json")), |
| 254 | + Path::new("/root/directory/.oxlintrc.json") |
| 255 | + ); |
| 256 | + } |
| 257 | + |
222 | 258 | #[test] |
223 | 259 | fn test_create_nested_configs_with_disabled_nested_configs() { |
224 | 260 | let mut flags = FxHashMap::default(); |
|
0 commit comments