Skip to content

Commit b4819dc

Browse files
authored
fix(java): stabilize Windows launches and cold-index run actions (#9)
1 parent 0e37afa commit b4819dc

14 files changed

Lines changed: 837 additions & 136 deletions

File tree

‎Cargo.lock‎

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 85 additions & 68 deletions
Large diffs are not rendered by default.

‎backend/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ lithe-java-domain = { path = "../crates/java-domain" }
1616
# has no release tag yet; sdk.lock.json and Cargo.lock must name the same commit.
1717
lithe-plugin-sdk = { git = "https://github.com/Lithe-IDEA/Lithe-core-rust", rev = "65cf82343c5e315aae2e335b2e8f0217b703e89a" }
1818
serde_json.workspace = true
19+
base64 = "0.22.1"
20+
zip.workspace = true
21+
url.workspace = true
22+
dunce = "1.0.5"

‎backend/src/launch.rs‎

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,24 @@ pub fn package_root() -> Result<PathBuf, PluginError> {
320320
let executable = std::env::current_exe()
321321
.and_then(fs::canonicalize)
322322
.map_err(|_| unavailable())?;
323-
executable
323+
package_root_from_executable(&executable).ok_or_else(unavailable)
324+
}
325+
326+
// Java instrumentation cannot load -javaagent through a Windows verbatim
327+
// prefix. Simplify only when an ordinary spelling has the same identity;
328+
// canonicalization above still owns symlink resolution.
329+
fn package_root_from_executable(executable: &Path) -> Option<PathBuf> {
330+
dunce::simplified(executable)
324331
.parent()
325332
.and_then(Path::parent)
326333
.map(Path::to_path_buf)
327-
.ok_or_else(unavailable)
328334
}
329335

330336
/// The package path of the Java runtime of `architecture`, as the answer's
331337
/// `executable.path` names it.
332338
pub fn runtime_executable(architecture: &str) -> String {
333-
format!("{RUNTIME_DIRECTORY}/{architecture}/bin/java")
339+
let executable = if cfg!(windows) { "java.exe" } else { "java" };
340+
format!("{RUNTIME_DIRECTORY}/{architecture}/bin/{executable}")
334341
}
335342

336343
/// Finds the JDT LS files of `architecture` in the package at `package_root`.
@@ -544,7 +551,7 @@ fn workspace_folders(host: &Host, workspace: &WorkspaceContext) -> Result<Vec<Pa
544551
/// and reads at most [`TRIGGER_SCAN_ENTRY_BUDGET`] entries. A folder that
545552
/// cannot be read contributes nothing; JDT LS treats a missing list as
546553
/// "nothing to import".
547-
pub fn trigger_files(folders: &[PathBuf]) -> Vec<String> {
554+
pub fn trigger_files(folders: &[PathBuf]) -> Result<Vec<String>, PluginError> {
548555
let mut files = Vec::new();
549556
let mut budget = TRIGGER_SCAN_ENTRY_BUDGET;
550557
let mut roots: Vec<&PathBuf> = folders.iter().collect();
@@ -563,7 +570,7 @@ pub fn trigger_files(folders: &[PathBuf]) -> Vec<String> {
563570
let mut subdirectories = Vec::new();
564571
for entry in entries {
565572
if budget == 0 {
566-
return files;
573+
return Ok(files);
567574
}
568575
budget -= 1;
569576
let name = entry.file_name();
@@ -580,16 +587,16 @@ pub fn trigger_files(folders: &[PathBuf]) -> Vec<String> {
580587
subdirectories.push((entry.path(), depth + 1));
581588
}
582589
} else if kind.is_file() && name.ends_with(".java") && !name.starts_with('.') {
583-
files.push(file_uri(&entry.path()));
590+
files.push(file_uri(&entry.path())?);
584591
if files.len() >= TRIGGER_FILE_LIMIT {
585-
return files;
592+
return Ok(files);
586593
}
587594
}
588595
}
589596
pending.extend(subdirectories);
590597
}
591598
}
592-
files
599+
Ok(files)
593600
}
594601

595602
/// Seconds since the Unix epoch; a clock before 1970 counts as 0, which only
@@ -638,7 +645,7 @@ pub fn resolve_launch_function(
638645
}
639646
None => (Vec::new(), Vec::new()),
640647
};
641-
let trigger_files = trigger_files(&folders);
648+
let trigger_files = trigger_files(&folders)?;
642649
context.check_cancelled()?;
643650
let answer = resolve(
644651
&request,
@@ -655,6 +662,23 @@ pub fn resolve_launch_function(
655662
#[cfg(test)]
656663
mod tests {
657664
use super::*;
665+
666+
#[test]
667+
#[cfg(windows)]
668+
fn package_paths_do_not_break_java_agent_loading() {
669+
// Native Windows JDK 21 exits before initialize when -javaagent uses
670+
// canonicalize's verbatim prefix, even for a normal local package.
671+
let root =
672+
package_root_from_executable(Path::new(r"\\?\C:\plugins\Java 支持\bin\backend.exe"))
673+
.unwrap();
674+
assert_eq!(root, Path::new(r"C:\plugins\Java 支持"));
675+
}
676+
677+
#[test]
678+
#[cfg(windows)]
679+
fn windows_runtime_uses_the_packaged_pe_launcher() {
680+
assert_eq!(runtime_executable("x86_64"), "runtime/x86_64/bin/java.exe");
681+
}
658682
use std::sync::atomic::{AtomicU64, Ordering};
659683
use std::sync::Mutex;
660684

@@ -687,8 +711,9 @@ mod tests {
687711
] {
688712
fs::create_dir_all(package.join(directory)).unwrap();
689713
}
714+
let runtime = runtime_executable("arm64");
690715
for file in [
691-
"runtime/arm64/bin/java",
716+
runtime.as_str(),
692717
LAUNCHER,
693718
"language-server/plugins/org.eclipse.jdt.ls.core_1.50.0.jar",
694719
"language-server/lombok/lombok.jar",
@@ -810,7 +835,7 @@ mod tests {
810835
assert!(reports.is_empty());
811836
assert_eq!(
812837
answer["executable"],
813-
json!({"kind": "packaged", "path": "runtime/arm64/bin/java"})
838+
json!({"kind": "packaged", "path": runtime_executable("arm64")})
814839
);
815840
let arguments = strings(&answer["arguments"]);
816841
let package = fixture.package();
@@ -852,7 +877,7 @@ mod tests {
852877
.iter()
853878
.position(|argument| argument == "-jar")
854879
.unwrap();
855-
assert_eq!(arguments[jar + 1], launcher);
880+
assert_eq!(Path::new(&arguments[jar + 1]), Path::new(&launcher));
856881
assert_eq!(arguments[jar + 2], "-configuration");
857882
let configuration = PathBuf::from(&arguments[jar + 3]);
858883
assert!(configuration.starts_with(fixture.root.join("state")));
@@ -873,16 +898,19 @@ mod tests {
873898
let fixture = Fixture::new();
874899
let (answer, _) = resolve_fixture(&fixture, &[]);
875900
let package = fixture.package();
876-
let expected: Vec<String> = [
901+
let expected: Vec<PathBuf> = [
877902
"language-server/java-debug/com.microsoft.java.debug.plugin-0.53.0.jar",
878903
"language-server/java-test/extensions/a.jar",
879904
"language-server/java-test/extensions/b.jar",
880905
]
881906
.iter()
882-
.map(|bundle| package.join(bundle).to_string_lossy().into_owned())
907+
.map(|bundle| package.join(bundle))
883908
.collect();
884909
assert_eq!(
885-
strings(&answer["initializationOptions"]["bundles"]),
910+
strings(&answer["initializationOptions"]["bundles"])
911+
.into_iter()
912+
.map(PathBuf::from)
913+
.collect::<Vec<_>>(),
886914
expected
887915
);
888916
assert_eq!(
@@ -908,15 +936,13 @@ mod tests {
908936
let (answer, _) = resolve_fixture(&fixture, &[]);
909937
let writable = strings(&answer["writablePaths"]);
910938
assert_eq!(writable.len(), 4);
911-
let hosts: Vec<String> = ["state", "workspace-state", "log", "tmp"]
939+
let hosts: Vec<PathBuf> = ["state", "workspace-state", "log", "tmp"]
912940
.iter()
913-
.map(|directory| fixture.root.join(directory).to_string_lossy().into_owned())
941+
.map(|directory| fixture.root.join(directory))
914942
.collect();
915943
for path in &writable {
916944
assert!(
917-
hosts
918-
.iter()
919-
.any(|host| path == host || path.starts_with(&format!("{host}/"))),
945+
hosts.iter().any(|host| Path::new(path).starts_with(host)),
920946
"{path}"
921947
);
922948
}
@@ -928,7 +954,7 @@ mod tests {
928954
.unwrap();
929955
assert!(writable
930956
.iter()
931-
.any(|path| configuration.starts_with(path) && path.starts_with(&hosts[0])));
957+
.any(|path| configuration.starts_with(path) && Path::new(path).starts_with(&hosts[0])));
932958
}
933959

934960
#[test]
@@ -1000,8 +1026,9 @@ mod tests {
10001026

10011027
#[test]
10021028
fn a_damaged_package_names_the_missing_file() {
1029+
let runtime = runtime_executable("arm64");
10031030
for (remove, expected) in [
1004-
("runtime/arm64/bin/java", "the Java runtime"),
1031+
(runtime.as_str(), "the Java runtime"),
10051032
(LAUNCHER, "the JDT LS launcher"),
10061033
("language-server/lombok/lombok.jar", "the Lombok agent"),
10071034
] {
@@ -1073,16 +1100,18 @@ mod tests {
10731100
] {
10741101
fs::write(workspace.join(file), b"").unwrap();
10751102
}
1076-
let files = trigger_files(&[workspace.clone(), workspace.clone()]);
1103+
let files = trigger_files(&[workspace.clone(), workspace.clone()]).unwrap();
10771104
assert_eq!(
10781105
files,
10791106
vec![
1080-
file_uri(&workspace.join("lib/Util.java")),
1081-
file_uri(&workspace.join("src/demo/Main.java")),
1107+
file_uri(&workspace.join("lib/Util.java")).unwrap(),
1108+
file_uri(&workspace.join("src/demo/Main.java")).unwrap(),
10821109
]
10831110
);
1084-
assert!(trigger_files(&[fixture.root.join("missing")]).is_empty());
1085-
assert!(trigger_files(&[]).is_empty());
1111+
assert!(trigger_files(&[fixture.root.join("missing")])
1112+
.unwrap()
1113+
.is_empty());
1114+
assert!(trigger_files(&[]).unwrap().is_empty());
10861115
}
10871116

10881117
#[test]
@@ -1093,7 +1122,10 @@ mod tests {
10931122
for index in 0..(TRIGGER_FILE_LIMIT + 5) {
10941123
fs::write(workspace.join(format!("C{index:03}.java")), b"").unwrap();
10951124
}
1096-
assert_eq!(trigger_files(&[workspace]).len(), TRIGGER_FILE_LIMIT);
1125+
assert_eq!(
1126+
trigger_files(&[workspace]).unwrap().len(),
1127+
TRIGGER_FILE_LIMIT
1128+
);
10971129
}
10981130

10991131
#[test]

‎backend/src/lib.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ pub fn backend() -> Result<Backend, SdkError> {
198198
Ok(())
199199
}
200200
})?;
201-
backend.function(COLLECT_FUNCTION_ID, run::collect_actions_function)?;
201+
backend.function(COLLECT_FUNCTION_ID, {
202+
let run_session = Arc::clone(&run_session);
203+
move |params, context| run::collect_actions_function(&run_session, params, context)
204+
})?;
202205
backend.suppress_success_diagnostics(COLLECT_FUNCTION_ID)?;
203206
backend.function(RUN_ACTION_FUNCTION_ID, {
204207
let run_session = Arc::clone(&run_session);

0 commit comments

Comments
 (0)