Skip to content

Commit

Permalink
[bazelbuild#6664]: Collapse two bazel info invocations into one.
Browse files Browse the repository at this point in the history
  In PR bazelbuild#6711 i added two back to back bazel info calls.

  This PR collapsed  those calls into one.
  • Loading branch information
mtoader committed Sep 5, 2024
1 parent 78fa3ee commit 7df9d91
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static BazelVersion parseVersion(String[] numbers) {
}

static BazelVersion parseVersion(BlazeInfo blazeInfo) {
return parseVersion(blazeInfo.get(BlazeInfo.RELEASE));
return parseVersion(blazeInfo.getRelease());
}

@Override
Expand Down
12 changes: 10 additions & 2 deletions base/src/com/google/idea/blaze/base/command/info/BlazeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.intellij.model.ProjectData;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.idea.blaze.base.ideinfo.ProtoWrapper;
Expand All @@ -33,7 +34,6 @@ public abstract class BlazeInfo implements ProtoWrapper<ProjectData.BlazeInfo> {
public static final String BUILD_LANGUAGE = "build-language";
public static final String OUTPUT_BASE_KEY = "output_base";
public static final String OUTPUT_PATH_KEY = "output_path";
public static final String MASTER_LOG = "master-log";
public static final String RELEASE = "release";

public static final String STARLARK_SEMANTICS = "starlark-semantics";
Expand Down Expand Up @@ -114,7 +114,7 @@ private static String getOrThrow(ImmutableMap<String, String> map, String key) {

abstract ImmutableMap<String, String> getBlazeInfoMap();

public String get(String key) {
protected String get(String key) {
return getBlazeInfoMap().get(key);
}

Expand All @@ -140,6 +140,14 @@ public File getBlazeTestlogsDirectory() {

public abstract File getOutputBase();

public String getStarlarkSemantics() {
return getBlazeInfoMap().get(STARLARK_SEMANTICS);
}

public String getRelease() {
return getBlazeInfoMap().get(RELEASE);
}

/** Creates a mock blaze info with the minimum information required for syncing. */
@VisibleForTesting
public static BlazeInfo createMockBlazeInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import java.util.List;
Expand All @@ -27,32 +28,32 @@
public abstract class BlazeInfoRunner {

public static BlazeInfoRunner getInstance() {
return ServiceManager.getService(BlazeInfoRunner.class);
return ApplicationManager.getApplication().getService(BlazeInfoRunner.class);
}

/**
* @param blazeFlags The blaze flags that will be passed to Blaze.
* @param key The key passed to blaze info
* @param keys The key passed to blaze info
* @return The blaze info value associated with the specified key
*/
public abstract ListenableFuture<String> runBlazeInfo(
Project project,
BuildInvoker invoker,
BlazeContext context,
List<String> blazeFlags,
String key);
String ...keys);

/**
* @param blazeFlags The blaze flags that will be passed to Blaze.
* @param key The key passed to blaze info
* @param keys The keys passed to blaze info
* @return The blaze info value associated with the specified key
*/
public abstract ListenableFuture<byte[]> runBlazeInfoGetBytes(
Project project,
BuildInvoker invoker,
BlazeContext context,
List<String> blazeFlags,
String key);
String ...keys);

/**
* This calls blaze info without any specific key so blaze info will return all keys and values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.idea.blaze.base.async.executor.BlazeExecutor;
import com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker;
import com.google.idea.blaze.base.command.BlazeCommand;
Expand All @@ -39,14 +38,14 @@ public ListenableFuture<byte[]> runBlazeInfoGetBytes(
BuildInvoker invoker,
BlazeContext context,
List<String> blazeFlags,
String key) {
String... keys) {
return BlazeExecutor.getInstance()
.submit(
() -> {
BlazeCommand.Builder builder = BlazeCommand.builder(invoker, BlazeCommandName.INFO);
builder.addBlazeFlags(blazeFlags);
if (key != null) {
builder.addBlazeFlags(key);
if (keys != null) {
builder.addBlazeFlags(keys);
}
try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper();
InputStream blazeInfoStream =
Expand All @@ -64,9 +63,9 @@ public ListenableFuture<String> runBlazeInfo(
BuildInvoker invoker,
BlazeContext context,
List<String> blazeFlags,
String key) {
String... keys) {
return Futures.transform(
runBlazeInfoGetBytes(project, invoker, context, blazeFlags, key),
runBlazeInfoGetBytes(project, invoker, context, blazeFlags, keys),
bytes -> new String(bytes, StandardCharsets.UTF_8).trim(),
BlazeExecutor.getInstance().getExecutor());
}
Expand All @@ -78,23 +77,27 @@ public ListenableFuture<BlazeInfo> runBlazeInfo(
BlazeContext context,
BuildSystemName buildSystemName,
List<String> blazeFlags) {
ListeningExecutorService executor = BlazeExecutor.getInstance().getExecutor();

return Futures.transformAsync(
runBlazeInfoGetBytes(project, invoker, context, blazeFlags, /* key= */ null),
bytes -> {
ImmutableMap.Builder<String, String> builder = parseBlazeInfoResult(new String(bytes, StandardCharsets.UTF_8).trim());

return Futures.transform(
runBlazeInfo(project, invoker, context, blazeFlags, BlazeInfo.STARLARK_SEMANTICS),

starLarkSemantics -> {
builder.put(BlazeInfo.STARLARK_SEMANTICS, starLarkSemantics);

return BlazeInfo.create(buildSystemName, builder.build());
}, executor);
},
executor);
return Futures.transform(
runBlazeInfoGetBytes(
project,
invoker,
context,
blazeFlags,
BlazeInfo.blazeBinKey(buildSystemName),
BlazeInfo.blazeGenfilesKey(buildSystemName),
BlazeInfo.blazeTestlogsKey(buildSystemName),
BlazeInfo.EXECUTION_ROOT_KEY,
BlazeInfo.PACKAGE_PATH_KEY,
BlazeInfo.OUTPUT_PATH_KEY,
BlazeInfo.OUTPUT_BASE_KEY,
BlazeInfo.RELEASE,
BlazeInfo.STARLARK_SEMANTICS),
bytes ->
BlazeInfo.create(
buildSystemName,
parseBlazeInfoResult(new String(bytes, StandardCharsets.UTF_8).trim()).build()),
BlazeExecutor.getInstance().getExecutor());
}

private static ImmutableMap.Builder<String, String> parseBlazeInfoResult(String blazeInfoString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ListenableFuture<ExternalWorkspaceData> getExternalWorkspaceData(

// validate that bzlmod is enabled (technically this validates that the --enable_bzlmod is not
// changed from the default `true` aka set to false)
String starLarkSemantics = blazeInfo.get(BlazeInfo.STARLARK_SEMANTICS);
String starLarkSemantics = blazeInfo.getStarlarkSemantics();
if (starLarkSemantics == null || starLarkSemantics.isEmpty() || starLarkSemantics.contains("enable_bzlmod=false")) {
return Futures.immediateFuture(ExternalWorkspaceData.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ImmutableList<String> getBlazeFlags() {
}

public static File getOutputFile(BlazeInfo blazeInfo) {
File coverageRoot = new File(blazeInfo.get(BlazeInfo.OUTPUT_PATH_KEY), "_coverage");
File coverageRoot = new File(blazeInfo.getOutputBase(), "_coverage");
return new File(coverageRoot, "_coverage_report.dat");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@
import com.intellij.testFramework.EdtTestUtil;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.util.lang.JavaVersion;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -287,8 +290,11 @@ public ListenableFuture<String> runBlazeInfo(
BuildInvoker invoker,
BlazeContext context,
List<String> blazeFlags,
String key) {
return Futures.immediateFuture(results.get(key));
String ...keys) {
return Futures.immediateFuture(
Arrays.stream(keys)
.map(key -> String.format("%s: %s", key, results.get(key)))
.collect(Collectors.joining("\n")));
}

@Override
Expand All @@ -308,7 +314,7 @@ public ListenableFuture<byte[]> runBlazeInfoGetBytes(
BuildInvoker invoker,
BlazeContext context,
List<String> blazeFlags,
String key) {
String ...key) {
return Futures.immediateFuture(null);
}

Expand Down

0 comments on commit 7df9d91

Please sign in to comment.