Skip to content

Commit

Permalink
Proper error messages when built-in rule attributes are overridden ba…
Browse files Browse the repository at this point in the history
…zelbuild#1811

--
MOS_MIGRATED_REVID=135241715
  • Loading branch information
vladmos authored and damienmg committed Oct 5, 2016
1 parent 39bb51a commit da57492
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public Metadata getMetadata() {
*/
public static RuleClass.Builder commonCoreAndSkylarkAttributes(RuleClass.Builder builder) {
return builder
.add(attr("name", STRING)
.nonconfigurable("Rule name"))
// The visibility attribute is special: it is a nodep label, and loading the
// necessary package groups is handled by {@link LabelVisitor#visitTargetVisibility}.
// Package groups always have the null configuration so that they are not duplicated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public BaseFunction invoke(
// We'll set the name later, pass the empty string for now.
Builder builder = new Builder("", RuleClassType.WORKSPACE, true);

builder.addOrOverrideAttribute(attr("$local", BOOLEAN).defaultValue(local).build());
BaseRuleClasses.commonCoreAndSkylarkAttributes(builder);
builder.add(attr("expect_failure", STRING));
if (attrs != Runtime.NONE) {
for (Map.Entry<String, Descriptor> attr :
castMap(attrs, String.class, Descriptor.class, "attrs").entrySet()) {
Expand All @@ -119,9 +122,6 @@ public BaseFunction invoke(
builder.addOrOverrideAttribute(attrBuilder.build(attrName));
}
}
builder.addOrOverrideAttribute(attr("$local", BOOLEAN).defaultValue(local).build());
BaseRuleClasses.commonCoreAndSkylarkAttributes(builder);
builder.add(attr("expect_failure", STRING));
builder.setConfiguredTargetFunction(implementation);
builder.setRuleDefinitionEnvironment(funcallEnv);
builder.setWorkspaceOnly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,30 @@ public void testLoadDoesNotHideWorkspaceFunction() throws Exception {
invalidatePackages();
assertThat(getRuleContext(getConfiguredTarget("//:data")).getWorkspaceName()).isEqualTo("bleh");
}

@Test
public void testSkylarkRepositoryCannotOverrideBuiltInAttribute() throws Exception {
scratch.file(
"def.bzl",
"def _impl(ctx):",
" print(ctx.attr.name)",
"",
"repo = repository_rule(",
" implementation=_impl,",
" attrs={'name': attr.string(mandatory=True)})");
scratch.file(rootDirectory.getRelative("BUILD").getPathString());
scratch.overwriteFile(
rootDirectory.getRelative("WORKSPACE").getPathString(),
"load('//:def.bzl', 'repo')",
"repo(name='foo')");

invalidatePackages();
try {
getConfiguredTarget("@foo//:bar");
fail();
} catch (AssertionError e) {
assertThat(e.getMessage()).contains("There is already a built-in attribute 'name' "
+ "which cannot be overridden");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,27 @@ public void testCannotOverrideBuiltInAttribute() throws Exception {
Assert.fail("Expected error '"
+ "There is already a built-in attribute 'tags' which cannot be overridden"
+ "' but got no error");
} catch (IllegalArgumentException | EvalException e) {
} catch (EvalException e) {
assertThat(e).hasMessage(
"There is already a built-in attribute 'tags' which cannot be overridden");
}
}

@Test
public void testCannotOverrideBuiltInAttributeName() throws Exception {
ev.setFailFast(true);
try {
evalAndExport(
"def impl(ctx): return", "r = rule(impl, attrs = {'name': attr.string()})");
Assert.fail("Expected error '"
+ "There is already a built-in attribute 'name' which cannot be overridden"
+ "' but got no error");
} catch (EvalException e) {
assertThat(e).hasMessage(
"There is already a built-in attribute 'name' which cannot be overridden");
}
}

@Test
public void testImplicitArgsAttribute() throws Exception {
evalAndExport(
Expand Down

0 comments on commit da57492

Please sign in to comment.