Skip to content

Commit a540253

Browse files
authored
fix(windows): quote paths in JDTLS and Lombok arguments (#151)
1 parent 325407c commit a540253

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

src/debugger.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use zed_extension_api::{
1212
use crate::{
1313
config::get_java_debug_jar,
1414
lsp::LspWrapper,
15-
util::{create_path_if_not_exists, get_curr_dir, path_to_string, should_use_local_or_download},
15+
util::{
16+
create_path_if_not_exists, get_curr_dir, path_to_quoted_string,
17+
should_use_local_or_download,
18+
},
1619
};
1720

1821
#[derive(Serialize, Deserialize, Debug)]
@@ -152,7 +155,7 @@ impl Debugger {
152155

153156
download_file(
154157
JAVA_DEBUG_PLUGIN_FORK_URL,
155-
&path_to_string(jar_path.clone())?,
158+
&path_to_quoted_string(jar_path.clone())?,
156159
DownloadedFileType::Uncompressed,
157160
)
158161
.map_err(|err| {
@@ -256,7 +259,7 @@ impl Debugger {
256259

257260
download_file(
258261
url.as_str(),
259-
&path_to_string(&jar_path)?,
262+
&path_to_quoted_string(&jar_path)?,
260263
DownloadedFileType::Uncompressed,
261264
)
262265
.map_err(|err| format!("Failed to download {url} {err}"))?;

src/java.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{
3232
try_to_fetch_and_install_latest_lombok,
3333
},
3434
lsp::LspWrapper,
35-
util::path_to_string,
35+
util::path_to_quoted_string,
3636
};
3737

3838
const PROXY_FILE: &str = include_str!("proxy.mjs");
@@ -279,14 +279,15 @@ impl Extension for Java {
279279
"--input-type=module".to_string(),
280280
"-e".to_string(),
281281
PROXY_FILE.to_string(),
282-
path_to_string(current_dir.clone())?,
282+
path_to_quoted_string(current_dir.clone())?,
283283
];
284284

285285
// Add lombok as javaagent if settings.java.jdt.ls.lombokSupport.enabled is true
286286
let lombok_jvm_arg = if is_lombok_enabled(&configuration) {
287287
let lombok_jar_path =
288288
self.lombok_jar_path(language_server_id, &configuration, worktree)?;
289-
let canonical_lombok_jar_path = path_to_string(current_dir.join(lombok_jar_path))?;
289+
let canonical_lombok_jar_path =
290+
path_to_quoted_string(current_dir.join(lombok_jar_path))?;
290291

291292
Some(format!("-javaagent:{canonical_lombok_jar_path}"))
292293
} else {

src/jdk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use zed_extension_api::{
66
set_language_server_installation_status,
77
};
88

9-
use crate::util::{get_curr_dir, path_to_string, remove_all_files_except};
9+
use crate::util::{get_curr_dir, path_to_quoted_string, remove_all_files_except};
1010

1111
// Errors
1212
const JDK_DIR_ERROR: &str = "Failed to read into JDK install directory";
@@ -79,7 +79,7 @@ pub fn try_to_fetch_and_install_latest_jdk(
7979

8080
download_file(
8181
build_corretto_url(&version, &platform, &arch).as_str(),
82-
path_to_string(install_path.clone())?.as_str(),
82+
path_to_quoted_string(install_path.clone())?.as_str(),
8383
match zed::current_platform().0 {
8484
Os::Windows => DownloadedFileType::Zip,
8585
_ => DownloadedFileType::GzipTar,

src/jdtls.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
jdk::try_to_fetch_and_install_latest_jdk,
2020
util::{
2121
create_path_if_not_exists, get_curr_dir, get_java_exec_name, get_java_executable,
22-
get_java_major_version, get_latest_versions_from_tag, path_to_string,
22+
get_java_major_version, get_latest_versions_from_tag, path_to_quoted_string,
2323
remove_all_files_except, should_use_local_or_download,
2424
},
2525
};
@@ -65,14 +65,14 @@ pub fn build_jdtls_launch_args(
6565
let jdtls_data_path = get_jdtls_data_path(worktree)?;
6666

6767
let mut args = vec![
68-
path_to_string(java_executable)?,
68+
path_to_quoted_string(java_executable)?,
6969
"-Declipse.application=org.eclipse.jdt.ls.core.id1".to_string(),
7070
"-Dosgi.bundles.defaultStartLevel=4".to_string(),
7171
"-Declipse.product=org.eclipse.jdt.ls.core.product".to_string(),
7272
"-Dosgi.checkConfiguration=true".to_string(),
7373
format!(
7474
"-Dosgi.sharedConfiguration.area={}",
75-
path_to_string(shared_config_path)?
75+
path_to_quoted_string(shared_config_path)?
7676
),
7777
"-Dosgi.sharedConfiguration.area.readOnly=true".to_string(),
7878
"-Dosgi.configuration.cascaded=true".to_string(),
@@ -86,9 +86,9 @@ pub fn build_jdtls_launch_args(
8686
args.extend(jvm_args);
8787
args.extend(vec![
8888
"-jar".to_string(),
89-
path_to_string(jar_path)?,
89+
path_to_quoted_string(jar_path)?,
9090
"-data".to_string(),
91-
path_to_string(jdtls_data_path)?,
91+
path_to_quoted_string(jdtls_data_path)?,
9292
]);
9393
if java_major_version >= 24 {
9494
args.push("-Djdk.xml.maxGeneralEntitySizeLimit=0".to_string());
@@ -195,10 +195,10 @@ pub fn try_to_fetch_and_install_latest_jdtls(
195195
&format!(
196196
"https://www.eclipse.org/downloads/download.php?file=/jdtls/milestones/{latest_version}/{latest_version_build}"
197197
),
198-
path_to_string(build_path.clone())?.as_str(),
198+
path_to_quoted_string(build_path.clone())?.as_str(),
199199
DownloadedFileType::GzipTar,
200200
)?;
201-
make_file_executable(path_to_string(binary_path)?.as_str())?;
201+
make_file_executable(path_to_quoted_string(binary_path)?.as_str())?;
202202

203203
// ...and delete other versions
204204
let _ = remove_all_files_except(prefix, build_directory.as_str());
@@ -241,7 +241,7 @@ pub fn try_to_fetch_and_install_latest_lombok(
241241
create_path_if_not_exists(prefix)?;
242242
download_file(
243243
&format!("https://projectlombok.org/downloads/{jar_name}"),
244-
path_to_string(jar_path.clone())?.as_str(),
244+
path_to_quoted_string(jar_path.clone())?.as_str(),
245245
DownloadedFileType::Uncompressed,
246246
)?;
247247

src/util.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ pub fn get_java_exec_name() -> String {
176176
/// * [`java_executable`] can't be converted into a String
177177
/// * No major version can be determined
178178
pub fn get_java_major_version(java_executable: &PathBuf) -> zed::Result<u32> {
179-
let program = path_to_string(java_executable).map_err(|_| JAVA_EXEC_ERROR.to_string())?;
179+
let program =
180+
path_to_quoted_string(java_executable).map_err(|_| JAVA_EXEC_ERROR.to_string())?;
180181
let output_bytes = Command::new(program).arg("-version").output()?.stderr;
181182
let output = String::from_utf8(output_bytes).map_err(|e| e.to_string())?;
182183

@@ -246,24 +247,28 @@ fn get_tag_at(github_tags: &Value, index: usize) -> Option<&str> {
246247
})
247248
}
248249

249-
/// Convert [`path`] into [`String`]
250+
/// Converts a [`Path`] into a double-quoted [`String`].
251+
///
252+
/// This function performs two steps in one: it converts the path to a string
253+
/// and wraps the result in double quotes (e.g., `"path/to/file"`).
250254
///
251255
/// # Arguments
252256
///
253-
/// * [`path`] the path of type [`AsRef<Path>`] to convert
257+
/// * `path` - The path of type `AsRef<Path>` to be converted and quoted.
254258
///
255259
/// # Returns
256260
///
257-
/// Returns a String representing [`path`]
261+
/// Returns a `String` representing the `path` enclosed in double quotes.
258262
///
259263
/// # Errors
260264
///
261265
/// This function will return an error when the string conversion fails
262-
pub fn path_to_string<P: AsRef<Path>>(path: P) -> zed::Result<String> {
266+
pub fn path_to_quoted_string<P: AsRef<Path>>(path: P) -> zed::Result<String> {
263267
path.as_ref()
264268
.to_path_buf()
265269
.into_os_string()
266270
.into_string()
271+
.map(|s| format!("\"{}\"", s))
267272
.map_err(|_| PATH_TO_STR_ERROR.to_string())
268273
}
269274

@@ -345,3 +350,25 @@ pub fn should_use_local_or_download(
345350
CheckUpdates::Always => Ok(None),
346351
}
347352
}
353+
354+
#[cfg(test)]
355+
mod tests {
356+
use super::*;
357+
358+
#[test]
359+
fn test_path_to_quoted_string_windows() {
360+
let path = PathBuf::from("C:\\Users\\User Name\\Projects\\zed-extension-java");
361+
let escaped = path_to_quoted_string(&path).unwrap();
362+
assert_eq!(
363+
escaped,
364+
"\"C:\\Users\\User Name\\Projects\\zed-extension-java\""
365+
);
366+
}
367+
368+
#[test]
369+
fn test_path_to_quoted_string_unix() {
370+
let path = PathBuf::from("/home/username/Projects/zed extension java");
371+
let escaped = path_to_quoted_string(&path).unwrap();
372+
assert_eq!(escaped, "\"/home/username/Projects/zed extension java\"");
373+
}
374+
}

0 commit comments

Comments
 (0)