Skip to content

Commit

Permalink
Expose bazel workspace_status to handlers (#7047)
Browse files Browse the repository at this point in the history
There is no need to parse URIs when their components are accessible
directly.

This is in preparation to a unified Bazel VCS handler.

Bug: n/a
Test: n/a
Change-Id: I9c97f6e4ddcf56a76a6b9831c34972a4e7e7c2c3

AOSP: cb89e1d2aff31858d26a4044de9253c972e0c219

Co-authored-by: Googler <[email protected]>
  • Loading branch information
LeFrosch and Googler authored Nov 26, 2024
1 parent c8496db commit 97b3ccf
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ public final class ParsedBepOutput {
new ParsedBepOutput(
"build-id",
null,
Optional.empty(),
ImmutableMap.of(),
ImmutableMap.of(),
ImmutableSetMultimap.of(),
0,
BuildResult.SUCCESS,
0,
ImmutableSet.of());

private static final String WORKSPACE_ITEM_KEY_SOURCE_URI = "SOURCE_URI";

/** Parses BEP events into {@link ParsedBepOutput} */
public static ParsedBepOutput parseBepArtifacts(InputStream bepStream)
throws BuildEventStreamException {
Expand Down Expand Up @@ -112,7 +110,7 @@ public static ParsedBepOutput parseBepArtifacts(
ImmutableSet.Builder<Label> targetsWithErrors = ImmutableSet.builder();
String localExecRoot = null;
String buildId = null;
Optional<String> sourceUri = Optional.empty();
ImmutableMap<String, String> workspaceStatus = ImmutableMap.of();
long startTimeMillis = 0L;
BuildResult buildResult = BuildResult.SUCCESS;
boolean emptyBuildEventStream = true;
Expand All @@ -124,14 +122,8 @@ public static ParsedBepOutput parseBepArtifacts(
localExecRoot = event.getWorkspaceInfo().getLocalExecRoot();
continue;
case WORKSPACE_STATUS:
ImmutableMap<String, Item> itemMap =
Maps.uniqueIndex(event.getWorkspaceStatus().getItemList(), Item::getKey);
// TODO(mathewi) This shouldn't really be here since it is dependant on VCS specific
// integration with Bazel. We should refactor this code to allow the BlazeVcsHandler to
// be involved here instead.
if (itemMap.containsKey(WORKSPACE_ITEM_KEY_SOURCE_URI)) {
sourceUri = Optional.of(itemMap.get(WORKSPACE_ITEM_KEY_SOURCE_URI).getValue());
}
workspaceStatus =
event.getWorkspaceStatus().getItemList().stream().collect(toImmutableMap(Item::getKey, Item::getValue));
continue;
case CONFIGURATION:
configIdToMnemonic.put(
Expand Down Expand Up @@ -196,7 +188,7 @@ public static ParsedBepOutput parseBepArtifacts(
return new ParsedBepOutput(
buildId,
localExecRoot,
sourceUri,
workspaceStatus,
filesMap,
targetToFileSets.build(),
startTimeMillis,
Expand Down Expand Up @@ -250,7 +242,7 @@ private static ImmutableMap<String, FileSet> fillInTransitiveFileSetData(
/** A path to the local execroot */
@Nullable private final String localExecRoot;

final Optional<String> sourceUri;
final ImmutableMap<String, String> workspaceStatus;

/** A map from file set ID to file set, with the same ordering as the BEP stream. */
private final ImmutableMap<String, FileSet> fileSets;
Expand All @@ -265,18 +257,18 @@ private static ImmutableMap<String, FileSet> fillInTransitiveFileSetData(
private final ImmutableSet<Label> targetsWithErrors;

private ParsedBepOutput(
@Nullable String buildId,
@Nullable String localExecRoot,
Optional<String> sourceUri,
ImmutableMap<String, FileSet> fileSets,
ImmutableSetMultimap<String, String> targetFileSets,
long syncStartTimeMillis,
BuildResult buildResult,
long bepBytesConsumed,
ImmutableSet<Label> targetsWithErrors) {
@Nullable String buildId,
@Nullable String localExecRoot,
ImmutableMap<String, String> workspaceStatus,
ImmutableMap<String, FileSet> fileSets,
ImmutableSetMultimap<String, String> targetFileSets,
long syncStartTimeMillis,
BuildResult buildResult,
long bepBytesConsumed,
ImmutableSet<Label> targetsWithErrors) {
this.buildId = buildId;
this.localExecRoot = localExecRoot;
this.sourceUri = sourceUri;
this.workspaceStatus = workspaceStatus;
this.fileSets = fileSets;
this.targetFileSets = targetFileSets;
this.syncStartTimeMillis = syncStartTimeMillis;
Expand All @@ -296,8 +288,8 @@ public String getLocalExecRoot() {
* specific. If present, the value will be can be used with {@link
* com.google.idea.blaze.base.vcs.BlazeVcsHandlerProvider.BlazeVcsHandler#vcsStateForSourceUri(String)}.
*/
public Optional<String> getSourceUri() {
return sourceUri;
public ImmutableMap<String, String> getWorkspaceStatus() {
return workspaceStatus;
}

/** Returns the build result. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
Expand All @@ -31,13 +30,11 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.ByteSource;
import com.google.common.io.MoreFiles;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.idea.blaze.base.bazel.BazelExitCodeException;
import com.google.idea.blaze.base.bazel.BazelExitCodeException.ThrowOption;
import com.google.idea.blaze.base.bazel.BuildSystem;
Expand All @@ -58,9 +55,7 @@
import com.google.idea.blaze.base.vcs.BlazeVcsHandlerProvider.BlazeVcsHandler;
import com.google.idea.blaze.common.Context;
import com.google.idea.blaze.common.Label;
import com.google.idea.blaze.common.Output;
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.common.artifact.BlazeArtifact;
import com.google.idea.blaze.common.artifact.BuildArtifactCache;
import com.google.idea.blaze.common.artifact.CachedArtifact;
import com.google.idea.blaze.common.artifact.OutputArtifact;
Expand Down Expand Up @@ -96,7 +91,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

/** An object that knows how to build dependencies for given targets */
public class BazelDependencyBuilder implements DependencyBuilder {
Expand Down Expand Up @@ -329,9 +323,9 @@ private OutputInfo createOutputInfo(
PrintOutput.log(String.format("Fetched and parsed artifact info files in %d ms", elapsed)));
}
Optional<VcsState> vcsState = Optional.empty();
if (blazeBuildOutputs.sourceUri.isPresent() && vcsHandler.isPresent()) {
if (vcsHandler.isPresent()) {
try {
vcsState = vcsHandler.get().vcsStateForSourceUri(blazeBuildOutputs.sourceUri.get());
vcsState = vcsHandler.get().vcsStateForWorkspaceStatus(blazeBuildOutputs.workspaceStatus);
} catch (BuildException e) {
context.handleExceptionAsWarning("Failed to get VCS state", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.idea.blaze.common.artifact.OutputArtifact;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand All @@ -41,7 +40,7 @@ public class BlazeBuildOutputs {

public static BlazeBuildOutputs noOutputs(BuildResult buildResult) {
return new BlazeBuildOutputs(
buildResult, ImmutableMap.of(), ImmutableMap.of(), ImmutableSet.of(), 0L, Optional.empty());
buildResult, ImmutableMap.of(), ImmutableMap.of(), ImmutableSet.of(), 0L, ImmutableMap.of());
}

@VisibleForTesting
Expand All @@ -52,7 +51,7 @@ public static BlazeBuildOutputs noOutputs(String buildId, BuildResult buildResul
ImmutableMap.of(buildId, buildResult),
ImmutableSet.of(),
0L,
Optional.empty());
ImmutableMap.of());
}

public static BlazeBuildOutputs fromParsedBepOutput(
Expand All @@ -69,7 +68,7 @@ public static BlazeBuildOutputs fromParsedBepOutput(
buildIdWithResult,
parsedOutput.getTargetsWithErrors(),
parsedOutput.getBepBytesConsumed(),
parsedOutput.getSourceUri());
parsedOutput.getWorkspaceStatus());
}

public final BuildResult buildResult;
Expand All @@ -78,7 +77,7 @@ public static BlazeBuildOutputs fromParsedBepOutput(
private final ImmutableSet<Label> targetsWithErrors;
public final long bepBytesConsumed;

public final Optional<String> sourceUri;
public final ImmutableMap<String, String> workspaceStatus;

/**
* {@link BepArtifactData} by {@link OutputArtifact#getBazelOutRelativePath()} for all artifacts from a
Expand All @@ -90,18 +89,18 @@ public static BlazeBuildOutputs fromParsedBepOutput(
private final ImmutableSetMultimap<String, OutputArtifact> perTargetArtifacts;

private BlazeBuildOutputs(
BuildResult buildResult,
Map<String, BepArtifactData> artifacts,
BuildResult buildResult,
Map<String, BepArtifactData> artifacts,
ImmutableMap<String, BuildResult> buildShardResults,
ImmutableSet<Label> targetsWithErrors,
long bepBytesConsumed,
Optional<String> sourceUri) {
ImmutableSet<Label> targetsWithErrors,
long bepBytesConsumed,
ImmutableMap<String, String> workspaceStatus) {
this.buildResult = buildResult;
this.artifacts = ImmutableMap.copyOf(artifacts);
this.buildShardResults = buildShardResults;
this.targetsWithErrors = targetsWithErrors;
this.bepBytesConsumed = bepBytesConsumed;
this.sourceUri = sourceUri;
this.workspaceStatus = workspaceStatus;

ImmutableSetMultimap.Builder<String, OutputArtifact> perTarget = ImmutableSetMultimap.builder();
artifacts.values().forEach(a -> a.topLevelTargets.forEach(t -> perTarget.put(t, a.artifact)));
Expand Down Expand Up @@ -171,9 +170,9 @@ public BlazeBuildOutputs updateOutputs(BlazeBuildOutputs nextOutputs) {
.collect(
// On duplicate buildIds, preserve most recent result
toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1)),
Sets.union(targetsWithErrors, nextOutputs.targetsWithErrors).immutableCopy(),
bepBytesConsumed + nextOutputs.bepBytesConsumed,
sourceUri);
Sets.union(targetsWithErrors, nextOutputs.targetsWithErrors).immutableCopy(),
bepBytesConsumed + nextOutputs.bepBytesConsumed,
workspaceStatus);
}

public ImmutableList<String> getBuildIds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -122,7 +123,7 @@ default Optional<ListenableFuture<VcsState>> getVcsState(
executor));
}

Optional<VcsState> vcsStateForSourceUri(String sourceUri) throws BuildException;
Optional<VcsState> vcsStateForWorkspaceStatus(Map<String, String> workspaceStatus) throws BuildException;

/**
* Diffs two VCS states from different points in time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.google.idea.blaze.base.sync.workspace.WorkingSet;
import com.google.idea.blaze.common.vcs.VcsState;
import com.google.idea.blaze.exception.BuildException;
import com.intellij.openapi.project.Project;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -79,7 +81,8 @@ public BlazeVcsSyncHandler createSyncHandler() {
}

@Override
public Optional<VcsState> vcsStateForSourceUri(String sourceUri) {
public Optional<VcsState> vcsStateForWorkspaceStatus(Map<String, String> workspaceStatus)
throws BuildException {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import com.google.idea.blaze.base.sync.workspace.WorkingSet;
import com.google.idea.blaze.base.vcs.BlazeVcsHandlerProvider;
import com.google.idea.blaze.common.vcs.VcsState;
import com.google.idea.blaze.exception.BuildException;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -113,7 +115,8 @@ public Optional<ListenableFuture<String>> getUpstreamVersion(
}

@Override
public Optional<VcsState> vcsStateForSourceUri(String sourceUri) {
public Optional<VcsState> vcsStateForWorkspaceStatus(Map<String, String> workspaceStatus)
throws BuildException {
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public BlazeVcsSyncHandler createSyncHandler() {
}

@Override
public Optional<VcsState> vcsStateForSourceUri(String sourceUri) {
public Optional<VcsState> vcsStateForWorkspaceStatus(Map<String, String> workspaceStatus) {
return Optional.empty();
}

Expand Down

0 comments on commit 97b3ccf

Please sign in to comment.