Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
> 在内存中补全类路径并自动找到 Tomcat 等 Servlet 容器,不修改项目文件,详见 [docs/project-models.md](docs/project-models.md)。
>
> **3.0.0 起使用你自己的 JDT LS**:插件包不再附带 Eclipse JDT Language Server 和运行它的 JDK(包从约 250 MB 降到约 11 MB),
> 做法和 Go 插件使用你自己的 `gopls` 一样。需要先安装 JDT LS(例如 `brew install jdtls`)和 JDK 21 或更新版本,
> 做法和 Go 插件使用你自己的 `gopls` 一样。需要先安装 JDT LS 和满足其要求的运行 JDK(至少 21,JDT LS 1.63+ 需要 25),
> 详见下文[语言服务器与 JDK](#语言服务器与-jdk)。

> **Viva 预览版 Java 支持(1.2.0)**:清单已迁到当前契约 0,后端使用 Core
Expand Down Expand Up @@ -204,16 +204,28 @@ Spring/MyBatis 等既有能力不属于本轮 Maven 验收范围。
5. VS Code 及其衍生编辑器里 Java 扩展(`redhat.java`)自带的 `server/`,取最新版本。

目录(或其中的 `libexec/`)里要有唯一一个 `plugins/org.eclipse.equinox.launcher_*.jar` 和当前平台的
`config_<平台>/config.ini`。安装方式举例:macOS / Linux 用 `brew install jdtls`;也可以从
[download.eclipse.org/jdtls](https://download.eclipse.org/jdtls/milestones/) 下载解压,再在设置里选目录。
`config_<平台>/config.ini`。从 [Eclipse 官方下载目录](https://download.eclipse.org/jdtls/milestones/) 选择版本,
下载归档并核对该版本发布的校验值,解压到自己的可写目录,再在设置里选择解压后的根目录(不是压缩包或 `bin`)。
macOS 已安装 Homebrew 时也可用 `brew install jdtls`;Windows 和 Linux 不再收到这条 macOS 指引。

| 系统 / 架构 | 必须存在的配置文件 |
| --- | --- |
| Windows x64 / ARM64 | `config_win/config.ini` |
| macOS x64 / ARM64 | `config_mac/config.ini` / `config_mac_arm/config.ini` |
| Linux x64 / ARM64 | `config_linux/config.ini` / `config_linux_arm/config.ini` |

已测试 JDT LS 1.61.0(CI 固定,见 `third_party/jdtls/manifest.json`)和 Homebrew 1.60.0。
2. **满足目标 JDT LS 要求的 JDK(至少 JDK 21;JDT LS 1.63+ 要求 JDK 25)**,用来运行 JDT LS。它是插件新增的「JDK for JDT LS」工具(`jdtls-runtime`),
在设置 → 构建、执行、部署 → 工具链里显示和选择;没选时依次用登录 shell 的 `java`(仅匹配 21 及更新版本,跳过 `PATH` 上的 Java 8/11/17)和找到的最高版本 JDK(含系统 JDK 目录与 VS Code / Cursor `redhat.java` 扩展自带的 `jre/`)。
后端会从目标 JDT LS 核心 bundle 的 `MANIFEST.MF`(`Bundle-RequiredExecutionEnvironment`)读取实际最低 Java 版本,低于该版本时启动直接报错并提示去工具链里选择。项目自己的 JDK 是原来的「JDK」工具(`java`),两者互不影响。
JDT 只能为它支持的 Java 版本编译:没有为项目选 JDK 时,JDT 用运行它的 JDK 编译,这个 JDK 比 JDT LS 支持的最新 Java 还新时编译会报错。

找不到 JDT LS 时,语言服务启动失败(`launch.server_missing`),错误信息说明如何安装或去哪里设置。
宿主当前的 `commandInstallers` 只能安装 `systemCommand` 启动的命令,JDT LS 由插件后端启动,所以还不能像 Go 插件那样在插件详情里一键安装。
找不到 JDT LS 时返回 `launch.server_missing`,显式目录无效时返回 `launch.server_invalid`;两种错误均提供对应系统的目录结构、官方下载地址和本地选择入口。显式目录无效时不会静默改用其他安装,也不会清空配置。运行 JDK 过旧时返回 `launch.runtime_too_old`,按所选 JDT LS 的实际 bundle 要求提示版本,而不是先固定报 Java 21。

未安装运行 JDK 时,从 [Eclipse Temurin 官方页面](https://adoptium.net/temurin/releases/) 选择满足要求、匹配系统和 CPU 架构的 **JDK** 归档,核对发布的校验值后解压。在「JDK for JDT LS」工具链中选择其根目录(含 `bin/java.exe` 或 `bin/java`;macOS `.jdk` 内为 `Contents/Home`)。保留项目「JDK」选择和全局 PATH,配置后重启 Java 语言服务器。下载或校验失败时不要选择或执行该归档中的程序;重新从官方来源获取。

这是**下载与本地配置引导,不是自动安装器**:现有目录选择器负责用户确认,错误中的 URL 是文本地址,不保证宿主渲染为可点击链接。清单设置说明同步提供中英文引导;后端错误文案仍受 API-37 本地化接口缺口限制,以英文返回。没有运行版本事实时继续交由宿主工具链解析处理,不把“未知”误报为“已兼容”。
宿主已有插件状态目录,但 API-38 托管网络/下载尚未实现;当前 `commandInstallers` 只能安装 `systemCommand` 启动的命令,JDT LS 使用后端启动。本次不增加未校验下载、静默安装、全局 PATH 修改、项目 JDK 修改或宿主 Java 特判。

## 工作方式

Expand Down
118 changes: 106 additions & 12 deletions backend/src/jdtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,46 @@ const EDITOR_EXTENSION_ENTRY_BUDGET: usize = 4096;
/// TODO(host-api: API-37 i18n): user-facing text must go through the host's
/// i18n table.
pub const SERVER_MISSING_MESSAGE: &str = "Java Support needs the Eclipse JDT Language Server, \
which was not found on this computer. Install it (for example with `brew install jdtls`), \
or choose its folder in Settings > Languages & Frameworks > Java > Language server folder.";
which was not found on this computer.";
/// Message of [`LAUNCH_SERVER_INVALID`].
///
/// TODO(host-api: API-37 i18n): user-facing text must go through the host's
/// i18n table.
pub const SERVER_INVALID_MESSAGE: &str = "The language server folder chosen in the Java \
settings does not contain the Eclipse JDT Language Server for this platform. Choose the \
folder that contains plugins and the config folders, or clear the setting to find JDT LS \
automatically.";
settings does not contain a usable Eclipse JDT Language Server for this platform. \
Your explicit choice has been kept; no other installation was selected. \
Choose a valid folder, or clear the setting to enable automatic discovery.";

/// Official distribution pages. These are guidance only: launch never downloads
/// or executes an installer, changes PATH, or changes either toolchain selection.
pub const SERVER_DOWNLOAD_URL: &str = "https://download.eclipse.org/jdtls/milestones/";
pub const JDK_DOWNLOAD_URL: &str = "https://adoptium.net/temurin/releases/";

/// Recovery instructions use the requested platform, not the backend build host,
/// so Windows never receives a Homebrew command or a macOS configuration folder.
/// TODO(host-api: API-37 i18n): localize backend messages when the host supports it.
pub fn server_setup_message(reason: &str, os: &str, arch: &str) -> String {
let Some(configuration) = configuration_name(os, arch) else {
return format!(
"{reason} This operating system or architecture is not supported by Java Support."
);
};
let package_manager = if os == "macos" {
" On macOS with Homebrew installed, you can also use `brew install jdtls`."
} else {
""
};
format!(
"{reason} Download JDT LS from {SERVER_DOWNLOAD_URL}, verify the published checksum, \
then extract the archive into a user-owned folder.{package_manager} In Settings > Languages & \
Frameworks > Java > Language server folder, select the extracted folder containing exactly one \
plugins/org.eclipse.equinox.launcher_*.jar and {configuration}/config.ini (not the archive or bin folder). \
JDT LS also needs a separate runtime JDK: at least Java 21, or Java 25 for JDT LS 1.63+. \
Download a JDK for your operating system and architecture from {JDK_DOWNLOAD_URL} and select it \
as \"JDK for JDT LS\" in Settings > Build, Execution, Deployment > Toolchains. \
Keep the project's \"JDK\" selection and global PATH unchanged. Restart the Java language server after configuring."
)
}

/// The JDT LS files a launch uses, absolute.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -302,11 +332,19 @@ pub fn locate_for_runtime(
arch: &str,
runtime_java_version: Option<u64>,
) -> Result<ServerInstallation, PluginError> {
let configuration = configuration_name(os, arch)
.ok_or_else(|| PluginError::new(LAUNCH_SERVER_MISSING, SERVER_MISSING_MESSAGE))?;
let configuration = configuration_name(os, arch).ok_or_else(|| {
PluginError::new(
LAUNCH_SERVER_MISSING,
server_setup_message(SERVER_MISSING_MESSAGE, os, arch),
)
})?;
if let Some(folder) = configured {
return inspect(folder, configuration)
.ok_or_else(|| PluginError::new(LAUNCH_SERVER_INVALID, SERVER_INVALID_MESSAGE));
return inspect(folder, configuration).ok_or_else(|| {
PluginError::new(
LAUNCH_SERVER_INVALID,
server_setup_message(SERVER_INVALID_MESSAGE, os, arch),
)
});
}
let mut first = None;
for folder in candidates(environment, os) {
Expand All @@ -320,7 +358,12 @@ pub fn locate_for_runtime(
first = Some(installation);
}
}
first.ok_or_else(|| PluginError::new(LAUNCH_SERVER_MISSING, SERVER_MISSING_MESSAGE))
first.ok_or_else(|| {
PluginError::new(
LAUNCH_SERVER_MISSING,
server_setup_message(SERVER_MISSING_MESSAGE, os, arch),
)
})
}

/// The folders [`locate`] tries without a configured one, in order. A folder
Expand Down Expand Up @@ -529,20 +572,71 @@ mod tests {
// An unusable choice fails instead of falling back to another installation.
let error = locate(Some(&temp.0.join("empty")), &search, "linux", "x86_64").unwrap_err();
assert_eq!(error.code(), LAUNCH_SERVER_INVALID);
assert_eq!(error.message(), SERVER_INVALID_MESSAGE);
assert_eq!(
error.message(),
server_setup_message(SERVER_INVALID_MESSAGE, "linux", "x86_64")
);
// Without a choice the discovered installation is used.
assert_eq!(
locate(None, &search, "linux", "x86_64").unwrap().home,
discovered
);
}

#[test]
fn setup_guidance_matches_each_supported_platform() {
for (os, arch, config) in [
("windows", "x86_64", "config_win"),
("windows", "arm64", "config_win"),
("macos", "x86_64", "config_mac"),
("macos", "arm64", "config_mac_arm"),
("linux", "x86_64", "config_linux"),
("linux", "arm64", "config_linux_arm"),
] {
for reason in [SERVER_MISSING_MESSAGE, SERVER_INVALID_MESSAGE] {
let message = server_setup_message(reason, os, arch);
assert!(
message.contains(&format!("{config}/config.ini")),
"{os}/{arch}"
);
assert_eq!(message.contains("brew install"), os == "macos");
assert!(message.contains(SERVER_DOWNLOAD_URL));
assert!(message.contains(JDK_DOWNLOAD_URL));
assert!(message.contains("checksum"));
assert!(message.contains("global PATH unchanged"));
}
}
let unsupported = server_setup_message(SERVER_MISSING_MESSAGE, "plan9", "x86_64");
assert!(unsupported.contains("not supported"));
assert!(!unsupported.contains("Download"));
}

#[test]
fn missing_windows_server_has_official_download_and_local_selection_guidance() {
let temp = TempDir::new();
let error = locate(None, &environment(&temp.0), "windows", "x86_64").unwrap_err();
assert_eq!(error.code(), LAUNCH_SERVER_MISSING);
assert!(!error.message().contains("brew"));
assert!(error
.message()
.contains("https://download.eclipse.org/jdtls/milestones/"));
assert!(error
.message()
.contains("plugins/org.eclipse.equinox.launcher_*.jar"));
assert!(error.message().contains("config_win/config.ini"));
assert!(error.message().contains("Language server folder"));
assert!(error.message().contains("JDK for JDT LS"));
}

#[test]
fn nothing_found_is_one_error_that_names_no_path() {
let temp = TempDir::new();
let error = locate(None, &environment(&temp.0), "windows", "x86_64").unwrap_err();
assert_eq!(error.code(), LAUNCH_SERVER_MISSING);
assert_eq!(error.message(), SERVER_MISSING_MESSAGE);
assert_eq!(
error.message(),
server_setup_message(SERVER_MISSING_MESSAGE, "windows", "x86_64")
);
assert!(!error.message().contains(temp.0.to_string_lossy().as_ref()));
let error = locate(None, &environment(&temp.0), "plan9", "x86_64").unwrap_err();
assert_eq!(error.code(), LAUNCH_SERVER_MISSING);
Expand Down
21 changes: 10 additions & 11 deletions backend/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,19 @@ pub const LAUNCH_ERROR_CODES: &[&str] = &[
pub const SERVER_RUNTIME_TOOL: &str = "jdtls-runtime";
/// Oldest Java feature release JDT LS runs on.
pub const MINIMUM_SERVER_JAVA_VERSION: u64 = 21;
/// Message of [`LAUNCH_RUNTIME_TOO_OLD`] for [`MINIMUM_SERVER_JAVA_VERSION`].
///
/// TODO(host-api: API-37 i18n): user-facing text must go through the host's
/// i18n table.
pub const RUNTIME_TOO_OLD_MESSAGE: &str = "The Eclipse JDT Language Server needs Java 21 or \
later, but the JDK chosen for it is older. Choose a JDK 21 or later for \"JDK for JDT LS\" in \
Settings > Build, Execution, Deployment > Toolchains.";

/// Message of [`LAUNCH_RUNTIME_TOO_OLD`] for the Java feature release
/// `minimum_java_version` the selected JDT LS installation requires.
/// TODO(host-api: API-37 i18n): localize backend messages when the host supports it.
pub fn runtime_too_old_message(minimum_java_version: u64) -> String {
let minimum = minimum_java_version.max(MINIMUM_SERVER_JAVA_VERSION);
let download = jdtls::JDK_DOWNLOAD_URL;
format!(
"The Eclipse JDT Language Server needs Java {minimum} or later, but the JDK chosen for \
it is older. Choose a JDK {minimum} or later for \"JDK for JDT LS\" in Settings > Build, \
Execution, Deployment > Toolchains."
Execution, Deployment > Toolchains. If none is installed, download a JDK for your operating \
system and architecture from {download}, verify its published checksum, and extract it into \
a user-owned folder. Select that JDK for JDT LS only; keep the project's \"JDK\" selection \
and global PATH unchanged. Restart the Java language server after configuring."
)
}

Expand Down Expand Up @@ -725,14 +722,16 @@ pub fn resolve_launch_function(
) -> Result<Value, FunctionError> {
context.check_cancelled()?;
let request = parse_launch_params(&params)?;
check_server_runtime(&request)?;
let server = jdtls::locate_for_runtime(
request.server_home.as_deref(),
&SearchEnvironment::current(),
&request.os,
&request.architecture,
server_runtime_feature(&request),
)?;
// Check the selected server's actual requirement before touching package
// resources or collecting project settings (e.g. report 25, not the floor 21).
check_server_runtime_for(&request, server.minimum_java_version)?;
let package_root = package_root()?;
let diagnostics = context.diagnostics();
let operation = context.function_id().to_owned();
Expand Down Expand Up @@ -1227,7 +1226,7 @@ mod tests {
)
.unwrap_err();
assert_eq!(error.code(), LAUNCH_RUNTIME_TOO_OLD);
assert_eq!(error.message(), RUNTIME_TOO_OLD_MESSAGE);
assert_eq!(error.message(), runtime_too_old_message(21));
}
}
assert!(fs::read_dir(fixture.root.join("state"))
Expand Down
Loading
Loading