Skip to content

Commit 6586341

Browse files
committed
Give StarlarkValue#debugPrint access to the StarlarkThread
Since `debugPrint` is meant for emitting potentially non-deterministic, it is safe to give it access to `StarlarkThread` information. This will be used in a follow-up to let `print` and `fail` emit `Label`s in display form without having to track the reverse repo mapping for the sake of invalidation of repo rules or module extensions. Work towards bazelbuild#20486
1 parent 7ffa325 commit 6586341

30 files changed

+222
-49
lines changed

src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,9 @@ private String getProgressMessageChecked(@Nullable RepositoryMapping mainReposit
368368
private String replaceProgressMessagePlaceholders(
369369
String progressMessage, @Nullable RepositoryMapping mainRepositoryMapping) {
370370
if (progressMessage.contains("%{label}") && owner.getLabel() != null) {
371-
String labelString;
372-
if (mainRepositoryMapping != null) {
373-
labelString = owner.getLabel().getDisplayForm(mainRepositoryMapping);
374-
} else {
375-
labelString = owner.getLabel().toString();
376-
}
377-
progressMessage = progressMessage.replace("%{label}", labelString);
371+
progressMessage =
372+
progressMessage.replace(
373+
"%{label}", owner.getLabel().getDisplayForm(mainRepositoryMapping));
378374
}
379375
if (progressMessage.contains("%{output}") && getPrimaryOutput() != null) {
380376
progressMessage =

src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/RuleConfiguredTarget.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import net.starlark.java.eval.Dict;
5555
import net.starlark.java.eval.Printer;
5656
import net.starlark.java.eval.Starlark;
57-
import net.starlark.java.eval.StarlarkSemantics;
57+
import net.starlark.java.eval.StarlarkThread;
5858

5959
/**
6060
* A {@link com.google.devtools.build.lib.analysis.ConfiguredTarget} that is produced by a rule.
@@ -249,7 +249,7 @@ public void repr(Printer printer) {
249249
}
250250

251251
@Override
252-
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
252+
public void debugPrint(Printer printer, StarlarkThread thread) {
253253
// Show the names of the provider keys that this target propagates.
254254
// Provider key names might potentially be *private* information, and thus a comprehensive
255255
// list of provider keys should not be exposed in any way other than for debug information.

src/main/java/com/google/devtools/build/lib/analysis/starlark/Args.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void repr(Printer printer) {
7171
}
7272

7373
@Override
74-
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
74+
public void debugPrint(Printer printer, StarlarkThread thread) {
7575
try {
7676
printer.append(
7777
Joiner.on(" ").join(build(/* mainRepoMappingSupplier= */ () -> null).arguments()));

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionValue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* graphs.
2828
*/
2929
@AutoValue
30-
abstract class BazelModuleResolutionValue implements SkyValue {
30+
public abstract class BazelModuleResolutionValue implements SkyValue {
3131
/* TODO(andreisolo): Also load the modules overridden by {@code single_version_override} or
3232
NonRegistryOverride if we need to detect changes in the dependency graph caused by them.
3333
*/

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileValue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static Key key(ModuleKey moduleKey, @Nullable ModuleOverride override) {
9999
/** {@link SkyKey} for {@link ModuleFileValue} computation. */
100100
@AutoCodec
101101
@AutoValue
102-
abstract static class Key implements SkyKey {
102+
public abstract static class Key implements SkyKey {
103103
private static final SkyKeyInterner<Key> interner = SkyKey.newInterner();
104104

105105
abstract ModuleKey getModuleKey();

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/TypeCheckedTag.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import net.starlark.java.annot.StarlarkBuiltin;
2525
import net.starlark.java.eval.EvalException;
2626
import net.starlark.java.eval.Printer;
27-
import net.starlark.java.eval.StarlarkSemantics;
27+
import net.starlark.java.eval.StarlarkThread;
2828
import net.starlark.java.eval.Structure;
2929
import net.starlark.java.spelling.SpellChecker;
3030
import net.starlark.java.syntax.Location;
@@ -150,7 +150,7 @@ public String getErrorMessageForUnknownField(String field) {
150150
}
151151

152152
@Override
153-
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
153+
public void debugPrint(Printer printer, StarlarkThread thread) {
154154
printer.append(String.format("'%s' tag at %s", tagClassName, location));
155155
}
156156
}

src/main/java/com/google/devtools/build/lib/cmdline/BazelModuleContext.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public abstract class BazelModuleContext {
4444
/** The repository mapping applicable to the repo where the .bzl file is located in. */
4545
public abstract RepositoryMapping repoMapping();
4646

47+
/**
48+
* The repository mapping applicable to the main repository. This is purely meant to support
49+
* {@link Label#debugPrint}.
50+
*/
51+
@Nullable
52+
public abstract RepositoryMapping mainRepoMapping();
53+
4754
/** Returns the name of the module's .bzl file, as provided to the parser. */
4855
public abstract String filename();
4956

@@ -160,11 +167,12 @@ public static BazelModuleContext ofInnermostBzlOrFail(StarlarkThread thread, Str
160167
public static BazelModuleContext create(
161168
Label label,
162169
RepositoryMapping repoMapping,
170+
@Nullable RepositoryMapping mainRepoMapping,
163171
String filename,
164172
ImmutableList<Module> loads,
165173
byte[] bzlTransitiveDigest) {
166174
return new AutoValue_BazelModuleContext(
167-
label, repoMapping, filename, loads, bzlTransitiveDigest);
175+
label, repoMapping, mainRepoMapping, filename, loads, bzlTransitiveDigest);
168176
}
169177

170178
public final Label.PackageContext packageContext() {

src/main/java/com/google/devtools/build/lib/cmdline/Label.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@
6666
"A BUILD target identifier."
6767
+ "<p>For every <code>Label</code> instance <code>l</code>, the string representation"
6868
+ " <code>str(l)</code> has the property that <code>Label(str(l)) == l</code>,"
69-
+ " regardless of where the <code>Label()</code> call occurs.")
69+
+ " regardless of where the <code>Label()</code> call occurs."
70+
+ "<p>When passed as positional arguments to <code>print()</code> or <code>fail()"
71+
+ "</code>, <code>Label</code> use a string representation optimized for human readability"
72+
+ " instead. This representation uses an <a href=\"/external/overview#apparent-repo-name\">"
73+
+ "apparent repository name</a> from the perspective of the main repository if possible.")
7074
@Immutable
7175
@ThreadSafe
7276
public final class Label implements Comparable<Label>, StarlarkValue, SkyKey, CommandLineItem {
@@ -442,7 +446,7 @@ public String getUnambiguousCanonicalForm() {
442446
* @param mainRepositoryMapping the {@link RepositoryMapping} of the main repository
443447
* @return analogous to {@link PackageIdentifier#getDisplayForm(RepositoryMapping)}
444448
*/
445-
public String getDisplayForm(RepositoryMapping mainRepositoryMapping) {
449+
public String getDisplayForm(@Nullable RepositoryMapping mainRepositoryMapping) {
446450
return packageIdentifier.getDisplayForm(mainRepositoryMapping) + ":" + name;
447451
}
448452

@@ -655,6 +659,11 @@ public void repr(Printer printer) {
655659
printer.append(")");
656660
}
657661

662+
@Override
663+
public void debugPrint(Printer printer, StarlarkThread thread) {
664+
printer.append(getDisplayForm(BazelModuleContext.ofInnermostBzlOrThrow(thread).mainRepoMapping()));
665+
}
666+
658667
@Override
659668
public void str(Printer printer, StarlarkSemantics semantics) {
660669
if (getRepository().isMain()

src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.devtools.build.skyframe.SkyFunctionName;
2525
import com.google.devtools.build.skyframe.SkyKey;
2626
import com.google.devtools.build.skyframe.SkyKey.SkyKeyInterner;
27+
import javax.annotation.Nullable;
2728
import javax.annotation.concurrent.Immutable;
2829

2930
/**
@@ -226,7 +227,7 @@ public String getUnambiguousCanonicalForm() {
226227
* <dd>only with Bzlmod if the current package belongs to a repository that is not visible
227228
* from the main module
228229
*/
229-
public String getDisplayForm(RepositoryMapping mainRepositoryMapping) {
230+
public String getDisplayForm(@Nullable RepositoryMapping mainRepositoryMapping) {
230231
return repository.getDisplayForm(mainRepositoryMapping) + "//" + pkgName;
231232
}
232233

src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,25 @@ public String getCanonicalForm() {
249249
* <dt><code>@protobuf</code>
250250
* <dd>if this repository is a WORKSPACE dependency and its <code>name</code> is "protobuf",
251251
* or if this repository is a Bzlmod dependency of the main module and its apparent name
252-
* is "protobuf"
252+
* is "protobuf" (in both cases only if mainRepositoryMapping is not null)
253253
* <dt><code>@@protobuf~3.19.2</code>
254254
* <dd>only with Bzlmod, if this a repository that is not visible from the main module
255255
*/
256-
public String getDisplayForm(RepositoryMapping mainRepositoryMapping) {
256+
public String getDisplayForm(@Nullable RepositoryMapping mainRepositoryMapping) {
257257
Preconditions.checkArgument(
258-
mainRepositoryMapping.ownerRepo() == null || mainRepositoryMapping.ownerRepo().isMain());
258+
mainRepositoryMapping == null
259+
|| mainRepositoryMapping.ownerRepo() == null
260+
|| mainRepositoryMapping.ownerRepo().isMain());
259261
if (!isVisible()) {
260262
return getNameWithAt();
261263
}
262264
if (isMain()) {
263265
// Packages in the main repository can always use repo-relative form.
264266
return "";
265267
}
268+
if (mainRepositoryMapping == null) {
269+
return getNameWithAt();
270+
}
266271
if (!mainRepositoryMapping.usesStrictDeps()) {
267272
// If the main repository mapping is not using strict visibility, then Bzlmod is certainly
268273
// disabled, which means that canonical and apparent names can be used interchangeably from

src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkingContext.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -276,23 +276,23 @@ public Sequence<Linkstamp> getLinkstampsForStarlark() {
276276
}
277277

278278
@Override
279-
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
279+
public void debugPrint(Printer printer, StarlarkThread thread) {
280280
printer.append("<LinkerInput(owner=");
281281
if (owner == null) {
282282
printer.append("[null owner, uses old create_linking_context API]");
283283
} else {
284-
owner.debugPrint(printer, semantics);
284+
owner.debugPrint(printer, thread);
285285
}
286286
printer.append(", libraries=[");
287287
for (LibraryToLink libraryToLink : libraries) {
288-
libraryToLink.debugPrint(printer, semantics);
288+
libraryToLink.debugPrint(printer, thread);
289289
printer.append(", ");
290290
}
291291
printer.append("], userLinkFlags=[");
292292
printer.append(Joiner.on(", ").join(userLinkFlags));
293293
printer.append("], nonCodeInputs=[");
294294
for (Artifact nonCodeInput : nonCodeInputs) {
295-
nonCodeInput.debugPrint(printer, semantics);
295+
nonCodeInput.debugPrint(printer, thread);
296296
printer.append(", ");
297297
}
298298
// TODO(cparsons): Add debug repesentation of linkstamps.
@@ -390,10 +390,10 @@ public String toString() {
390390
@Nullable private final ExtraLinkTimeLibraries extraLinkTimeLibraries;
391391

392392
@Override
393-
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
393+
public void debugPrint(Printer printer, StarlarkThread thread) {
394394
printer.append("<CcLinkingContext([");
395395
for (LinkerInput linkerInput : linkerInputs.toList()) {
396-
linkerInput.debugPrint(printer, semantics);
396+
linkerInput.debugPrint(printer, thread);
397397
printer.append(", ");
398398
}
399399
printer.append("])>");

src/main/java/com/google/devtools/build/lib/rules/cpp/FeatureConfigurationForStarlark.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void str(Printer printer, StarlarkSemantics semantics) {
6262
}
6363

6464
@Override
65-
public void debugPrint(Printer printer, StarlarkSemantics semantics) {
65+
public void debugPrint(Printer printer, StarlarkThread thread) {
6666
printer.append("<FeatureConfiguration(");
6767
printer.append(Joiner.on(", ").join(featureConfiguration.getEnabledFeatureNames()));
6868
printer.append(")>");

src/main/java/com/google/devtools/build/lib/rules/cpp/LibraryToLink.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import net.starlark.java.eval.Printer;
3232
import net.starlark.java.eval.Sequence;
3333
import net.starlark.java.eval.StarlarkList;
34-
import net.starlark.java.eval.StarlarkSemantics;
3534
import net.starlark.java.eval.StarlarkThread;
3635

3736
/** Encapsulates information for linking a library. */
@@ -224,7 +223,7 @@ LinkerInputs.LibraryToLink getInterfaceLibraryToLink() {
224223
abstract boolean getDisableWholeArchive();
225224

226225
@Override
227-
public final void debugPrint(Printer printer, StarlarkSemantics semantics) {
226+
public final void debugPrint(Printer printer, StarlarkThread thread) {
228227
printer.append("<LibraryToLink(");
229228
printer.append(
230229
Joiner.on(", ")

src/main/java/com/google/devtools/build/lib/skyframe/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,7 @@ java_library(
25872587
":sky_functions",
25882588
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:module_extension",
25892589
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:repo_rule_value",
2590+
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution",
25902591
"//src/main/java/com/google/devtools/build/lib/cmdline",
25912592
"//src/main/java/com/google/devtools/build/lib/events",
25922593
"//src/main/java/com/google/devtools/build/lib/packages",

src/main/java/com/google/devtools/build/lib/skyframe/BzlLoadFunction.java

+34
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import java.util.LinkedHashSet;
6666
import java.util.List;
6767
import java.util.Map;
68+
import java.util.Optional;
6869
import java.util.concurrent.atomic.AtomicBoolean;
6970
import java.util.function.Consumer;
7071
import javax.annotation.Nullable;
@@ -762,6 +763,11 @@ private BzlLoadValue computeInternalWithCompiledBzl(
762763
if (repoMapping == null) {
763764
return null;
764765
}
766+
Optional<RepositoryMapping> mainRepoMapping =
767+
getMainRepositoryMapping(key, builtins.starlarkSemantics, env);
768+
if (mainRepoMapping == null) {
769+
return null;
770+
}
765771
Label.RepoMappingRecorder repoMappingRecorder = new Label.RepoMappingRecorder();
766772
ImmutableList<Pair<String, Location>> programLoads = getLoadsFromProgram(prog);
767773
ImmutableList<Label> loadLabels =
@@ -840,6 +846,7 @@ private BzlLoadValue computeInternalWithCompiledBzl(
840846
BazelModuleContext.create(
841847
label,
842848
repoMapping,
849+
mainRepoMapping.orElse(null),
843850
prog.getFilename(),
844851
ImmutableList.copyOf(loadMap.values()),
845852
transitiveDigest);
@@ -972,6 +979,33 @@ private static RepositoryMapping getRepositoryMapping(
972979
return repositoryMappingValue.getRepositoryMapping();
973980
}
974981

982+
@Nullable
983+
private static Optional<RepositoryMapping> getMainRepositoryMapping(
984+
BzlLoadValue.Key key, StarlarkSemantics starlarkSemantics, Environment env)
985+
throws InterruptedException {
986+
if (!starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD)) {
987+
return Optional.empty();
988+
}
989+
RepositoryMappingValue.Key repoMappingKey;
990+
if (key instanceof BzlLoadValue.KeyForBuild) {
991+
repoMappingKey = RepositoryMappingValue.key(RepositoryName.MAIN);
992+
} else if ((key instanceof BzlLoadValue.KeyForBzlmod
993+
&& !(key instanceof BzlLoadValue.KeyForBzlmodBootstrap))
994+
|| key instanceof BzlLoadValue.KeyForWorkspace) {
995+
// Since the main repo mapping requires evaluating WORKSPACE, but WORKSPACE can load from
996+
// extension repos, requesting the full main repo mapping would cause a cycle.
997+
repoMappingKey = RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS;
998+
} else {
999+
// Builtins and Bzlmod bootstrap repo rules don't need the main repo mapping.
1000+
return Optional.empty();
1001+
}
1002+
var mainRepositoryMappingValue = (RepositoryMappingValue) env.getValue(repoMappingKey);
1003+
if (mainRepositoryMappingValue == null) {
1004+
return null;
1005+
}
1006+
return Optional.of(mainRepositoryMappingValue.getRepositoryMapping());
1007+
}
1008+
9751009
/**
9761010
* Validates a label appearing in a {@code load()} statement, throwing {@link
9771011
* LabelSyntaxException} on failure.

src/main/java/com/google/devtools/build/lib/skyframe/BzlmodRepoCycleReporter.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import com.google.common.base.Predicates;
2121
import com.google.common.collect.ImmutableList;
2222
import com.google.common.collect.Iterables;
23+
import com.google.devtools.build.lib.bazel.bzlmod.BazelDepGraphValue;
24+
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleResolutionValue;
2325
import com.google.devtools.build.lib.bazel.bzlmod.BzlmodRepoRuleValue;
2426
import com.google.devtools.build.lib.bazel.bzlmod.ModuleExtensionId;
27+
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue;
2528
import com.google.devtools.build.lib.cmdline.Label;
2629
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
2730
import com.google.devtools.build.lib.events.Event;
@@ -69,6 +72,15 @@ public class BzlmodRepoCycleReporter implements CyclesReporter.SingleCycleReport
6972
private static final Predicate<SkyKey> IS_WORKSPACE_FILE =
7073
SkyFunctions.isSkyFunction(WorkspaceFileValue.WORKSPACE_FILE);
7174

75+
private static final Predicate<SkyKey> IS_MODULE_RESOLUTION =
76+
SkyFunctions.isSkyFunction(SkyFunctions.BAZEL_MODULE_RESOLUTION);
77+
78+
private static final Predicate<SkyKey> IS_DEP_GRAPH =
79+
SkyFunctions.isSkyFunction(SkyFunctions.BAZEL_DEP_GRAPH);
80+
81+
private static final Predicate<SkyKey> IS_MODULE_FILE =
82+
SkyFunctions.isSkyFunction(SkyFunctions.MODULE_FILE);
83+
7284
private static void requestRepoDefinitions(
7385
ExtendedEventHandler eventHandler, Iterable<SkyKey> repos) {
7486
for (SkyKey repo : repos) {
@@ -113,7 +125,10 @@ public boolean maybeReportCycle(
113125
IS_MODULE_EXTENSION_REPO_MAPPING_ENTRIES,
114126
IS_PACKAGE,
115127
IS_EXTERNAL_PACKAGE,
116-
IS_WORKSPACE_FILE))
128+
IS_WORKSPACE_FILE,
129+
IS_MODULE_RESOLUTION,
130+
IS_DEP_GRAPH,
131+
IS_MODULE_FILE))
117132
&& Iterables.any(cycle, Predicates.or(IS_REPO_RULE, IS_EXTENSION_IMPL))) {
118133
StringBuilder cycleMessage =
119134
new StringBuilder(
@@ -127,7 +142,10 @@ public boolean maybeReportCycle(
127142
IS_EXTENSION_IMPL,
128143
IS_BZL_LOAD,
129144
IS_REPO_MAPPING,
130-
IS_WORKSPACE_FILE));
145+
IS_WORKSPACE_FILE,
146+
IS_MODULE_RESOLUTION,
147+
IS_DEP_GRAPH,
148+
IS_MODULE_FILE));
131149
Function<Object, String> printer =
132150
rawInput -> {
133151
SkyKey input = (SkyKey) rawInput;
@@ -144,6 +162,12 @@ public boolean maybeReportCycle(
144162
return String.format("repository mapping of %s", key.repoName());
145163
} else if (input.argument() instanceof WorkspaceFileValue.WorkspaceFileKey) {
146164
return "WORKSPACE file";
165+
} else if (input.argument() == BazelModuleResolutionValue.KEY) {
166+
return "module resolution";
167+
} else if (input.argument() == BazelDepGraphValue.KEY) {
168+
return "module dependency graph";
169+
} else if (input.argument() instanceof ModuleFileValue.Key) {
170+
return "module file of " + input.argument();
147171
} else {
148172
Preconditions.checkArgument(input.argument() instanceof BzlLoadValue.Key);
149173
return ((BzlLoadValue.Key) input.argument()).getLabel().toString();

0 commit comments

Comments
 (0)