Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

feature: Try and resolve java home if it's a symlink #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/__tests__/getJavaHome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
8 changes: 8 additions & 0 deletions src/getJavaHome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Comment on lines +42 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more readable? I find flat ifs and early return more readable than nesting it

  const cleanJavaHome = configuredJavaHome?.trim();

  if (cleanJavaHome == null) return TE.left({});

  if (cleanJavaHome.length === 0) return TE.left({});

  if (fs.existsSync(cleanJavaHome)) {
    const stat = fs.lstatSync(cleanJavaHome);
    const realpath = fs.realpathSync(cleanJavaHome);
    if (stat.isSymbolicLink() && realpath.endsWith(`bin${path.sep}java`)) {
      const javaHome = path.dirname(path.dirname(realpath));
      return TE.right(javaHome);
    }
  }
  
  return TE.right(cleanJavaHome);

return TE.right(configuredJavaHome);
} else {
return TE.left({});
Expand Down