Skip to content

Commit

Permalink
Fix fast run with enabled bzlmod
Browse files Browse the repository at this point in the history
Look for the java binary in the right directory under runfiles when bzlmod is enabled

(cherry picked from commit ce6dd7e)
  • Loading branch information
mai93 committed Nov 21, 2023
1 parent b302c78 commit 578e7f5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,36 @@ String getTestRunner() {

@Override
File getJavaBinFromLauncher(
Project project, Label label, @Nullable Label javaLauncher, boolean swigdeps) {
Project project,
Label label,
@Nullable Label javaLauncher,
boolean swigdeps,
String runfilesPath) {
if (javaLauncher == null) {
return STANDARD_JAVA_BINARY;
return getStandardJavaBinary(runfilesPath);
} else {
return new File(getTestBinary(label) + "_nativedeps");
}
}

/**
* Look for the directory containing Bazel local jdk and return the java binary.
*
* <p>Bazel adds the Java launcher to the runfiles path when building a Java test target. If
* `bzlmod` is enabled, the directory name is formatted as
* 'rules_java~{RULES_JAVA_VERSION}~toolchains~local_jdk' otherwise it is `local_jdk`.
*/
private static File getStandardJavaBinary(String runfilesPath) {
for (File file :
new File(runfilesPath)
.listFiles(fn -> fn.getName().matches("rules_java~.*~toolchains~local_jdk"))) {
if (file.isDirectory()) {
return file.toPath().resolve("bin/java").toFile();
}
}
return STANDARD_JAVA_BINARY;
}

@Override
public ImmutableSet<BuildSystemName> getSupportedBuildSystems() {
return ImmutableSet.of(BuildSystemName.Bazel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ static FastBuildTestEnvironmentCreator getInstance(BuildSystemName buildSystemNa
abstract String getTestRunner();

abstract File getJavaBinFromLauncher(
Project project, Label label, @Nullable Label javaLauncher, boolean swigdeps);
Project project,
Label label,
@Nullable Label javaLauncher,
boolean swigdeps,
String runfilesDir);

GeneralCommandLine createCommandLine(
Project project,
Expand Down Expand Up @@ -95,7 +99,11 @@ GeneralCommandLine createCommandLine(

commandBuilder.setJavaBinary(
getJavaBinFromLauncher(
project, target, getLauncher(fastBuildInfo).orElse(null), getSwigdeps(fastBuildInfo)));
project,
target,
getLauncher(fastBuildInfo).orElse(null),
getSwigdeps(fastBuildInfo),
runfilesDir.toString()));

fastBuildInfo.classpath().forEach(commandBuilder::addClasspathElement);

Expand Down

0 comments on commit 578e7f5

Please sign in to comment.