Skip to content

Commit d309e07

Browse files
authored
fix(language_server): fix panics when paths contains specials characters like [ or ] (#10622)
closes #10575
1 parent 1962bc6 commit d309e07

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

crates/oxc_language_server/src/linter/error_with_position.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use std::{borrow::Cow, path::PathBuf, str::FromStr};
1+
use std::{borrow::Cow, str::FromStr};
22

33
use oxc_linter::MessageWithPosition;
4-
use tower_lsp_server::{
5-
UriExt,
6-
lsp_types::{
7-
self, CodeDescription, DiagnosticRelatedInformation, NumberOrString, Position, Range, Uri,
8-
},
4+
use tower_lsp_server::lsp_types::{
5+
self, CodeDescription, DiagnosticRelatedInformation, NumberOrString, Position, Range, Uri,
96
};
107

118
use oxc_diagnostics::Severity;
@@ -32,13 +29,12 @@ fn cmp_range(first: &Range, other: &Range) -> std::cmp::Ordering {
3229

3330
fn message_with_position_to_lsp_diagnostic(
3431
message: &MessageWithPosition<'_>,
35-
path: &PathBuf,
32+
uri: &Uri,
3633
) -> lsp_types::Diagnostic {
3734
let severity = match message.severity {
3835
Severity::Error => Some(lsp_types::DiagnosticSeverity::ERROR),
3936
_ => Some(lsp_types::DiagnosticSeverity::WARNING),
4037
};
41-
let uri = lsp_types::Uri::from_file_path(path).unwrap();
4238

4339
let related_information = message.labels.as_ref().map(|spans| {
4440
spans
@@ -103,10 +99,10 @@ fn message_with_position_to_lsp_diagnostic(
10399

104100
pub fn message_with_position_to_lsp_diagnostic_report(
105101
message: &MessageWithPosition<'_>,
106-
path: &PathBuf,
102+
uri: &Uri,
107103
) -> DiagnosticReport {
108104
DiagnosticReport {
109-
diagnostic: message_with_position_to_lsp_diagnostic(message, path),
105+
diagnostic: message_with_position_to_lsp_diagnostic(message, uri),
110106
fixed_content: message.fix.as_ref().map(|infos| FixedContent {
111107
message: infos.span.message().map(std::string::ToString::to_string),
112108
code: infos.content.to_string(),

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,19 @@ impl IsolatedLintHandler {
3838
Self { linter, options }
3939
}
4040

41-
pub fn run_single(
42-
&self,
43-
path: &Path,
44-
content: Option<String>,
45-
) -> Option<Vec<DiagnosticReport>> {
46-
if !Self::should_lint_path(path) {
41+
pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<DiagnosticReport>> {
42+
let path = uri.to_file_path()?;
43+
44+
if !Self::should_lint_path(&path) {
4745
return None;
4846
}
4947

5048
let allocator = Allocator::default();
5149

52-
Some(self.lint_path(&allocator, path, content).map_or(vec![], |errors| {
53-
let path_buf = &path.to_path_buf();
54-
50+
Some(self.lint_path(&allocator, &path, content).map_or(vec![], |errors| {
5551
let mut diagnostics: Vec<DiagnosticReport> = errors
5652
.iter()
57-
.map(|e| message_with_position_to_lsp_diagnostic_report(e, path_buf))
53+
.map(|e| message_with_position_to_lsp_diagnostic_report(e, uri))
5854
.collect();
5955

6056
// a diagnostics connected from related_info to original diagnostic
@@ -64,10 +60,7 @@ impl IsolatedLintHandler {
6460
continue;
6561
};
6662
let related_information = Some(vec![DiagnosticRelatedInformation {
67-
location: lsp_types::Location {
68-
uri: Uri::from_file_path(path).unwrap(),
69-
range: d.diagnostic.range,
70-
},
63+
location: lsp_types::Location { uri: uri.clone(), range: d.diagnostic.range },
7164
message: "original diagnostic".to_string(),
7265
}]);
7366
for r in related_info {

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use tower_lsp_server::{UriExt, lsp_types::Uri};
3+
use tower_lsp_server::lsp_types::Uri;
44

55
use oxc_linter::Linter;
66

@@ -35,7 +35,7 @@ impl ServerLinter {
3535
}
3636

3737
pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<DiagnosticReport>> {
38-
self.isolated_linter.run_single(&uri.to_file_path().unwrap(), content)
38+
self.isolated_linter.run_single(uri, content)
3939
}
4040
}
4141

@@ -107,6 +107,8 @@ mod test {
107107
Tester::new().test_and_snapshot_single_file("fixtures/linter/astro/debugger.astro");
108108
Tester::new().test_and_snapshot_single_file("fixtures/linter/vue/debugger.vue");
109109
Tester::new().test_and_snapshot_single_file("fixtures/linter/svelte/debugger.svelte");
110+
// ToDo: fix Tester to work only with Uris and do not access the file system
111+
// Tester::new().test_and_snapshot_single_file("fixtures/linter/nextjs/%5B%5B..rest%5D%5D/debugger.ts");
110112
}
111113

112114
#[test]

0 commit comments

Comments
 (0)