Skip to content

Commit bbc221f

Browse files
aiutocopybara-github
authored andcommitted
Improve the check for being a package metadata rule
This allows the refactoring which will happen after default_applicable_licenses is renamed to default_package_metadata. The current behavior is intended to prevent `applicable_licenses` from being set on a `license` rule. The required behavior is that we don't set `applicable_licenses` on any of the metadata rules. Future changes might have to take into account the ability to set the license for a tool rule within `rules_package`. For background, see https://docs.google.com/document/d/1uyJjkKbE8kV8EinakaR9q-Un25zCukhoH_dRBkWHSKQ/edit#heading=h.izpa22p82m6c Closes #16596. PiperOrigin-RevId: 485457037 Change-Id: Ifb105f646ae0c291a6841b3ddb84ed536e9d71e3
1 parent 19b8d24 commit bbc221f

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static LicensesProvider of(RuleContext ruleContext) {
6868
ListMultimap<String, ? extends TransitiveInfoCollection> configuredMap =
6969
ruleContext.getConfiguredTargetMap();
7070

71-
if (rule.getRuleClassObject().isBazelLicense()) {
71+
if (rule.getRuleClassObject().isPackageMetadataRule()) {
7272
// Don't crawl a new-style license, it's effectively a leaf.
7373
// The representation of the new-style rule is unfortunately hardcoded here,
7474
// but this is code in the old-style licensing path that will ultimately be removed.

src/main/java/com/google/devtools/build/lib/packages/Rule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ public License getLicense() {
884884
// have old-style licenses. This is hardcoding the representation
885885
// of new-style rules, but it's in the old-style licensing code path
886886
// and will ultimately be removed.
887-
if (ruleClass.isBazelLicense()) {
887+
if (ruleClass.isPackageMetadataRule()) {
888888
return License.NO_LICENSE;
889889
} else if (isAttrDefined("licenses", BuildType.LICENSE)
890890
&& isAttributeValueExplicitlySpecified("licenses")) {

src/main/java/com/google/devtools/build/lib/packages/RuleClass.java

+40-24
Original file line numberDiff line numberDiff line change
@@ -2237,9 +2237,6 @@ private void populateDefaultRuleAttributeValues(
22372237

22382238
} else if (attr.getName().equals(APPLICABLE_LICENSES_ATTR)
22392239
&& attr.getType() == BuildType.LABEL_LIST) {
2240-
// TODO(b/149505729): Determine the right semantics for someone trying to define their own
2241-
// attribute named applicable_licenses.
2242-
//
22432240
// The check here is preventing against an corner case where the license() rule can get
22442241
// itself as an applicable_license. This breaks the graph because there is now a self-edge.
22452242
//
@@ -2262,22 +2259,7 @@ private void populateDefaultRuleAttributeValues(
22622259
// have the self-edge problem, they would get all default_applicable_licenses and now the
22632260
// graph is inconsistent in that some license() rules have applicable_licenses while others
22642261
// do not.
2265-
//
2266-
// Another possible workaround is to leverage the fact that license() rules instantiated
2267-
// before the package() rule will not get default_applicable_licenses applied, and the
2268-
// self-edge problem cannot occur in that case. The semantics for how package() should
2269-
// impact rules instantiated prior are not clear and not well understood. If this
2270-
// modification is distasteful, leveraging the package() behavior and clarifying the
2271-
// semantics is an option. It's not recommended since BUILD files are not thought to be
2272-
// order-dependent, but they have always been, so fixing that behavior may be more important
2273-
// than some unfortunate code here.
2274-
//
2275-
// Breaking the encapsulation to recognize license() rules and treat them uniformly results
2276-
// fixes the self-edge problem and results in the simplest, semantically
2277-
// correct graph.
2278-
//
2279-
// TODO(b/183637322) consider this further
2280-
if (rule.getRuleClassObject().isBazelLicense()) {
2262+
if (rule.getRuleClassObject().isPackageMetadataRule()) {
22812263
// Do nothing
22822264
} else {
22832265
rule.setAttributeValue(
@@ -2714,10 +2696,44 @@ public static boolean isThirdPartyPackage(PackageIdentifier packageIdentifier) {
27142696
&& packageIdentifier.getPackageFragment().isMultiSegment();
27152697
}
27162698

2717-
// Returns true if this rule is a license() rule as defined in
2718-
// https://docs.google.com/document/d/1uwBuhAoBNrw8tmFs-NxlssI6VRolidGYdYqagLqHWt8/edit#
2719-
// TODO(b/183637322) consider this further
2720-
public boolean isBazelLicense() {
2721-
return name.equals("_license") && hasAttr("license_kinds", BuildType.LABEL_LIST);
2699+
/**
2700+
* Returns true if this rule is a <code>license()</code> as described in
2701+
* https://docs.google.com/document/d/1uwBuhAoBNrw8tmFs-NxlssI6VRolidGYdYqagLqHWt8/edit# or
2702+
* similar metadata.
2703+
*
2704+
* <p>The intended use is to detect if this rule is of a type which would be used in <code>
2705+
* default_package_metadata</code>, so that we don't apply it to an instanced of itself when
2706+
* <code>applicable_licenses</code> is left unset. Doing so causes a self-referential loop. To
2707+
* prevent that, we are overly cautious at this time, treating all rules from <code>@rules_license
2708+
* </code> as potential metadata rules.
2709+
*
2710+
* <p>Most users will only use declarations from <code>@rules_license</code>. If they which to
2711+
* create organization local rules, they must be careful to avoid loops by explicitly setting
2712+
* <code>applicable_licenses</code> on each of the metadata targets they define, so that default
2713+
* processing is not an issue.
2714+
*/
2715+
public boolean isPackageMetadataRule() {
2716+
// If it was not defined in Starlark, it can not be a new style package metadata rule.
2717+
if (ruleDefinitionEnvironmentLabel == null) {
2718+
return false;
2719+
}
2720+
if (ruleDefinitionEnvironmentLabel.getRepository().getName().equals("rules_license")) {
2721+
// For now, we treat all rules in rules_license as potenial metadate rules.
2722+
// In the future we should add a way to disambiguate the two. The least invasive
2723+
// thing is to add a hidden attribute to mark metadata rules. That attribute
2724+
// could have a default value referencing @rules_license//<something>. That style
2725+
// of checking would allow users to apply it to their own metadata rules. We are
2726+
// not building it today because the exact needs are not clear.
2727+
return true;
2728+
}
2729+
// BEGIN-INTERNAL
2730+
// TODO(aiuto): This is a Google-ism, remove from Bazel.
2731+
String packageName = ruleDefinitionEnvironmentLabel.getPackageName();
2732+
if (packageName.startsWith("tools/build_defs/license")
2733+
|| packageName.startsWith("third_party/rules_license")) {
2734+
return true;
2735+
}
2736+
// END-INTERNAL
2737+
return false;
27222738
}
27232739
}

0 commit comments

Comments
 (0)