Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SpellChecker suggestions for common Bzlmod errors #17121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ java_library(
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/main/java/net/starlark/java/annot",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/spelling",
"//src/main/java/net/starlark/java/syntax",
"//src/main/protobuf:failure_details_java_proto",
"//third_party:auto_value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import static com.google.common.collect.ImmutableBiMap.toImmutableBiMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.bazel.repository.downloader.DownloadManager;
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
Expand Down Expand Up @@ -51,6 +53,7 @@
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.StarlarkSemantics;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.spelling.SpellChecker;
import net.starlark.java.syntax.Location;

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

Expand Down Expand Up @@ -206,12 +215,14 @@ public SkyValue compute(SkyKey skyKey, Environment env)
ExternalDepsException.withMessage(
Code.BAD_MODULE,
"module extension \"%s\" from \"%s\" does not generate repository \"%s\", yet it"
+ " is imported as \"%s\" in the usage at %s",
+ " is imported as \"%s\" in the usage at %s%s",
extensionId.getExtensionName(),
extensionId.getBzlFileLabel(),
repoImport.getValue(),
repoImport.getKey(),
usage.getLocation()),
usage.getLocation(),
SpellChecker.didYouMean(repoImport.getValue(),
threadContext.getGeneratedRepos().keySet())),
Transience.PERSISTENT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Structure;
import net.starlark.java.spelling.SpellChecker;

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