Skip to content

Add make variables for runfiles location of $(JAVABASE) to support --no_legacy_external_runfiles.#272

Closed
thirtyseven wants to merge 6 commits intobazelbuild:masterfrom
thirtyseven:patch-2
Closed

Add make variables for runfiles location of $(JAVABASE) to support --no_legacy_external_runfiles.#272
thirtyseven wants to merge 6 commits intobazelbuild:masterfrom
thirtyseven:patch-2

Conversation

@thirtyseven
Copy link
Contributor

@thirtyseven thirtyseven commented Feb 6, 2025

Add make variables for runfiles locations of $(JAVA) and $(JAVABASE). These are needed to access these paths when passing them to test rules via env when using --no_legacy_external_runfiles. The original make variables still work in exec contexts such as genrules.

Add make variables for runfiles locations of $(JAVA) and $(JAVABASE). These are needed to access these paths when passing them to test rules via `env`.
@thirtyseven thirtyseven requested review from a team and comius as code owners February 6, 2025 16:51
@fmeum
Copy link
Contributor

fmeum commented Feb 6, 2025

@meteorcloudy We might want to cherry-pick this into 8.1.0 as it makes it easier to adopt the --nolegacy_external_runfiles flip.

We could then also document it on the Make variables page.

@meteorcloudy
Copy link
Member

@fmeum Do you mean bump the rules_java version in MODULE.tools to include this change? End users can also just depend on a newer rules_java to get this?

@fmeum
Copy link
Contributor

fmeum commented Feb 6, 2025

@fmeum Do you mean bump the rules_java version in MODULE.tools to include this change? End users can also just depend on a newer rules_java to get this?

That is correct, but if we don't bump the minimum requirement, we can't document these Make variables on the versioned page for 8.1.0.

@meteorcloudy
Copy link
Member

OK, let's try to get this reviewed first, then let's see if we can make it for Bazel 8.1

@hvadehra Can you take a look?

Copy link
Member

@hvadehra hvadehra left a comment

Choose a reason for hiding this comment

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

Please also add a test for this. Easiest might be to use this somehow in https://github.com/bazelbuild/rules_java/tree/master/test/repo

platform_common.TemplateVariableInfo({
"JAVA": str(toolchain.java_executable_exec_path),
"JAVABASE": str(toolchain.java_home),
"JAVA_RUNFILES": str(toolchain.java_executable_runfiles_path),
Copy link
Member

Choose a reason for hiding this comment

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

I think we should instead be adding these to

platform_common.TemplateVariableInfo({
and then just forward that provider like we do below in this file:
template_variable_info = runtime[platform_common.TemplateVariableInfo]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to make this change, I think I would have to make java_runtime_alias and java_host_runtime_alias identical by adding a dependency on //toolchains:current_java_runtime to java_runtime_alias. I can do this, but wanted to make sure it's your intent first.

Copy link
Member

Choose a reason for hiding this comment

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

My bad, please ignore the bit about just forwarding the provider. But I do think we need to make the same change also in rules_java/java/common/rules/java_runtime.bzl

Copy link
Contributor Author

@thirtyseven thirtyseven Feb 7, 2025

Choose a reason for hiding this comment

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

I have made the requested changes, however the test I added is failing due to toolchain.java_executable_runfiles_path appearing not to be a valid path in the context of the test. It's returning a path like:

/private/var/tmp/_bazel_thirtyseven/43339e4e51eaab2f90ecf6c85b4023b4/sandbox/darwin-sandbox/49/execroot/_main/../rules_java++toolchains+local_jdk/bin/java

which does not exist when running the test.

I'm not sure if this is due to something missing from my test or if I'm misusing this field, any help would be appreciated.

Copy link
Contributor Author

@thirtyseven thirtyseven Feb 8, 2025

Choose a reason for hiding this comment

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

I have removed JAVA_RUNFILES from this PR for now. It seemed unlikely that it was going to work as I intended (making a Java executable easily available to test rules).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Realized I could do something like:

"JAVA_RUNFILES": toolchain.java_home_runfiles_path + "/bin/java",

it's a little ugly but works. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Does that work? I would think the issue is that JAVA_RUNFILES is a special env var1 so if we'll want to use a different name.

Footnotes

  1. https://github.com/bazelbuild/bazel/blob/d796714abba0a5531edf795c53a3985300577303/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java#L82

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 what a better name would be, I'm ok with leaving it out letting the user locate bin/java.

@thirtyseven thirtyseven changed the title Add make variables for runfiles locations of $(JAVA) and $(JAVABASE) to support --no_legacy_external_runfiles. Add make variables for runfiles location of $(JAVABASE) to support --no_legacy_external_runfiles. Feb 8, 2025
@hvadehra
Copy link
Member

Please fix the buidifier failure, the sh rules have been moved out of Bazel, so we'll need to load them from @rules_sh

Load rules_shell

Load rules_shell in WORKSPACE mode

Buildify WORKSPACE

Fix WORKSPACE
hvadehra
hvadehra previously approved these changes Feb 11, 2025
platform_common.TemplateVariableInfo({
"JAVA": java_binary_exec_path,
"JAVABASE": java_home,
"JAVABASE_RUNFILES": java_home_runfiles_path,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not adding an equivalent for the java binary makes this difficult to use cross-platform (think Windows).

I don't think the deprecated env var JAVA_RUNFILES would necessarily conflict with a make var. It's something we could likely just get rid of at this point anyway.

But to avoid confusion, how about calling this JAVA_ROOTPATH or JAVABASE_ROOTPATH to match $(rootpath ...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I've tried to address this.

platform_common.TemplateVariableInfo({
"JAVA": java_binary_exec_path,
"JAVABASE": java_home,
"JAVA_ROOTPATH": paths.join(java_home_runfiles_path, "bin/java"),
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work on Windows. Could you use java_binary_runfiles_path instead? If that somehow isn't the right path, you could also reuse the basename of the execpath.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, java_binary_runfiles_path does actually work, the earlier issue I had must have been due to the JAVA_RUNFILES name conflict.

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.

4 participants