Skip to content

Commit

Permalink
Prettify labels in action progress messages with Bzlmod
Browse files Browse the repository at this point in the history
`UiStateTracker` is provided with the repository mapping of the main
repository after the loading phase has been completed and uses this
mapping to "unmap" canonical labels back to the apparent name used for
them by the main repository.
  • Loading branch information
fmeum committed Dec 18, 2022
1 parent 658ba15 commit a4aea06
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.devtools.build.lib.actions.extra.ExtraActionInfo;
import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Expand Down Expand Up @@ -363,18 +364,36 @@ public boolean showsOutputUnconditionally() {
@Nullable
@Override
public final String getProgressMessage() {
return getProgressMessageChecked(null);
}

@Nullable
@Override
public final String getProgressMessage(RepositoryMapping mainRepositoryMapping) {
Preconditions.checkNotNull(mainRepositoryMapping);
return getProgressMessageChecked(mainRepositoryMapping);
}

private String getProgressMessageChecked(@Nullable RepositoryMapping mainRepositoryMapping) {
String message = getRawProgressMessage();
if (message == null) {
return null;
}
message = replaceProgressMessagePlaceholders(message);
message = replaceProgressMessagePlaceholders(message, mainRepositoryMapping);
String additionalInfo = getOwner().getAdditionalProgressInfo();
return additionalInfo == null ? message : message + " [" + additionalInfo + "]";
}

private String replaceProgressMessagePlaceholders(String progressMessage) {
private String replaceProgressMessagePlaceholders(String progressMessage,
@Nullable RepositoryMapping mainRepositoryMapping) {
if (progressMessage.contains("%{label}") && getOwner().getLabel() != null) {
progressMessage = progressMessage.replace("%{label}", getOwner().getLabel().toString());
String labelString;
if (mainRepositoryMapping != null) {
labelString = getOwner().getLabel().getDisplayForm(mainRepositoryMapping);
} else {
labelString = getOwner().getLabel().toString();
}
progressMessage = progressMessage.replace("%{label}", labelString);
}
if (progressMessage.contains("%{output}") && getPrimaryOutput() != null) {
progressMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.actions;

import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import javax.annotation.Nullable;

Expand All @@ -33,6 +34,19 @@ public interface ActionExecutionMetadata extends ActionAnalysisMetadata {
@Nullable
String getProgressMessage();

/**
* A variant of {@link #getProgressMessage} that additionally takes the {@link RepositoryMapping}
* of the main repository, which can be used by the implementation to emit labels with apparent
* instead of canonical repository names. A return value of {@code null} indicates no
* message should be reported.
*
* <p>The default implementation simply returns the result of {@link #getProgressMessage}.
*/
@Nullable
default String getProgressMessage(RepositoryMapping mainRepositoryMapping) {
return getProgressMessage();
}

/**
* Returns a human-readable description of the inputs to {@link #getKey(ActionKeyContext)}. Used
* in the output from '--explain', and in error messages for '--check_up_to_date' and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.events.ExtendedEventHandler;

/**
Expand All @@ -24,6 +25,7 @@
public final class LoadingPhaseCompleteEvent implements ExtendedEventHandler.Postable {
private final ImmutableSet<Label> labels;
private final ImmutableSet<Label> filteredLabels;
private final RepositoryMapping mainRepositoryMapping;

/**
* Construct the event.
Expand All @@ -33,9 +35,11 @@ public final class LoadingPhaseCompleteEvent implements ExtendedEventHandler.Pos
*/
public LoadingPhaseCompleteEvent(
ImmutableSet<Label> labels,
ImmutableSet<Label> filteredLabels) {
ImmutableSet<Label> filteredLabels,
RepositoryMapping mainRepositoryMapping) {
this.labels = Preconditions.checkNotNull(labels);
this.filteredLabels = Preconditions.checkNotNull(filteredLabels);
this.mainRepositoryMapping = Preconditions.checkNotNull(mainRepositoryMapping);
}

/**
Expand All @@ -53,6 +57,13 @@ public ImmutableSet<Label> getFilteredLabels() {
return filteredLabels;
}

/**
* @return The repository mapping of the main repository.
*/
public RepositoryMapping getMainRepositoryMapping() {
return mainRepositoryMapping;
}

@Override
public boolean storeForReplay() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.google.devtools.build.lib.buildtool.buildevent.TestFilteringCompleteEvent;
import com.google.devtools.build.lib.clock.Clock;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.events.ExtendedEventHandler.FetchProgress;
import com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent;
import com.google.devtools.build.lib.skyframe.ConfigurationPhaseStartedEvent;
Expand Down Expand Up @@ -89,6 +90,8 @@ class UiStateTracker {

private String status;
protected String additionalMessage;
// Not null after the loading phase has completed.
private RepositoryMapping mainRepositoryMapping;

protected final Clock clock;

Expand Down Expand Up @@ -427,6 +430,7 @@ void loadingComplete(LoadingPhaseCompleteEvent event) {
} else {
additionalMessage = count + " targets";
}
mainRepositoryMapping = event.getMainRepositoryMapping();
}

/**
Expand Down Expand Up @@ -641,11 +645,11 @@ static String suffix(String s, int len) {
* If possible come up with a human-readable description of the label that fits within the given
* width; a non-positive width indicates not no restriction at all.
*/
private static String shortenedLabelString(Label label, int width) {
private String shortenedLabelString(Label label, int width) {
if (width <= 0) {
return label.toString();
return label.getDisplayForm(mainRepositoryMapping);
}
String name = label.toString();
String name = label.getDisplayForm(mainRepositoryMapping);
if (name.length() <= width) {
return name;
}
Expand Down Expand Up @@ -787,7 +791,7 @@ protected String describeAction(
postfix += " " + strategy;
}

String message = action.getProgressMessage();
String message = action.getProgressMessage(mainRepositoryMapping);
if (message == null) {
message = action.prettyPrint();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ public TargetPatternPhaseValue compute(SkyKey key, Environment env) throws Inter
mapOriginalPatternsToLabels(expandedPatterns, targets.getTargets()),
testSuiteExpansions.buildOrThrow()));
env.getListener()
.post(new LoadingPhaseCompleteEvent(result.getTargetLabels(), removedTargetLabels));
.post(new LoadingPhaseCompleteEvent(result.getTargetLabels(), removedTargetLabels,
repositoryMappingValue.getRepositoryMapping()));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.buildtool.ExecutionProgressReceiver;
import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;
import com.google.devtools.build.lib.buildtool.buildevent.ExecutionProgressReceiverAvailableEvent;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent;
import com.google.devtools.build.lib.runtime.SkymeldUiStateTracker.BuildStatus;
import com.google.devtools.build.lib.skyframe.ConfigurationPhaseStartedEvent;
Expand Down Expand Up @@ -74,7 +75,8 @@ public void loadingComplete_stateChanges() {
uiStateTracker.buildStatus = BuildStatus.TARGET_PATTERN_PARSING;

uiStateTracker.loadingComplete(
new LoadingPhaseCompleteEvent(ImmutableSet.of(), ImmutableSet.of()));
new LoadingPhaseCompleteEvent(ImmutableSet.of(), ImmutableSet.of(),
RepositoryMapping.ALWAYS_FALLBACK));

assertThat(uiStateTracker.buildStatus).isEqualTo(BuildStatus.LOADING_COMPLETE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -54,6 +57,8 @@
import com.google.devtools.build.lib.buildtool.buildevent.TestFilteringCompleteEvent;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.ExtendedEventHandler.FetchProgress;
import com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent;
import com.google.devtools.build.lib.runtime.SkymeldUiStateTracker.BuildStatus;
Expand Down Expand Up @@ -83,12 +88,18 @@
import net.starlark.java.syntax.Location;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.AdditionalMatchers;

/** Tests {@link UiStateTracker}. */
/**
* Tests {@link UiStateTracker}.
*/
@RunWith(TestParameterInjector.class)
public class UiStateTrackerTest extends FoundationTestCase {

@TestParameter boolean isSkymeld;
@TestParameter
boolean isSkymeld;
static final RepositoryMapping MOCK_REPO_MAPPING = RepositoryMapping.createAllowingFallback(
ImmutableMap.of("main", RepositoryName.MAIN));

private UiStateTracker getUiStateTracker(ManualClock clock) {
if (isSkymeld) {
Expand Down Expand Up @@ -186,8 +197,11 @@ private Action mockAction(String progressMessage, String primaryOutput) {
ActionsTestUtil.createArtifact(ArtifactRoot.asSourceRoot(Root.fromPath(outputBase)), path);

Action action = mock(Action.class);
when(action.getProgressMessage()).thenReturn(progressMessage);
when(action.getProgressMessage(eq(MOCK_REPO_MAPPING))).thenReturn(progressMessage);
when(action.getPrimaryOutput()).thenReturn(artifact);

verify(action, never()).getProgressMessage(AdditionalMatchers.not(eq(MOCK_REPO_MAPPING)));
verify(action, never()).getProgressMessage();
return action;
}

Expand All @@ -208,9 +222,13 @@ private ActionOwner dummyActionOwner() throws LabelSyntaxException {
}

private void simulateExecutionPhase(UiStateTracker uiStateTracker) {
uiStateTracker.loadingComplete(
new LoadingPhaseCompleteEvent(ImmutableSet.of(), ImmutableSet.of(), MOCK_REPO_MAPPING));
if (this.isSkymeld) {
// SkymeldUiStateTracker needs to be in the configuration phase before the execution phase.
((SkymeldUiStateTracker) uiStateTracker).buildStatus = BuildStatus.ANALYSIS_COMPLETE;
} else {
uiStateTracker.analysisComplete();
}
uiStateTracker.progressReceiverAvailable(
new ExecutionProgressReceiverAvailableEvent(dummyExecutionProgressReceiver()));
Expand Down Expand Up @@ -253,8 +271,8 @@ public void testLoadingActivity() throws IOException {
assertThat(loadingOutput).contains(loadingActivity);

// When it is configuring targets.
stateTracker.loadingComplete(
new LoadingPhaseCompleteEvent(ImmutableSet.of(), ImmutableSet.of()));
stateTracker.loadingComplete(new LoadingPhaseCompleteEvent(ImmutableSet.of(), ImmutableSet.of(),
MOCK_REPO_MAPPING));
String additionalMessage = "5 targets";
stateTracker.additionalMessage = additionalMessage;
String configuredTargetProgressString = "5 targets configured";
Expand Down

0 comments on commit a4aea06

Please sign in to comment.