Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow per-target java runtime selection for scala_junit_tests #1373

Merged
merged 5 commits into from
Mar 29, 2022

Conversation

wiwa
Copy link
Contributor

@wiwa wiwa commented Mar 24, 2022

Description

Add an attr (runtime_jdk) to scala_junit_tests to specify the version of Java to run with via JavaRuntimeInfo.
Adapt java_bin() and phase_default_info to pick up the runtime and add its runfiles.

Motivation

This change allows users to test their code on different Java versions before having to adapt their code to compile with those versions.
Our specific use case is to test and run code on the JDK it will be deployed onto, supporting a smoother migration from older versions.

@google-cla
Copy link

google-cla bot commented Mar 24, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

@@ -166,8 +166,20 @@ def compile_java(ctx, source_jars, source_files, output, extra_javac_opts, provi
def runfiles_root(ctx):
return "${TEST_SRCDIR}/%s" % ctx.workspace_name

def specified_java_runtime(ctx, default_runtime):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def specified_java_runtime(ctx, default_runtime):
def specified_java_runtime(ctx, default_runtime=None):

Copy link
Contributor

Choose a reason for hiding this comment

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

so caller don't need to call with None

also would be good make sure the return value isn't None.

Copy link
Contributor

@wisechengyi wisechengyi left a comment

Choose a reason for hiding this comment

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

Thanks. Looks reasonable.

Is this already 4 & 5 compatible, or is it going to break one? At the moment we probably need a compatible solution.

@@ -166,8 +166,20 @@ def compile_java(ctx, source_jars, source_files, output, extra_javac_opts, provi
def runfiles_root(ctx):
return "${TEST_SRCDIR}/%s" % ctx.workspace_name

def specified_java_runtime(ctx, default_runtime):
Copy link
Contributor

Choose a reason for hiding this comment

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

so caller don't need to call with None

also would be good make sure the return value isn't None.

@wisechengyi
Copy link
Contributor

Also we should probably add this new field to https://github.com/bazelbuild/rules_scala/blob/master/docs/scala_test.md when this is functionality good.

@wiwa wiwa changed the title [WIP] Allow per-target java runtime selection for scala_junit_tests Allow per-target java runtime selection for scala_junit_tests Mar 25, 2022
@ShaneDelmore
Copy link
Contributor

Thanks. Looks reasonable.

Is this already 4 & 5 compatible, or is it going to break one? At the moment we probably need a compatible solution.

Our original version was Bazel 5 only, but we reworked it to be 4 and 5 compat.

@liucijus
Copy link
Collaborator

LGTM! @wiwa please fix linter errors by running ./lint.sh.

Has this improvement been tested on any other codebase than rules scala itself?

@wiwa
Copy link
Contributor Author

wiwa commented Mar 28, 2022

Has this improvement been tested on any other codebase than rules scala itself?

Yes, it works for our use case!

@wiwa
Copy link
Contributor Author

wiwa commented Mar 28, 2022

Also we should probably add this new field to https://github.com/bazelbuild/rules_scala/blob/master/docs/scala_test.md when this is functionality good.

We only add the runtime_jdk attr to scala_junit_test, which seems to not propagate to scala_test. We can make a followup which addresses all test rules?

@liucijus
Copy link
Collaborator

Also we should probably add this new field to https://github.com/bazelbuild/rules_scala/blob/master/docs/scala_test.md when this is functionality good.

We only add the runtime_jdk attr to scala_junit_test, which seems to not propagate to scala_test. We can make a followup which addresses all test rules?

Yes, it's easier to review smaller PRs

Copy link
Collaborator

@liucijus liucijus left a comment

Choose a reason for hiding this comment

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

Thanks, @wiwa!

@simuons
Copy link
Collaborator

simuons commented Mar 29, 2022

Hi @wiwa , probably I'm too late but gonna ask anyway. Have you considered adding runtime_jdk on a toolchain and use that for all test targets. Do you really need this to be controlled per target basis?

@wisechengyi
Copy link
Contributor

Hi @wiwa , probably I'm too late but gonna ask anyway. Have you considered adding runtime_jdk on a toolchain and use that for all test targets. Do you really need this to be controlled per target basis?

Hi @simuons, this is still a good question. There's a few reasons. Here is a couple of major ones:

  1. Twitter has thousands of targets to migrate, some are 11 runtime compatible and some are not, so we would like to have runtime selection on target level in order to migrate the tests (PR for scala_binary to come soon) incrementally, so regression can be prevented while we move the JDK 11 migration forward.
  2. Each team has their own cadence of development, testing, and deployment, so flipping the switch for everyone at the same time would likely be too risky.

Copy link
Collaborator

@simuons simuons left a comment

Choose a reason for hiding this comment

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

@wiwa thanks for PR
@wisechengyi thanks for explanation.

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.

5 participants