Skip to content

Conversation

@MSP-Greg
Copy link
Contributor

@MSP-Greg MSP-Greg commented Apr 11, 2025

JRuby-head is using JDK 21. This is not the default on GHA. setup-ruby added code to change JAVA_HOME, assuming it would trigger the correct JDK being used.

Puma tests against jruby-head, and it was failing when rake compile ran. After reviewing the code, javac is being called without a path.

Added code to check if File.join(ENV["JAVA_HOME"], "bin", "javac") exists. If it does, use the full path, otherwise fallback to current behavior.

I changed the Gemfile in Puma to use the PR branch, and the job correctly compiles.

@MSP-Greg
Copy link
Contributor Author

@headius

Sorry for the ping. You and Pavel have the two most recent commits here. I'm somewhat java challenged. Does this look ok?

JFYI, started with Puma failing on jruby-head...

@MSP-Greg MSP-Greg force-pushed the 00-java-home branch 2 times, most recently from 7ba0151 to da7dea5 Compare April 12, 2025 01:21
EOF
warn_once(not_jruby_compile_msg) unless defined?(JRUBY_VERSION)

javac_path = File.exist?(t = File.join(ENV["JAVA_HOME"], "bin", "javac")) ?
Copy link
Member

Choose a reason for hiding this comment

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

It seems that ENV["JAVA_HOME"] may not exist:

jruby_home = ENV['JRUBY_HOME']
if jruby_home

Can we reuse the checked ENV["JAVA_HOME"] value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if ENV["JAVA_HOME"] is used anywhere?

Can we use:

ENV.fetch("JAVA_HOME", "/")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just pushed a change to using ENV.fetch. Used "/" for the default so it's an absolute path...

Copy link
Member

Choose a reason for hiding this comment

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

Oh, sorry. It's JRUBY_HOME not JAVE_HOME...

Comment on lines 111 to 112
javac_path = File.exist?(t = File.join(ENV.fetch("JAVA_HOME", "/"), "bin", "javac")) ?
t : "javac"
Copy link
Member

Choose a reason for hiding this comment

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

Using / as the default JAVA_HOME will not work when javac exists in both of /usr/bin/javac and /usr/local/bin/javac, and PATH is /usr/local/bin:/usr/bin:/bin.

We may need to check ENV["JAVA_HOME"] explicitly:

Suggested change
javac_path = File.exist?(t = File.join(ENV.fetch("JAVA_HOME", "/"), "bin", "javac")) ?
t : "javac"
java_home = ENV["JAVA_HOME"]
if java_home
javac_path = File.join(java_home, "bin", "javac")
javac_path = nil unless File.exist?(javac_path)
end
javac_path ||= "javac"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kou

Thanks for the review. I'm not sure about your example, it seems a rather extreme and odd edge case. Also, I'm not familiar with all of the *nix distributions.

"/" was a bad choice for default, as File.join('/', 'bin', 'javac')" and File.join('', 'bin', 'javac') both equal "/bin/javac".

The previous code worked correctly when used with GHA. But your changes also work.

Updated with your suggested code.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, sorry. I considered the following case:

  • ENV["PATH"] is "/usr/local/bin:/usr/bin
  • ENV["JAVA_HOME"] isn't set
  • /usr/local/bin/javac exists
  • /usr/bin/javac exists

In the case, javac uses /usr/local/bin/javac but File.join(ENV.fetch("JAVA_HOME", "/"), "bin", "javac") uses /usr/bin/javac not /usr/local/bin/javac.

Copy link
Contributor Author

@MSP-Greg MSP-Greg Apr 13, 2025

Choose a reason for hiding this comment

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

Sorry, I need to try this.

If ENV["JAVA_HOME"] isn't set, then ENV.fetch("JAVA_HOME", "/"), "bin", "javac") yields /bin/javac, I thought File.exist?("/bin/javac") would be false, and then javac would be used, and the file would be resolved by PATH?

Again, I need to try it.

Regardless, thanks for your help and the new release.

@MSP-Greg
Copy link
Contributor Author

Some background on this issue.

At present, GitHub Actions (GHA) runners provide at least three Java versions (11, 17, 21). Some provide version 8. Version 17 is the default. ENV variables exist for determining the location of each version.

JRuby 10/head requires version 21. setup-ruby made changes for this by changing the value of ENV['JAVA_HOME'].

This PR finishes the work by using ENV['JAVA_HOME'] to determine the path for the correct version of javac to run for the compile process.

@kou
Copy link
Member

kou commented Apr 13, 2025

@headius I'll merge this and release a new version.

If you have any problem with this, please share it.

@kou kou merged commit c413d3b into rake-compiler:master Apr 13, 2025
14 checks passed
@MSP-Greg MSP-Greg deleted the 00-java-home branch April 13, 2025 03:22
@eregon
Copy link

eregon commented Dec 13, 2025

Thanks for this PR.

It works fine on UNIX but not Windows:
https://github.com/eregon/actions-shell/actions/runs/20195116809/job/57977991482#step:4:369

ENV["JAVA_HOME"]
"C:\\hostedtoolcache\\windows\\Java_Temurin-Hotspot_jdk\\21.0.9-10.0\\x64"

File.join(ENV["JAVA_HOME"], "bin", "javac")
"C:\\hostedtoolcache\\windows\\Java_Temurin-Hotspot_jdk\\21.0.9-10.0\\x64/bin/javac"

File.exist?(File.join(ENV["JAVA_HOME"], "bin", "javac"))
false

I'm unsure if it's a bug of File.join on JRuby on Windows (the code does File.join(java_home, "bin", "javac") which intuitively should work, either use only \ or convert everything to / but a mix seems quite broken).
cc @headius

@eregon
Copy link

eregon commented Dec 13, 2025

It seems File.join does have those "broken" semantics even on CRuby :/
https://github.com/eregon/actions-shell/actions/runs/20195181454/job/57978154719#step:4:798

JAVA_HOME = "C:\\hostedtoolcache\\windows\\Java_Temurin-Hotspot_jdk\\21.0.9-10.0\\x64"
File.join(JAVA_HOME, "bin", "javac")
"C:\\hostedtoolcache\\windows\\Java_Temurin-Hotspot_jdk\\21.0.9-10.0\\x64/bin/javac"

So I guess we should just hack it with

File.join(java_home.tr('\\', '/'), "bin", "javac")

then, or seeing that File.join is beyond useless here, just:

"#{java_home.tr('\\', '/')}/bin/javac"

@eregon
Copy link

eregon commented Dec 13, 2025

Oof even that doesn't work, Windows is truly cursed IMO:
https://github.com/eregon/actions-shell/actions/runs/20195353255/job/57978552583#step:4:377

"#{JAVA_HOME.tr('\\', '/')}/bin/javac"
"C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/21.0.9-10.0/x64/bin/javac"

File.exist?("#{JAVA_HOME.tr('\\', '/')}/bin/javac")
false

At this point I'll let others investigate/fix it.

@eregon
Copy link

eregon commented Dec 13, 2025

Ah, it's my mistake on Windows there is no bin/javac, it's bin/javac.exe.
Then all of them work:

 File.join(JAVA_HOME, "bin", "javac.exe")
"C:\\hostedtoolcache\\windows\\Java_Temurin-Hotspot_jdk\\21.0.9-10.0\\x64/bin/javac.exe"

File.exist?(File.join(JAVA_HOME, "bin", "javac.exe"))
true

"#{JAVA_HOME.tr('\\', '/')}/bin/javac.exe"
"C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/21.0.9-10.0/x64/bin/javac.exe"

File.exist?("#{JAVA_HOME.tr('\\', '/')}/bin/javac.exe")
true

"#{JAVA_HOME}\\bin\\javac.exe"
"C:\\hostedtoolcache\\windows\\Java_Temurin-Hotspot_jdk\\21.0.9-10.0\\x64\\bin\\javac.exe"

File.exist?("#{JAVA_HOME}\\bin\\javac.exe")
true

Sanity has returned.

@eregon
Copy link

eregon commented Dec 13, 2025

@kou so there is db38e70 but https://rubygems.org/gems/rake-compiler only has 1.3.0 not 1.3.1, so it seems you forgot to push the gem or it didn't work?
Could you push 1.3.1 (or 1.3.2 if 1.3.1 is not possible?)

That would also explain why CI failed on ruby/strscan#155

And now I found the discussion I recalled: ruby/strscan#151
I had hope @headius would look into this before we run into this again.

@eregon
Copy link

eregon commented Dec 13, 2025

@headius IMO this is messy enough that I think it would be better to add JAVA_HOME/bin to PATH in setup-ruby.
I don't see a disadvantage to do so, setup-java would do the same thing and it's likely to work poorly to have java in PATH different than JAVA_HOME/bin/java (e.g. javac uses PATH, Maven uses JAVA_HOME, a wild mix).

eregon added a commit to eregon/setup-ruby that referenced this pull request Dec 13, 2025
* See discussion in rake-compiler/rake-compiler#247
  it is messy if that's not the case and also creates inconsistencies
  based on which Java tool/executable is used.
@eregon
Copy link

eregon commented Dec 13, 2025

I went ahead and did a PR to do that so we can't run into such issues again: ruby/setup-ruby#836

eregon added a commit to ruby/setup-ruby that referenced this pull request Dec 13, 2025
* See discussion in rake-compiler/rake-compiler#247
  it is messy if that's not the case and also creates inconsistencies
  based on which Java tool/executable is used.
@kou
Copy link
Member

kou commented Dec 14, 2025

Oh, sorry. I didn't notice it.

I've published 1.3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants