-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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. | ||
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)); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
@Override | ||
// TODO(bazel-team): this is the command-line passed options, we should remove from Starlark | ||
// probably. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thejava_runtime.version
instead of the value of--java_runtime_version=
?