Skip to content

Commit c3245cd

Browse files
fmeumcopybara-github
authored andcommitted
Add SpellChecker suggestions for common Bzlmod errors
Closes #17121. PiperOrigin-RevId: 500606458 Change-Id: I3a4c3650e8f89401669f1d5e941073ed603673db
1 parent 0696ba3 commit c3245cd

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ java_library(
170170
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
171171
"//src/main/java/net/starlark/java/annot",
172172
"//src/main/java/net/starlark/java/eval",
173+
"//src/main/java/net/starlark/java/spelling",
173174
"//src/main/java/net/starlark/java/syntax",
174175
"//src/main/protobuf:failure_details_java_proto",
175176
"//third_party:auto_value",

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package com.google.devtools.build.lib.bazel.bzlmod;
1717

1818
import static com.google.common.collect.ImmutableBiMap.toImmutableBiMap;
19+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1920

21+
import com.google.common.collect.ImmutableSet;
2022
import com.google.devtools.build.lib.analysis.BlazeDirectories;
2123
import com.google.devtools.build.lib.bazel.repository.downloader.DownloadManager;
2224
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
@@ -51,6 +53,7 @@
5153
import net.starlark.java.eval.StarlarkList;
5254
import net.starlark.java.eval.StarlarkSemantics;
5355
import net.starlark.java.eval.StarlarkThread;
56+
import net.starlark.java.spelling.SpellChecker;
5457
import net.starlark.java.syntax.Location;
5558

5659
/**
@@ -147,13 +150,19 @@ public SkyValue compute(SkyKey skyKey, Environment env)
147150
// Check that the .bzl file actually exports a module extension by our name.
148151
Object exported = bzlLoadValue.getModule().getGlobal(extensionId.getExtensionName());
149152
if (!(exported instanceof ModuleExtension.InStarlark)) {
153+
ImmutableSet<String> exportedExtensions =
154+
bzlLoadValue.getModule().getGlobals().entrySet().stream()
155+
.filter(e -> e.getValue() instanceof ModuleExtension.InStarlark)
156+
.map(Entry::getKey)
157+
.collect(toImmutableSet());
150158
throw new SingleExtensionEvalFunctionException(
151159
ExternalDepsException.withMessage(
152160
Code.BAD_MODULE,
153-
"%s does not export a module extension called %s, yet its use is requested at %s",
161+
"%s does not export a module extension called %s, yet its use is requested at %s%s",
154162
extensionId.getBzlFileLabel(),
155163
extensionId.getExtensionName(),
156-
sampleUsageLocation),
164+
sampleUsageLocation,
165+
SpellChecker.didYouMean(extensionId.getExtensionName(), exportedExtensions)),
157166
Transience.PERSISTENT);
158167
}
159168

@@ -206,12 +215,14 @@ public SkyValue compute(SkyKey skyKey, Environment env)
206215
ExternalDepsException.withMessage(
207216
Code.BAD_MODULE,
208217
"module extension \"%s\" from \"%s\" does not generate repository \"%s\", yet it"
209-
+ " is imported as \"%s\" in the usage at %s",
218+
+ " is imported as \"%s\" in the usage at %s%s",
210219
extensionId.getExtensionName(),
211220
extensionId.getBzlFileLabel(),
212221
repoImport.getValue(),
213222
repoImport.getKey(),
214-
usage.getLocation()),
223+
usage.getLocation(),
224+
SpellChecker.didYouMean(
225+
repoImport.getValue(), threadContext.getGeneratedRepos().keySet())),
215226
Transience.PERSISTENT);
216227
}
217228
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.starlark.java.annot.StarlarkBuiltin;
2525
import net.starlark.java.eval.EvalException;
2626
import net.starlark.java.eval.Structure;
27+
import net.starlark.java.spelling.SpellChecker;
2728

2829
/**
2930
* A {@link Tag} whose attribute values have been type-checked against the attribute schema define
@@ -48,9 +49,10 @@ public static TypeCheckedTag create(TagClass tagClass, Tag tag, LabelConverter l
4849
if (attrIndex == null) {
4950
throw ExternalDepsException.withMessage(
5051
Code.BAD_MODULE,
51-
"in tag at %s, unknown attribute %s provided",
52+
"in tag at %s, unknown attribute %s provided%s",
5253
tag.getLocation(),
53-
attrValue.getKey());
54+
attrValue.getKey(),
55+
SpellChecker.didYouMean(attrValue.getKey(), tagClass.getAttributeIndices().keySet()));
5456
}
5557
Attribute attr = tagClass.getAttributes().get(attrIndex);
5658
Object nativeValue;

0 commit comments

Comments
 (0)