From 4e35c02c7c400bfbbfa69164a1ec3bd51966ca79 Mon Sep 17 00:00:00 2001 From: kshyanashree <109167932+kshyanashree@users.noreply.github.com> Date: Tue, 14 Feb 2023 14:35:50 -0800 Subject: [PATCH] Prettify labels in action progress messages with Bzlmod (#17278) `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. Fixes #17130 Closes #17045. PiperOrigin-RevId: 501256229 Change-Id: I796585da07773ee3a589bb83948b9e0667b2e799 Co-authored-by: Fabian Meumertzheim --- .../build/lib/actions/AbstractAction.java | 34 +++++++++++++++++++ .../lib/actions/ActionExecutionMetadata.java | 14 ++++++++ .../pkgcache/LoadingPhaseCompleteEvent.java | 13 ++++++- .../lib/runtime/SkymeldUiStateTracker.java | 1 + .../build/lib/runtime/UiStateTracker.java | 15 +++++--- .../skyframe/TargetPatternPhaseFunction.java | 6 +++- .../runtime/SkymeldUiStateTrackerTest.java | 4 ++- .../build/lib/runtime/UiStateTrackerTest.java | 19 +++++++++-- 8 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java b/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java index 262754415cf3cb..e76a72dcfedf13 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java +++ b/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java @@ -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; @@ -363,14 +364,47 @@ 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, mainRepositoryMapping); String additionalInfo = getOwner().getAdditionalProgressInfo(); return additionalInfo == null ? message : message + " [" + additionalInfo + "]"; } + private String replaceProgressMessagePlaceholders( + String progressMessage, @Nullable RepositoryMapping mainRepositoryMapping) { + if (progressMessage.contains("%{label}") && getOwner().getLabel() != null) { + 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 = + progressMessage.replace("%{output}", getPrimaryOutput().getExecPathString()); + } + if (progressMessage.contains("%{input}") && getPrimaryInput() != null) { + progressMessage = progressMessage.replace("%{input}", getPrimaryInput().getExecPathString()); + } + return progressMessage; + } + /** * Returns a progress message string that is specific for this action. This is then annotated with * additional information, currently the string '[for tool]' for actions in the tool diff --git a/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionMetadata.java b/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionMetadata.java index 338fb06993fdbc..4d21092df79b82 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionMetadata.java +++ b/src/main/java/com/google/devtools/build/lib/actions/ActionExecutionMetadata.java @@ -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; @@ -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. + * + *

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 diff --git a/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseCompleteEvent.java b/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseCompleteEvent.java index aad9f721f1ba05..f40f7a2ceb5aae 100644 --- a/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseCompleteEvent.java +++ b/src/main/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseCompleteEvent.java @@ -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; /** @@ -24,6 +25,7 @@ public final class LoadingPhaseCompleteEvent implements ExtendedEventHandler.Postable { private final ImmutableSet