Skip to content

Commit

Permalink
Improve use_repo_rule error when not referencing a repository_rule
Browse files Browse the repository at this point in the history
Makes the error message less confusing when referencing an existing symbol that happens to be a macro, not a repository rule.

Closes #20657.

PiperOrigin-RevId: 595434847
Change-Id: Ifc37a65685c0196301d79a439f3245037cf39e21
  • Loading branch information
fmeum authored and copybara-github committed Jan 3, 2024
1 parent b0722bc commit 69fa6cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public RunModuleExtensionResult run(
// Instiantiate the repos one by one.
for (InnateExtensionRepo repo : repos) {
Object exported = repo.loadedBzl().getModule().getGlobal(repo.ruleName());
if (!(exported instanceof RepositoryRuleFunction)) {
if (exported == null) {
ImmutableSet<String> exportedRepoRules =
repo.loadedBzl().getModule().getGlobals().entrySet().stream()
.filter(e -> e.getValue() instanceof RepositoryRuleFunction)
Expand All @@ -636,6 +636,17 @@ public RunModuleExtensionResult run(
repo.tag().getLocation(),
SpellChecker.didYouMean(repo.ruleName(), exportedRepoRules)),
Transience.PERSISTENT);
} else if (!(exported instanceof RepositoryRuleFunction)) {
throw new SingleExtensionEvalFunctionException(
ExternalDepsException.withMessage(
Code.BAD_MODULE,
"%s exports a value called %s of type %s, yet a repository_rule is requested"
+ " at %s",
repo.bzlLabel(),
repo.ruleName(),
Starlark.type(exported),
repo.tag().getLocation()),
Transience.PERSISTENT);
}
RepositoryRuleFunction repoRule = (RepositoryRuleFunction) exported;
Dict<String, Object> kwargs = repo.tag().getAttributeValues().attributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,35 @@ public void innate_noSuchRepoRule() throws Exception {
"load('@data//:data.bzl', self_data='data')",
"data=self_data");
scratch.file(
workspaceRoot.getRelative("repo.bzl").getPathString(), "data_repo = 3 # not a repo rule");
workspaceRoot.getRelative("repo.bzl").getPathString(),
"# not a repo rule",
"def data_repo(name):",
" pass");

SkyKey skyKey = BzlLoadValue.keyForBuild(Label.parseCanonical("//:data.bzl"));
reporter.removeHandler(failFastHandler);
EvaluationResult<BzlLoadValue> result =
evaluator.evaluate(ImmutableList.of(skyKey), evaluationContext);
assertThat(result.hasError()).isTrue();
assertThat(result.getError().getException())
.hasMessageThat()
.contains(
"//:repo.bzl exports a value called data_repo of type function, yet a repository_rule"
+ " is requested at /ws/MODULE.bazel");
}

@Test
public void innate_noSuchValue() throws Exception {
scratch.file(
workspaceRoot.getRelative("MODULE.bazel").getPathString(),
"data_repo = use_repo_rule('//:repo.bzl', 'data_repo')",
"data_repo(name='data', data='get up at 6am.')");
scratch.file(workspaceRoot.getRelative("BUILD").getPathString());
scratch.file(
workspaceRoot.getRelative("data.bzl").getPathString(),
"load('@data//:data.bzl', self_data='data')",
"data=self_data");
scratch.file(workspaceRoot.getRelative("repo.bzl").getPathString(), "");

SkyKey skyKey = BzlLoadValue.keyForBuild(Label.parseCanonical("//:data.bzl"));
reporter.removeHandler(failFastHandler);
Expand Down

0 comments on commit 69fa6cb

Please sign in to comment.