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

Verify that Java language level isn't higher than runtime version #18340

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import com.google.auto.value.AutoValue;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.analysis.PlatformOptions;
import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode;
import com.google.devtools.build.lib.analysis.config.CoreOptions;
import com.google.devtools.build.lib.analysis.config.Fragment;
import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
import com.google.devtools.build.lib.analysis.config.RequiresOptions;
Expand Down Expand Up @@ -207,6 +209,9 @@ public JavaConfiguration(BuildOptions buildOptions) throws InvalidConfigurationE
+ " --tool_java_runtime_version). This may result in incorrect toolchain selection "
+ "(see https://github.com/bazelbuild/bazel/issues/7849).");
}

checkLanguageAndRuntimeVersion(javaOptions.javaLanguageVersion, javaOptions.javaRuntimeVersion,
buildOptions.get(CoreOptions.class).isExec);
}

private static void checkLegacyToolchainFlagIsUnset(String flag, Label label)
Expand All @@ -218,6 +223,58 @@ private static void checkLegacyToolchainFlagIsUnset(String flag, Label label)
}
}

private static void checkLanguageAndRuntimeVersion(@Nullable String rawLanguageVersion,
@Nullable String rawRuntimeVersion, boolean isExec) throws InvalidConfigurationException {
int languageVersion;
if (Strings.isNullOrEmpty(rawLanguageVersion)) {
return;
}
try {
languageVersion = Integer.parseUnsignedInt(rawLanguageVersion);
} catch (NumberFormatException e) {
// This is unexpected, but for the sake of compatibility with unusual toolchains, we should
// not fail the build.
return;
}

int runtimeVersion;
if (Strings.isNullOrEmpty(rawRuntimeVersion)) {
return;
}
int lastUnderscore = rawRuntimeVersion.lastIndexOf('_');
String rawRuntimeVersionLastPart;
if (lastUnderscore == -1) {
rawRuntimeVersionLastPart = rawRuntimeVersion;
} else {
rawRuntimeVersionLastPart = rawRuntimeVersion.substring(lastUnderscore + 1);
}
try {
runtimeVersion = Integer.parseUnsignedInt(rawRuntimeVersionLastPart);
} catch (NumberFormatException ignored) {
// Could be e.g. the "jdk" part of "local_jdk" without a version number, which we can't
// check at this point.
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems slightly dicey to figure out the Java features version from the string name of the runtime version. That convention doesn't hold for all of the Java runtimes we define at google, for example.

Is there somewhere we could do this validation when we have the JavaRuntimeInfo, in order to check the java_runtime.version instead of the value of --java_runtime_version=?

return;
}

if (languageVersion > runtimeVersion) {
if (isExec) {
throw new InvalidConfigurationException(
String.format(
"--tool_java_language_version=%s is incompatible with current Java runtime '%s':"
+ " Both --tool_java_runtime_version and the 'java_runtime' attribute of the"
+ " Java toolchain have to be set to versions at least as high as the language"
+ " version.",
rawLanguageVersion, rawRuntimeVersion));
} else {
throw new InvalidConfigurationException(
String.format(
"--java_language_version=%s is incompatible with --java_runtime_version=%s: The"
+ " runtime version has to be at least as high as the language version.",
rawLanguageVersion, rawRuntimeVersion));
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we go with the logic in this class, it would be nice to have some unit test coverage for the parsing logic in JavaConfigurationTest


@Override
// TODO(bazel-team): this is the command-line passed options, we should remove from Starlark
// probably.
Expand Down
89 changes: 89 additions & 0 deletions src/test/shell/bazel/bazel_with_jdk_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,93 @@ function test_bazel_compiles_with_localjdk() {
expect_not_log "exec external/remotejdk11_linux/bin/java"
}

function test_java_language_greater_than_runtime_version() {
mkdir -p pkg
cat >pkg/BUILD <<EOF
java_binary(
name = "Main",
srcs = ["Main.java"],
main_class = "com.example.Main",
)
EOF

cat >pkg/Main.java <<EOF
package com.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
EOF

bazel build //pkg:Main \
--java_language_version=17 \
--java_runtime_version=remotejdk_11 \
&>"${TEST_log}" && fail "Expected build to fail"

expect_log "--java_language_version=17 is incompatible with --java_runtime_version=remotejdk_11: The runtime version has to be at least as high as the language version."
}

function test_tool_java_language_greater_than_runtime_version() {
mkdir -p pkg
cat >pkg/BUILD <<'EOF'
java_binary(
name = "Main",
srcs = ["Main.java"],
main_class = "com.example.Main",
)

genrule(
name = "gen",
outs = ["gen.txt"],
tools = [":Main"],
cmd = "$(location :Main) > $@",
)
EOF

cat >pkg/Main.java <<'EOF'
package com.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
EOF

bazel build //pkg:gen \
--tool_java_language_version=17 \
--tool_java_runtime_version=remotejdk_11 \
&>"${TEST_log}" && fail "Expected build to fail"

expect_log "--tool_java_language_version=17 is incompatible with current Java runtime 'remotejdk_11': Both --tool_java_runtime_version and the 'java_runtime' attribute of the Java toolchain have to be set to versions at least as high as the language version."
}

function test_tool_java_language_greater_than_java_toolchain_runtime_version() {
mkdir -p pkg
cat >pkg/BUILD <<'EOF'
java_binary(
name = "Main",
srcs = ["Main.java"],
main_class = "com.example.Main",
deps = ["@bazel_tools//tools/java/runfiles"],
)
EOF

cat >pkg/Main.java <<'EOF'
package com.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
EOF

bazel build //pkg:Main \
--tool_java_language_version=20 \
--tool_java_runtime_version=remotejdk_20 \
&>"${TEST_log}" && fail "Expected build to fail"

expect_log "--tool_java_language_version=20 is incompatible with current Java runtime 'remotejdk_17': Both --tool_java_runtime_version and the 'java_runtime' attribute of the Java toolchain have to be set to versions at least as high as the language version."
}

run_suite "Tests detection of local JDK and that Bazel executes with a bundled JDK."
Loading