Skip to content

Commit

Permalink
Merge pull request #1484 from tgodzik/remove-locate
Browse files Browse the repository at this point in the history
bugfix: Remove locate-java-home
  • Loading branch information
tgodzik authored Apr 21, 2024
2 parents b2dbfa5 + 3ebd387 commit 92ddb6a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 224 deletions.
1 change: 0 additions & 1 deletion packages/metals-languageclient/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"typescript": "^3.7.5"
},
"dependencies": {
"@viperproject/locate-java-home": "^1.1.5",
"fp-ts": "^2.4.1",
"mkdirp": "^2.1.1",
"node-fetch": "^2.6.0",
Expand Down
114 changes: 0 additions & 114 deletions packages/metals-languageclient/src/__tests__/getJavaHome.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IJavaHomeInfo } from "@viperproject/locate-java-home/js/es5/lib/interfaces";
import path from "path";

const exampleJavaVersionString = `openjdk "17.0.1" 2021-10-19
Expand Down Expand Up @@ -27,121 +26,8 @@ describe("getJavaHome", () => {
const javaHome = await require("../getJavaHome").getJavaHome("17");
expect(javaHome).toBe(JAVA_HOME);
});

it("checks PATH variable", async () => {
const javaPaths = [
{
binPath: path.join("/", "test", "usr", "bin", "java"),
realPath: path.join(java11Jdk.path, "bin", "java"),
},
];
const PATH = path.join("/", "test", "usr", "bin");
mockLocateJavaHome([java11Jdk, java17Jdk]);
mockFs(javaPaths);
process.env = { PATH };
const javaHome = await require("../getJavaHome").getJavaHome("11");
expect(javaHome).toBe(java11Jdk.path);
});

// NOTE(gabro): we don't care about testing locate-java-home since it's an external dependency
// and we assume it works as expected. However, we want to test how we select a specific version
// when multiple installed Java are available.

it("falls back to installed Java", async () => {
mockLocateJavaHome([java11Jdk]);
const javaHome = await require("../getJavaHome").getJavaHome("11");
expect(javaHome).toBe(java11Jdk.path);
});

it("prefers installed JDK over JRE", async () => {
mockLocateJavaHome([java11Jre, java11Jdk]);
const javaHome = await require("../getJavaHome").getJavaHome("11");
expect(javaHome).toBe(java11Jdk.path);
});

it("prefers the most recent installed JDK 11", async () => {
mockLocateJavaHome([java11Jdk]);
const javaHome = await require("../getJavaHome").getJavaHome("11");
expect(javaHome).toBe(java11Jdk.path);
});

it("prefers the most recent installed JDK 17", async () => {
mockLocateJavaHome([java17Jdk, java11Jdk]);
const javaHome = await require("../getJavaHome").getJavaHome("17");
expect(javaHome).toBe(java17Jdk.path);
});

it("prefers the most recent security patch", async () => {
mockLocateJavaHome([java11Jdk, java11JdkNewPatch]);
const javaHome = await require("../getJavaHome").getJavaHome("11");
expect(javaHome).toBe(java11JdkNewPatch.path);
});
});

const java11Jdk = {
path: path.join("/", "path", "to", "java11jdk"),
version: "1.11.0",
security: 1,
isJDK: true,
};

const java17Jdk = {
path: path.join("/", "path", "to", "java17jdk"),
version: "1.17.0",
security: 1,
isJDK: true,
};

const java11Jre = {
path: path.join("/", "path", "to", "java11jdk"),
version: "1.11.0",
security: 1,
isJDK: false,
};

const java11JdkNewPatch = {
path: path.join("/", "path", "to", "java11jdk", "high", "security"),
version: "1.11.0",
security: 192,
isJDK: true,
};

function mockLocateJavaHome(
javas: { path: string; version: string; security: number; isJDK: boolean }[]
): void {
jest.resetModules();
jest
.spyOn(require("@viperproject/locate-java-home"), "default")
.mockImplementation((_options: unknown, cb: unknown) => {
(cb as (err: Error | null, found?: IJavaHomeInfo[]) => void)(
null,
javas.map((j) => ({
...j,
is64Bit: true,
executables: {
java: path.join(j.path, "bin", "java"),
javac: path.join(j.path, "bin", "javac"),
javap: path.join(j.path, "bin", "javap"),
},
}))
);
});
}

function mockFs(javaLinks: { binPath: String; realPath: String }[]): void {
mockExistsFs(javaLinks);
jest
.spyOn(require("fs"), "realpathSync")
.mockImplementation((path: unknown) => {
const value = javaLinks.find((o) => o.binPath == path);
if (value) {
return value.realPath;
} else {
return path;
}
});
}

function mockExistsFs(javaLinks: { binPath: String }[]): void {
jest
.spyOn(require("fs"), "existsSync")
Expand Down
84 changes: 1 addition & 83 deletions packages/metals-languageclient/src/getJavaHome.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import { TaskEither, chain } from "fp-ts/lib/TaskEither";
import * as TE from "fp-ts/lib/TaskEither";
import * as E from "fp-ts/lib/Either";
import { pipe } from "fp-ts/lib/function";
import _locateJavaHome from "@viperproject/locate-java-home";
import * as semver from "semver";
import {
ILocateJavaHomeOptions,
IJavaHomeInfo,
} from "@viperproject/locate-java-home/js/es5/lib/interfaces";
import { toPromise } from "./util";
import fs from "fs";
import path from "path";
import { spawn } from "promisify-child-process";
import { OutputChannel } from "./interfaces/OutputChannel";
Expand All @@ -31,7 +19,7 @@ export async function getJavaHome(
outputChannel: OutputChannel
): Promise<string | undefined> {
const fromEnvValue = await fromEnv(javaVersion, outputChannel);
return fromEnvValue ? fromEnvValue : await locate(javaVersion);
return fromEnvValue;
}

const versionRegex = /\"\d\d/;
Expand Down Expand Up @@ -80,73 +68,3 @@ export async function fromEnv(

return undefined;
}

function locate(javaVersion: JavaVersion): Promise<undefined | string> {
return toPromise(
pipe(
locateJavaHome({ version: `>=${javaVersion}` }),
chain((javaHomes) => {
if (!javaHomes || javaHomes.length === 0) {
return TE.right(undefined);
} else {
const jdkHomes = javaHomes.filter((j) => j.isJDK);
const fromBinPath = matchesBinFromPath(jdkHomes);
const jdkHome = fromBinPath ? fromBinPath : latestJdk(jdkHomes);
if (jdkHome) {
return TE.right(jdkHome.path);
} else {
return TE.right(javaHomes[0].path);
}
}
})
)
);
}

function matchesBinFromPath(
jdkHomes: IJavaHomeInfo[]
): IJavaHomeInfo | undefined {
const value = process.env["PATH"];
if (value && jdkHomes.length > 0) {
const result = value
.split(path.delimiter)
.map((p) => path.join(p, "java"))
.filter((p) => fs.existsSync(p));

if (result.length > 0) {
const realpath = fs.realpathSync(result[0]);
const matched = jdkHomes.find((home) => {
const javaBin = path.join(home.path, "bin", "java");
return javaBin == realpath;
});
return matched;
} else {
return undefined;
}
} else {
return undefined;
}
}

function latestJdk(jdkHomes: IJavaHomeInfo[]): IJavaHomeInfo | undefined {
if (jdkHomes.length > 0) {
return jdkHomes.sort((a, b) => {
const byVersion = -semver.compare(a.version, b.version);
if (byVersion === 0) return b.security - a.security;
else return byVersion;
})[0];
} else {
return undefined;
}
}

function locateJavaHome(
opts: ILocateJavaHomeOptions
): TaskEither<Error, IJavaHomeInfo[] | undefined> {
return () =>
new Promise((resolve) =>
_locateJavaHome(opts, (err, res) =>
err != null ? resolve(E.left(err)) : resolve(E.right(res))
)
);
}
15 changes: 1 addition & 14 deletions packages/metals-languageclient/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,6 @@
dependencies:
"@types/yargs-parser" "*"

"@viperproject/locate-java-home@^1.1.5":
version "1.1.11"
resolved "https://registry.yarnpkg.com/@viperproject/locate-java-home/-/locate-java-home-1.1.11.tgz#9ae7c281ab005bcbe8b730fee495def0440385f3"
integrity sha512-cnwxSsB1QB4eeerRd8XsBEDh/BHD1726g/UqA3RAvPBlbZVqzm75jyq+0p9hsVQdtHxUdLRzpjSktck8YEuaAQ==
dependencies:
async "^3.2.3"
semver "^7.3.5"

abab@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.3.tgz#623e2075e02eb2d3f2475e49f99c91846467907a"
Expand Down Expand Up @@ -805,11 +797,6 @@ astral-regex@^1.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==

async@^3.2.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/async/-/async-3.2.3.tgz#ac53dafd3f4720ee9e8a160628f18ea91df196c9"
integrity sha512-spZRyzKL5l5BZQrr/6m/SqFdBN0q3OCI0f9rjfBzCMBIP4p75P620rR3gTmaksNOhmzgdxcaxdNfMy6anrbM0g==

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
Expand Down Expand Up @@ -3163,7 +3150,7 @@ semver@6.x, semver@^6.0.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.1.1, semver@^7.3.5:
semver@^7.1.1:
version "7.5.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.2.tgz#5b851e66d1be07c1cdaf37dfc856f543325a2beb"
integrity sha512-SoftuTROv/cRjCze/scjGyiDtcUyxw1rgYQSZY7XTmtR5hX+dm76iDbTH8TkLPHCQmlbQVSSbNZCPM2hb0knnQ==
Expand Down
31 changes: 19 additions & 12 deletions packages/metals-vscode/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@
"@typescript-eslint/types" "5.56.0"
eslint-visitor-keys "^3.3.0"

"@viperproject/locate-java-home@^1.1.5":
version "1.1.7"
resolved "https://registry.yarnpkg.com/@viperproject/locate-java-home/-/locate-java-home-1.1.7.tgz#9544f0db8ad57e4a3e8790c2000e8397dab8932e"
integrity sha512-wboCBiSBxkPNqe03sRiy6eXMu0RDQfxOVxRcxgA8e5GRcYVhhCjr4eiLYlBelObYmthUc0J9SjaabmioMbPN4Q==
"@viperproject/locate-java-home@^1.1.15":
version "1.1.15"
resolved "https://registry.yarnpkg.com/@viperproject/locate-java-home/-/locate-java-home-1.1.15.tgz#7ba323dff906c179b37a56015a4660a83445f4e7"
integrity sha512-ZTyIulVHqCOwe7wcZ7h6qkmziBB5SgSlDv1ssbvf5s1oHmH9OVJXtBNARnbXIr2gvH5L4yCr2EHPxb21Uyg67A==
dependencies:
async "^3.2.2"
semver "^7.3.5"
async "^3.2.5"
semver "^7.5.4"

"@vscode/test-electron@^2.3.0":
version "2.3.0"
Expand Down Expand Up @@ -435,10 +435,10 @@ assertion-error@^1.1.0:
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.1.0.tgz#e60b6b0e8f301bd97e5375215bda406c85118c0b"
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==

async@^3.2.2:
version "3.2.3"
resolved "https://registry.yarnpkg.com/async/-/async-3.2.3.tgz#ac53dafd3f4720ee9e8a160628f18ea91df196c9"
integrity sha512-spZRyzKL5l5BZQrr/6m/SqFdBN0q3OCI0f9rjfBzCMBIP4p75P620rR3gTmaksNOhmzgdxcaxdNfMy6anrbM0g==
async@^3.2.5:
version "3.2.5"
resolved "https://registry.yarnpkg.com/async/-/async-3.2.5.tgz#ebd52a8fdaf7a2289a24df399f8d8485c8a46b66"
integrity sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==

azure-devops-node-api@^11.0.1:
version "11.1.0"
Expand Down Expand Up @@ -1549,7 +1549,7 @@ merge2@^1.3.0, merge2@^1.4.1:
"metals-languageclient@file:../metals-languageclient":
version "0.5.18"
dependencies:
"@viperproject/locate-java-home" "^1.1.5"
"@viperproject/locate-java-home" "^1.1.15"
fp-ts "^2.4.1"
mkdirp "^2.1.1"
node-fetch "^2.6.0"
Expand Down Expand Up @@ -2081,13 +2081,20 @@ semver@^5.1.0, semver@^5.4.1:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==

semver@^7.1.1, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8, semver@^7.5.2:
semver@^7.1.1, semver@^7.3.7, semver@^7.3.8, semver@^7.5.2:
version "7.5.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.2.tgz#5b851e66d1be07c1cdaf37dfc856f543325a2beb"
integrity sha512-SoftuTROv/cRjCze/scjGyiDtcUyxw1rgYQSZY7XTmtR5hX+dm76iDbTH8TkLPHCQmlbQVSSbNZCPM2hb0knnQ==
dependencies:
lru-cache "^6.0.0"

semver@^7.5.4:
version "7.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.0.tgz#1a46a4db4bffcccd97b743b5005c8325f23d4e2d"
integrity sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==
dependencies:
lru-cache "^6.0.0"

serialize-javascript@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-6.0.0.tgz#efae5d88f45d7924141da8b5c3a7a7e663fefeb8"
Expand Down

0 comments on commit 92ddb6a

Please sign in to comment.