Skip to content

Commit f628997

Browse files
iancha1992fmeum
andauthored
[8.6.0] Fix visibility for implicit deps of parent rules (https://github.com/… (#28722)
/pull/28627) The visibility of a default value of an implicit dep in an extended rule should be checked relative to the definition of the rule that introduced it, which may not be the child rule. Fixes #28618 Closes #28627. PiperOrigin-RevId: 871303947 Change-Id: I0027e277dc9f01396fa4674297d2a73e1e9d257e <!-- Thank you for contributing to Bazel! Please read the contribution guidelines: https://bazel.build/contribute.html --> ### Description <!-- Please provide a brief summary of the changes in this PR. --> ### Motivation <!-- Why is this change important? Does it fix a specific bug or add a new feature? If this PR fixes an existing issue, please link it here (e.g. "Fixes #1234"). --> ### Build API Changes <!-- Does this PR affect the Build API? (e.g. Starlark API, providers, command-line flags, native rules) If yes, please answer the following: 1. Has this been discussed in a design doc or issue? (Please link it) 2. Is the change backward compatible? 3. If it's a breaking change, what is the migration plan? --> No ### Checklist - [ ] I have added tests for the new use cases (if any). - [ ] I have updated the documentation (if applicable). ### Release Notes <!-- If this is a new feature, please add 'RELNOTES[NEW]: <description>' here. If this is a breaking change, please add 'RELNOTES[INC]: <reason>' here. If this change should be mentioned in release notes, please add 'RELNOTES: <reason>' here. --> RELNOTES: None Commit a16489f --------- Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
1 parent bbb3a6e commit f628997

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/main/java/com/google/devtools/build/lib/analysis/CommonPrerequisiteValidator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,13 @@ private void validateDirectPrerequisiteVisibility(
135135
// Never null since we already checked that the aspect is Starlark-defined.
136136
implicitDefinition = checkNotNull(aspectClass.getExtensionLabel());
137137
} else {
138-
// Never null since we already checked that the rule is a Starlark rule.
139-
implicitDefinition =
140-
checkNotNull(rule.getRuleClassObject().getRuleDefinitionEnvironmentLabel());
138+
var owningRule = rule.getRuleClassObject();
139+
com.google.devtools.build.lib.packages.RuleClass parent;
140+
while ((parent = owningRule.getStarlarkParent()) != null
141+
&& parent.getAttributeByNameMaybe(attrName) != null) {
142+
owningRule = parent;
143+
}
144+
implicitDefinition = checkNotNull(owningRule.getRuleDefinitionEnvironmentLabel());
141145
}
142146
// Validate with respect to the defining .bzl.
143147
if (!isVisibleToLocation(prerequisite, implicitDefinition.getPackageIdentifier())) {

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5364,6 +5364,64 @@ def _impl(ctx):
53645364
"Error in rule: attribute `_tool`: private attributes cannot be overridden.");
53655365
}
53665366

5367+
@Test
5368+
public void extendRule_parentPrivateAttrDefault_visibilityCheckedAgainstParent()
5369+
throws Exception {
5370+
scratch.file(
5371+
"extend_rule_testing/parent/BUILD",
5372+
"""
5373+
exports_files(
5374+
["parent_tool.txt"],
5375+
visibility = ["//extend_rule_testing/parent:__pkg__"],
5376+
)
5377+
""");
5378+
scratch.file("extend_rule_testing/parent/parent_tool.txt");
5379+
scratch.file(
5380+
"extend_rule_testing/parent/parent.bzl",
5381+
"""
5382+
def _impl(ctx):
5383+
return []
5384+
5385+
parent_library = rule(
5386+
implementation = _impl,
5387+
extendable = True,
5388+
attrs = {
5389+
"_tool": attr.label(
5390+
allow_single_file = True,
5391+
default = "//extend_rule_testing/parent:parent_tool.txt",
5392+
),
5393+
},
5394+
)
5395+
""");
5396+
scratch.file(
5397+
"extend_rule_testing/child.bzl",
5398+
"""
5399+
load("//extend_rule_testing/parent:parent.bzl", "parent_library")
5400+
5401+
def _impl(ctx):
5402+
return ctx.super()
5403+
5404+
my_library = rule(
5405+
implementation = _impl,
5406+
parent = parent_library,
5407+
)
5408+
""");
5409+
scratch.file(
5410+
"extend_rule_testing/BUILD",
5411+
"""
5412+
load(":child.bzl", "my_library")
5413+
5414+
my_library(name = "my_target")
5415+
""");
5416+
5417+
// This should succeed because the visibility of the parent's private attribute default should
5418+
// be checked against the parent rule's package, not the child rule's package.
5419+
// See https://github.com/bazelbuild/bazel/issues/28618
5420+
getConfiguredTarget("//extend_rule_testing:my_target");
5421+
5422+
assertNoEvents();
5423+
}
5424+
53675425
@Test
53685426
public void extendRule_attributeOverrideDefault() throws Exception {
53695427
scratch.file("extend_rule_testing/parent/BUILD");

0 commit comments

Comments
 (0)