Skip to content

Commit

Permalink
Fix bug in subrules that permitted a subrule to call another without …
Browse files Browse the repository at this point in the history
…declaring a dependency on it

PiperOrigin-RevId: 580162487
Change-Id: Id44e0b7e957113efcc7e72a96e762fb035411225
  • Loading branch information
hvadehra authored and copybara-github committed Nov 7, 2023
1 parent b1184e7 commit 804c503
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ void setLockedForSubrule(boolean isLocked) {
this.lockedForSubruleEvaluation = isLocked;
}

boolean getLockedForSubrule() {
return lockedForSubruleEvaluation;
}

/**
* Represents `ctx.outputs`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public Object call(StarlarkThread thread, Tuple args, Dict<String, Object> kwarg
BazelRuleAnalysisThreadContext.fromOrFail(thread, getName())
.getRuleContext()
.getStarlarkRuleContext();
if (ruleContext.getLockedForSubrule()) {
throw Starlark.errorf("subrules cannot call other subrules");
}
ImmutableSet<? extends StarlarkSubruleApi> declaredSubrules = ruleContext.getSubrules();
if (!declaredSubrules.contains(this)) {
throw getUndeclaredSubruleError(ruleContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ public void testSubrule_aspectMustDeclareSubrule() throws Exception {
+ " declare '_my_subrule' in 'subrules'");
}

// this is a bug that must be fixed
@Test
public void testSubruleCallingSibling_succeeds() throws Exception {
public void testSubruleCallingUndeclaredSibling_fails() throws Exception {
scratch.file(
"subrule_testing/myrule.bzl",
"def _subrule1_impl(ctx):",
Expand All @@ -175,11 +174,13 @@ public void testSubruleCallingSibling_succeeds() throws Exception {
"load('myrule.bzl', 'my_rule')",
"my_rule(name = 'foo')");

StructImpl provider =
getProvider("//subrule_testing:foo", "//subrule_testing:myrule.bzl", "MyInfo");
AssertionError error =
assertThrows(AssertionError.class, () -> getConfiguredTarget("//subrule_testing:foo"));

assertThat(provider).isNotNull();
assertThat(provider.getValue("result")).isEqualTo("result from subrule1");
assertThat(error).isNotNull();
assertThat(error)
.hasMessageThat()
.contains("Error in _my_subrule1: subrules cannot call other subrules");
}

@Test
Expand Down

0 comments on commit 804c503

Please sign in to comment.