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 b9f90ff
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 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,11 @@ public interface ActionExecutionMetadata extends ActionAnalysisMetadata {
@Nullable
String 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,7 @@ class UiStateTracker {

private String status;
protected String additionalMessage;
private RepositoryMapping mainRepositoryMapping = RepositoryMapping.ALWAYS_FALLBACK;

protected final Clock clock;

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

/**
Expand Down Expand Up @@ -641,11 +644,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 +790,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,6 +18,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -54,6 +55,7 @@
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.events.ExtendedEventHandler.FetchProgress;
import com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent;
import com.google.devtools.build.lib.runtime.SkymeldUiStateTracker.BuildStatus;
Expand Down Expand Up @@ -187,6 +189,7 @@ private Action mockAction(String progressMessage, String primaryOutput) {

Action action = mock(Action.class);
when(action.getProgressMessage()).thenReturn(progressMessage);
when(action.getProgressMessage(any())).thenReturn(progressMessage);
when(action.getPrimaryOutput()).thenReturn(artifact);
return action;
}
Expand Down Expand Up @@ -253,8 +256,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(),
RepositoryMapping.ALWAYS_FALLBACK));
String additionalMessage = "5 targets";
stateTracker.additionalMessage = additionalMessage;
String configuredTargetProgressString = "5 targets configured";
Expand Down

0 comments on commit b9f90ff

Please sign in to comment.