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

[#6664]: Collapse two bazel info invocations into one #6728

Merged
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 @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We were using this method in our extension to load the java-home from bazel info. Was there a reson to restrict its visibility?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I made accessors for individual values and restricted the set of values pulled from bazel info when i merged the two calls to it generated by the fact that i needed to pull info about startlark state (to see if bzlmod and other similar flags are set or not).

I also switched the callers of that to use the accessors. Not sure how i missed yours. What is the context of that miss ?

Copy link
Contributor Author

@mtoader mtoader Sep 27, 2024

Choose a reason for hiding this comment

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

This PR also moved to a white list of things pulled from bazel info. I'm pretty sure java-home was not there since it was not used in the plugin codebase. I can add that and make an accessor for it. Would that help you?

Copy link
Contributor

Choose a reason for hiding this comment

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

Its a internal plugin we have on top of bazel, so no easy to see it was being used :)

I'll let you know, Right not at least its only a compile error and should not be affecting runtime. I'm seeing whether we can use the projects SDK for this usecase instead of using this from bazelInfo.

Copy link
Contributor

Choose a reason for hiding this comment

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

Went ahead and created a PR to add java-home to the info cache: #6810
Our use case does require to use the same java that bazel uses. Thanks for the explanation, I was able to understand the changes better.

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
Loading