From 18fb0654597dfa1a9f5ca7ca6cd933809cd38d10 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:29:42 +0800 Subject: [PATCH] fix(lsp): accept file URIs a language server leaves unencoded MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jdtls on Windows sends a project path as is in its URIs (file:///H:/项目/…). lsp-types rejects such a URI, so the whole response failed: Find Usages, Go to Declaration, references, implementations and call hierarchy fell back to a text search for any project under a non-ASCII folder. Every message from a language server now has the characters URIs don't allow (non-ASCII, spaces, brackets…) percent-encoded in its file: URIs, string values and object keys (a workspace edit's changes) alike, before it is read. Fixes #3205 --- crates/ide-jsonrpc/src/lib.rs | 14 +++++-- crates/ide-jsonrpc/src/tests.rs | 26 ++++++++++++ crates/ide-lsp/src/handler.rs | 4 ++ crates/ide-lsp/src/uri.rs | 71 +++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 4 deletions(-) diff --git a/crates/ide-jsonrpc/src/lib.rs b/crates/ide-jsonrpc/src/lib.rs index a44c98a15b..13b841103e 100644 --- a/crates/ide-jsonrpc/src/lib.rs +++ b/crates/ide-jsonrpc/src/lib.rs @@ -223,6 +223,10 @@ pub trait Handler: Send + Sync + 'static { /// Called once the response to a `method` request is queued: what it sends follows the response. fn after_response(&self, _method: &str) {} + + /// Adjusts every message from the peer (responses included) before it is dispatched, e.g. + /// to repair values the peer spells more loosely than the types that read them accept. + fn normalize_incoming(&self, _message: &mut Value) {} } /// A [`Handler`] that ignores notifications and rejects requests. @@ -585,9 +589,10 @@ impl Peer { .expect("spawn jsonrpc notification thread"); } - // Reader: parses and dispatches; never runs handler code itself. + // Reader: parses and dispatches; runs no handler code beyond `normalize_incoming`. { let inner = inner.clone(); + let handler = handler.clone(); tokio::spawn(async move { let mut reader = BufReader::new(reader); let seen_message = AtomicBool::new(false); @@ -610,7 +615,7 @@ impl Peer { match msg { Ok(Some(body)) => { seen_message.store(true, Ordering::Relaxed); - dispatch(&inner, ¬if_tx, &body); + dispatch(&inner, &*handler, ¬if_tx, &body); } Ok(None) => break, Err(e) => { @@ -783,15 +788,16 @@ async fn handle_request(inner: Arc, handler: Arc, id: Value, } } -fn dispatch(inner: &Arc, notif_tx: &std::sync::mpsc::Sender, body: &[u8]) { +fn dispatch(inner: &Arc, handler: &dyn Handler, notif_tx: &std::sync::mpsc::Sender, body: &[u8]) { tracing::trace!(target: "ide_jsonrpc::wire", "<-- {}", String::from_utf8_lossy(body)); - let msg: Value = match serde_json::from_slice(body) { + let mut msg: Value = match serde_json::from_slice(body) { Ok(v) => v, Err(e) => { tracing::warn!("jsonrpc: unparsable message: {e}"); return; } }; + handler.normalize_incoming(&mut msg); let Value::Object(mut obj) = msg else { tracing::warn!("jsonrpc: ignoring non-object message"); return; diff --git a/crates/ide-jsonrpc/src/tests.rs b/crates/ide-jsonrpc/src/tests.rs index c851cf66dd..d5b71a6592 100644 --- a/crates/ide-jsonrpc/src/tests.rs +++ b/crates/ide-jsonrpc/src/tests.rs @@ -541,3 +541,29 @@ async fn output_before_the_handshake_is_reported_not_dropped() { assert!(lines.iter().any(|l| l == "python: not found"), "{lines:?}"); assert!(lines.iter().any(|l| l.ends_with(". . .") && l.len() > 6), "{lines:?}"); } + +#[tokio::test(flavor = "multi_thread")] +async fn incoming_messages_are_normalized_before_dispatch() { + struct Server; + impl Handler for Server { + fn on_notification(&self, _: &str, _: Value) {} + fn on_request(&self, _: &str, params: Value) -> Result { + Ok(params) + } + } + struct Client; + impl Handler for Client { + fn on_notification(&self, _: &str, _: Value) {} + fn on_request(&self, _: &str, _: Value) -> Result { + Err((codes::METHOD_NOT_FOUND, "no".into())) + } + fn normalize_incoming(&self, message: &mut Value) { + if let Some(result) = message.get_mut("result") { + *result = json!("normalized"); + } + } + } + let (client, _server) = pair(Arc::new(Client), Arc::new(Server)); + let v: Value = client.request("echo", "raw", None).await.unwrap(); + assert_eq!(v, json!("normalized")); +} diff --git a/crates/ide-lsp/src/handler.rs b/crates/ide-lsp/src/handler.rs index cb151c441c..bba5adc6d1 100644 --- a/crates/ide-lsp/src/handler.rs +++ b/crates/ide-lsp/src/handler.rs @@ -391,6 +391,10 @@ fn tsserver_ran_out_of_memory(text: &str) -> bool { } impl Handler for ClientHandler { + fn normalize_incoming(&self, message: &mut Value) { + crate::uri::encode_loose_file_uris(message); + } + fn on_notification(&self, method: &str, params: Value) { let Some(mgr) = self.mgr.upgrade() else { return }; match method { diff --git a/crates/ide-lsp/src/uri.rs b/crates/ide-lsp/src/uri.rs index 6eb752ccdf..017f822165 100644 --- a/crates/ide-lsp/src/uri.rs +++ b/crates/ide-lsp/src/uri.rs @@ -1,6 +1,8 @@ //! `file://` URI ⇄ path conversion (RFC 8089, VS Code-compatible encoding). use percent_encoding::{AsciiSet, CONTROLS, percent_decode_str, utf8_percent_encode}; +use serde_json::Value; +use std::borrow::Cow; use std::path::{Path, PathBuf}; use std::str::FromStr; @@ -22,6 +24,53 @@ const PATH: &AsciiSet = &CONTROLS .add(b'|') .add(b'}'); +/// What RFC 3986 doesn't allow in a URI and a server may still leave unencoded in a `file:` +/// URI (non-ASCII is always encoded); `%` stays, it starts the escapes already there. +const LOOSE: &AsciiSet = &CONTROLS + .add(b' ') + .add(b'"') + .add(b'<') + .add(b'>') + .add(b'[') + .add(b'\\') + .add(b']') + .add(b'^') + .add(b'`') + .add(b'{') + .add(b'|') + .add(b'}'); + +/// Percent-encodes the characters URIs don't allow in every `file:` URI in `value`, string +/// values and object keys alike (a workspace edit's `changes`). Eclipse's jdtls sends a path +/// as is (`file:///H:/项目/…`), which `lsp_types::Uri` rejects, failing the whole response. +pub fn encode_loose_file_uris(value: &mut Value) { + match value { + Value::String(s) => { + if let Some(encoded) = encode_loose(s) { + *s = encoded; + } + } + Value::Array(items) => items.iter_mut().for_each(encode_loose_file_uris), + Value::Object(map) => { + if map.keys().any(|k| encode_loose(k).is_some()) { + *map = std::mem::take(map).into_iter().map(|(k, v)| (encode_loose(&k).unwrap_or(k), v)).collect(); + } + map.values_mut().for_each(encode_loose_file_uris); + } + _ => {} + } +} + +fn encode_loose(s: &str) -> Option { + if !s.get(..5).is_some_and(|scheme| scheme.eq_ignore_ascii_case("file:")) { + return None; + } + match Cow::from(utf8_percent_encode(s, LOOSE)) { + Cow::Owned(encoded) => Some(encoded), + Cow::Borrowed(_) => None, + } +} + pub fn path_to_uri_string(path: &Path) -> String { let s = if cfg!(windows) { dunce::simplified(path).to_string_lossy().replace('\\', "/") @@ -95,6 +144,28 @@ pub use ide_core::text::is_virtual_uri; mod tests { use super::*; + #[test] + fn file_uris_a_server_leaves_unencoded_are_encoded() { + // jdtls on a project under a Chinese folder (`java.usages` / references response). + let mut v = serde_json::json!({ "result": [{ + "location": { "uri": "file:///H:/项目/online disk/A.java", "range": { "start": { "line": 1, "character": 2 }, "end": { "line": 1, "character": 3 } } }, + "kind": "unknown" + }], "edit": { "changes": { "file:///tmp/目录/b.java": [] } }, "text": "项目" }); + let raw = v["result"][0]["location"].clone(); + assert!(serde_json::from_value::(raw).is_err(), "lsp-types rejects it as sent"); + encode_loose_file_uris(&mut v); + let uri = v["result"][0]["location"]["uri"].as_str().unwrap(); + assert_eq!(uri, "file:///H:/%E9%A1%B9%E7%9B%AE/online%20disk/A.java"); + let location: lsp_types::Location = serde_json::from_value(v["result"][0]["location"].clone()).unwrap(); + assert!(uri_to_path(location.uri.as_str()).unwrap().ends_with("项目/online disk/A.java")); + assert!(v["edit"]["changes"].get("file:///tmp/%E7%9B%AE%E5%BD%95/b.java").is_some()); + assert_eq!(v["text"], "项目", "only file: URIs change"); + // Already encoded: untouched. + let mut encoded = serde_json::json!("file:///a%20b/%E9%A1%B9.java"); + encode_loose_file_uris(&mut encoded); + assert_eq!(encoded, "file:///a%20b/%E9%A1%B9.java"); + } + #[cfg(unix)] #[test] fn round_trip() {