Skip to content

Commit 686367e

Browse files
authored
Use hash for proxy's hex_encoding (#319)
- Reuse existing hash-encoding pattern to avoid creating filenames breaching MacOS 255-bytes limit - Update debugger to use same hash-encoding when retrieving the HTTP port - Address #318
1 parent 7cc50c1 commit 686367e

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎proxy/src/main.rs‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use proxy_common::{
1515
spawn_parent_monitor, LspReader,
1616
};
1717
use serde_json::{json, Value};
18+
use sha1::{Digest, Sha1};
1819
use std::{
1920
collections::{HashMap, HashSet},
2021
env, fs,
@@ -588,7 +589,7 @@ fn queue_rewrite_response(
588589
// --- Utilities ---
589590

590591
fn hex_encode(s: &str) -> String {
591-
s.as_bytes().iter().map(|b| format!("{b:02x}")).collect()
592+
hex::encode(Sha1::digest(s.as_bytes()))
592593
}
593594

594595
fn clear_latest_workspace(latest: &Mutex<Option<u64>>, token: u64) {
@@ -1246,4 +1247,34 @@ mod tests {
12461247

12471248
assert_eq!(completion_resolve_fallback(&error, &tracked), None);
12481249
}
1250+
1251+
#[test]
1252+
fn proxy_id_stays_under_macos_filename_limit() {
1253+
let long_workspace = format!("/Users/test/{}", "a".repeat(200));
1254+
1255+
let id = hex_encode(&long_workspace);
1256+
1257+
assert!(
1258+
id.len() <= 255,
1259+
"proxy_id exceeded macOS filename limit: {} bytes",
1260+
id.len()
1261+
);
1262+
assert_eq!(id.len(), 40, "SHA-1 hex should always be 40 chars");
1263+
}
1264+
1265+
#[test]
1266+
fn proxy_port_file_works_for_long_workdir() {
1267+
let temp = std::env::temp_dir().join("java-proxy-long-path-test");
1268+
let _ = fs::remove_dir_all(&temp);
1269+
let long_workspace = temp.join("a".repeat(200));
1270+
fs::create_dir_all(&long_workspace).unwrap();
1271+
1272+
let proxy_id = hex_encode(long_workspace.to_string_lossy().trim_end_matches('/'));
1273+
let port_file = long_workspace.join("proxy").join(&proxy_id);
1274+
1275+
fs::create_dir_all(port_file.parent().unwrap()).unwrap();
1276+
fs::write(&port_file, "3000").unwrap();
1277+
1278+
assert!(port_file.file_name().unwrap().len() <= 255);
1279+
}
12491280
}

‎src/lsp.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55

66
use serde::{Deserialize, Serialize, de::DeserializeOwned};
77
use serde_json::json;
8+
use sha1::{Digest, Sha1};
89
use zed_extension_api::{
910
self as zed,
1011
http_client::{HttpMethod, HttpRequest, fetch},
@@ -89,11 +90,7 @@ where
8990
}
9091

9192
fn string_to_hex(s: &str) -> String {
92-
let mut hex_string = String::new();
93-
for byte in s.as_bytes() {
94-
hex_string.push_str(&format!("{byte:02x}"));
95-
}
96-
hex_string
93+
hex::encode(Sha1::digest(s.as_bytes()))
9794
}
9895

9996
#[derive(Serialize, Deserialize)]

0 commit comments

Comments
 (0)