Skip to content

Commit 2502fcd

Browse files
committed
Only change JAVA_HOME if JRuby fails to start
1 parent efa9afc commit 2502fcd

File tree

4 files changed

+71
-25
lines changed

4 files changed

+71
-25
lines changed

common.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import exec from "@actions/exec";
2+
13
const os = require('os')
24
const path = require('path')
35
const fs = require('fs')
@@ -404,15 +406,29 @@ export function setupPath(newPathEntries) {
404406
return msys2Type
405407
}
406408

407-
export function setupJavaHome() {
409+
export async function setupJavaHome() {
408410
core.startGroup(`Modifying JAVA_HOME for JRuby`)
409-
let arch = os.arch();
410-
if (arch == "x64" || os.platform() != "darwin") {
411-
arch = "X64"
411+
412+
console.log("attempting to run with existing JAVA_HOME")
413+
let ret = await exec.exec('ruby', ['--version']);
414+
415+
if (ret === 0) {
416+
console.log("JRuby successfully starts, using existing JAVA_HOME")
417+
} else {
418+
console.log("JRuby failed to start, try Java 21 envs")
419+
let arch = os.arch();
420+
if (arch == "x64" || os.platform() != "darwin") {
421+
arch = "X64"
422+
}
423+
let newHomeVar = `JAVA_HOME_21_${arch}`;
424+
let newHome = process.env[newHomeVar];
425+
426+
if (newHome === "undefined") {
427+
throw new Error(`JAVA_HOME is not Java 21+ needed for JRuby and \$${newHomeVar} is not defined`)
428+
}
429+
console.log(`Setting JAVA_HOME to ${newHomeVar} path ${newHome}`)
430+
core.exportVariable("JAVA_HOME", newHome)
412431
}
413-
let newHomeVar = `JAVA_HOME_21_${arch}`;
414-
let newHome = process.env[newHomeVar];
415-
console.log(`Setting JAVA_HOME to ${newHomeVar} path ${newHome}`)
416-
core.exportVariable("JAVA_HOME", newHome);
432+
417433
core.endGroup()
418434
}

dist/index.js

+43-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export async function setupRuby(options = {}) {
7979

8080
const rubyPrefix = await installer.install(platform, engine, version)
8181

82+
if (engine == "jruby") {
83+
await common.setupJavaHome();
84+
}
85+
8286
await common.measure('Print Ruby version', async () =>
8387
await exec.exec('ruby', ['--version']))
8488

ruby-builder.js

-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ export async function install(platform, engine, version) {
4949
// Set the PATH now, so the MSYS2 'tar' is in Path on Windows
5050
common.setupPath([path.join(rubyPrefix, 'bin')])
5151

52-
if (engine == "jruby") {
53-
common.setupJavaHome();
54-
}
55-
5652
if (!inToolCache) {
5753
await io.mkdirP(rubyPrefix)
5854
await downloadAndExtract(platform, engine, version, rubyPrefix)

0 commit comments

Comments
 (0)