From 3f88cf10fc5f4a6339ab456936a521d7d439c274 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Mon, 8 Aug 2022 17:54:04 +0200 Subject: [PATCH] feature: Try and resolve java home if it's a symlink Resolves https://github.com/scalameta/metals-vscode/issues/1094 --- src/__tests__/getJavaHome.test.ts | 8 ++++++++ src/getJavaHome.ts | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/__tests__/getJavaHome.test.ts b/src/__tests__/getJavaHome.test.ts index 42881d5..30c89b2 100644 --- a/src/__tests__/getJavaHome.test.ts +++ b/src/__tests__/getJavaHome.test.ts @@ -20,6 +20,14 @@ describe("getJavaHome", () => { expect(javaHome).toBe(javaHomeConfig); }); + it("infers-from-symlink", async () => { + if (process.platform == "linux") { + const javaHomeConfig = "/usr/bin/java"; + const javaHome = await getJavaHome(javaHomeConfig); + expect(javaHome).not.toBe(javaHomeConfig); + } + }); + it("reads from JAVA_HOME", async () => { const JAVA_HOME = "/path/to/java"; process.env = { ...originalEnv, JAVA_HOME }; diff --git a/src/getJavaHome.ts b/src/getJavaHome.ts index 300a4bf..2afc25a 100644 --- a/src/getJavaHome.ts +++ b/src/getJavaHome.ts @@ -39,6 +39,14 @@ function fromConfig( typeof configuredJavaHome === "string" && configuredJavaHome.trim() !== "" ) { + if (fs.existsSync(configuredJavaHome)) { + const stat = fs.lstatSync(configuredJavaHome); + const realpath = fs.realpathSync(configuredJavaHome); + if (stat.isSymbolicLink() && realpath.endsWith(`bin${path.sep}java`)) { + const javaHome = path.dirname(path.dirname(realpath)); + return TE.right(javaHome); + } + } return TE.right(configuredJavaHome); } else { return TE.left({});