Skip to content

Commit 89cc21b

Browse files
committed
fix(language_server): normalize oxlintrc config path (#10982)
closes #10941
1 parent 17e49c3 commit 89cc21b

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::{Component, Path, PathBuf};
22
use std::sync::Arc;
33

44
use globset::Glob;
@@ -28,8 +28,8 @@ impl ServerLinter {
2828
let nested_configs = Self::create_nested_configs(root_uri, options);
2929
let root_path = root_uri.to_file_path().unwrap();
3030
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));
3333
if config.try_exists().is_ok_and(|exists| exists) {
3434
if let Ok(oxlintrc) = Oxlintrc::from_file(&config) {
3535
oxlintrc
@@ -206,19 +206,55 @@ impl ServerLinter {
206206
}
207207
}
208208

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+
209234
#[cfg(test)]
210235
mod test {
211-
use std::{path::PathBuf, str::FromStr};
236+
use std::{
237+
path::{Path, PathBuf},
238+
str::FromStr,
239+
};
212240

213241
use rustc_hash::FxHashMap;
214242
use tower_lsp_server::lsp_types::Uri;
215243

216244
use crate::{
217245
Options,
218-
linter::server_linter::ServerLinter,
246+
linter::server_linter::{ServerLinter, normalize_path},
219247
tester::{Tester, get_file_uri},
220248
};
221249

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+
222258
#[test]
223259
fn test_create_nested_configs_with_disabled_nested_configs() {
224260
let mut flags = FxHashMap::default();

0 commit comments

Comments
 (0)