@@ -271,9 +271,6 @@ site of the rule. Such attributes can be assigned a default value (as in
271
271
A rule symbol, macro symbol, or the name of a built-in common attribute list (see below) from which
272
272
the macro should inherit attributes.
273
273
274
- <p>If <code>inherit_attrs</code> is set, the macro's implementation function <em>must</em> have a
275
- <code>**kwargs</code> residual keyword parameter.
276
-
277
274
<p>If <code>inherit_attrs</code> is set to the string <code>"common"</code>, the macro will inherit
278
275
<a href="/reference/be/common-definitions#common-attributes">common rule attribute definitions</a>
279
276
used by all Starlark rules.
@@ -282,29 +279,41 @@ site of the rule. Such attributes can be assigned a default value (as in
282
279
a global variable in a .bzl file, then such a value has not been registered as a rule or macro
283
280
symbol, and therefore cannot be used for <code>inherit_attrs</code>.
284
281
285
- <p>By convention, a macro should pass inherited, non-overridden attributes unchanged to the "main"
286
- rule or macro symbol which the macro is wrapping. Typically, most inherited attributes will not have
287
- a parameter in the implementation function's parameter list, and will simply be passed via
288
- <code>**kwargs</code>. However, it may be convenient for the implementation function to have
289
- explicit parameters for some inherited attributes (most commonly, <code>tags</code> and
290
- <code>testonly</code>) if the macro needs to pass those attributes to both "main" and non-"main"
291
- targets.
292
-
293
282
<p>The inheritance mechanism works as follows:</p>
294
283
<ol>
295
284
<li>The special <code>name</code> and <code>visibility</code> attributes are never inherited;
296
285
<li>Hidden attributes (ones whose name starts with <code>"_"</code>) are never inherited;
297
- <li>The remaining inherited attributes are merged with the <code>attrs</code> dictionary, with
298
- the entries in <code>attrs</code> dictionary taking precedence in case of conflicts.
286
+ <li>Attributes whose names are already defined in the <code>attrs</code> dictionary are never
287
+ inherited (the entry in <code>attrs</code> takes precedence; note that an entry may be set to
288
+ <code>None</code> to ensure that no attribute by that name gets defined on the macro);
289
+ <li>All other attributes are inherited from the rule or macro and effectively merged into the
290
+ <code>attrs</code> dict.
299
291
</ol>
300
292
301
- <p>For example, the following macro inherits all attributes from <code>native.cc_library</code>, except
302
- for <code>cxxopts</code> (which is removed from the attribute list) and <code>copts</code> (which is
303
- given a new definition):
293
+ <p>When a non-mandatory attribute is inherited, the default value of the attribute is overridden
294
+ to be <code>None</code>, regardless of what it was specified in the original rule or macro. This
295
+ ensures that when the macro forwards the attribute's value to an instance of the wrapped rule or
296
+ macro – such as by passing in the unmodified <code>**kwargs</code> – a value that was
297
+ absent from the outer macro's call will also be absent in the inner rule or macro's call (since
298
+ passing <code>None</code> to an attribute is treated the same as omitting the attribute).
299
+ This is important because omitting an attribute has subtly different semantics from passing
300
+ its apparent default value. In particular, omitted attributes are not shown in some <code>bazel
301
+ query</code> output formats, and computed defaults only execute when the value is omitted. If the
302
+ macro needs to examine or modify an inherited attribute – for example, to add a value to an
303
+ inherited <code>tags</code> attribute – you must make sure to handle the <code>None</code>
304
+ case in the macro's implementation function.
305
+
306
+ <p>For example, the following macro inherits all attributes from <code>native.cc_library</code>,
307
+ except for <code>cxxopts</code> (which is removed from the attribute list) and <code>copts</code>
308
+ (which is given a new definition). It also takes care to checks for the default <code>None</code>
309
+ value of the inherited <code>tags</code> attribute before appending an additional tag.
304
310
305
311
<pre class="language-python">
306
- def _my_cc_library_impl(name, visibility, **kwargs):
307
- ...
312
+ def _my_cc_library_impl(name, visibility, tags, **kwargs):
313
+ # Append a tag; tags attr is inherited from native.cc_library, and therefore is None unless
314
+ # explicitly set by the caller of my_cc_library()
315
+ my_tags = (tags or []) + ["my_custom_tag"]
316
+ native.cc_library(name = name, visibility = visibility, tags = my_tags, **kwargs)
308
317
309
318
my_cc_library = macro(
310
319
implementation = _my_cc_library_impl,
@@ -316,12 +325,17 @@ def _my_cc_library_impl(name, visibility, **kwargs):
316
325
)
317
326
</pre>
318
327
319
- <p>Note that a macro may inherit a non-hidden attribute with a computed default (for example,
320
- <a href="/reference/be/common-definitions#common.testonly"><code>testonly</code></a>); normally,
321
- macros do not allow attributes with computed defaults. If such an attribute is unset in a macro
322
- invocation, the value passed to the implementation function will be <code>None</code>, and the
323
- <code>None</code> may be safely passed on to the corresponding attribute of a rule target, causing
324
- the rule to compute the default as expected.
328
+ <p>If <code>inherit_attrs</code> is set, the macro's implementation function <em>must</em> have a
329
+ <code>**kwargs</code> residual keyword parameter.
330
+
331
+ <p>By convention, a macro should pass inherited, non-overridden attributes unchanged to the "main"
332
+ rule or macro symbol which the macro is wrapping. Typically, most inherited attributes will not have
333
+ a parameter in the implementation function's parameter list, and will simply be passed via
334
+ <code>**kwargs</code>. It can be convenient for the implementation function to have explicit
335
+ parameters for some inherited attributes (most commonly, <code>tags</code> and
336
+ <code>testonly</code>) if the macro needs to pass those attributes to both "main" and non-"main"
337
+ targets – but if the macro also needs to examine or manipulate those attributes, you must take
338
+ care to handle the <code>None</code> default value of non-mandatory inherited attributes.
325
339
""" ),
326
340
// TODO: #19922 - Make a concepts page for symbolic macros, migrate some details like the
327
341
// list of disallowed APIs to there.
0 commit comments