Skip to content

Commit 47d92b2

Browse files
Googlerulfjack
Googler
authored andcommitted
Only treat "env" and "env_inherit" attrs specially for native rules
This avoids unexpected behavior in the case of Skylark rules that happen to use the same attribute names. Fixes bazelbuild#12758 PiperOrigin-RevId: 349587545
1 parent a023c9f commit 47d92b2

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/main/java/com/google/devtools/build/docgen/templates/attributes/binary/env.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
executed by <code>bazel run</code>.
77
</p>
88

9+
<p>
10+
This attribute only applies to native rules, like <code>cc_binary</code>, <code>py_binary</code>,
11+
and <code>sh_binary</code>. It does not apply to Starlark-defined executable rules.
12+
</p>
13+
914
<p>
1015
<em class="harmful">NOTE: The environment variables are not set when you run the target
1116
outside of bazel (for example, by manually executing the binary in

src/main/java/com/google/devtools/build/docgen/templates/attributes/test/env.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
<p>Specifies additional environment variables to set when the test is
77
executed by <code>bazel test</code>.
88
</p>
9+
10+
<p>
11+
This attribute only applies to native rules, like <code>cc_test</code>, <code>py_test</code>,
12+
and <code>sh_test</code>. It does not apply to Starlark-defined test rules.
13+
</p>

src/main/java/com/google/devtools/build/docgen/templates/attributes/test/env_inherit.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
<p>Specifies additional environment variables to inherit from the
44
external environment when the test is executed by <code>bazel test</code>.
55
</p>
6+
7+
<p>
8+
This attribute only applies to native rules, like <code>cc_test</code>, <code>py_test</code>,
9+
and <code>sh_test</code>. It does not apply to Starlark-defined test rules.
10+
</p>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,14 @@ private static CommandLine computeArgs(RuleContext ruleContext, CommandLine addi
464464
}
465465

466466
private static ActionEnvironment computeActionEnvironment(RuleContext ruleContext) {
467-
if (!ruleContext.getRule().isAttrDefined("env", Type.STRING_DICT)
468-
&& !ruleContext.getRule().isAttrDefined("env_inherit", Type.STRING_LIST)) {
467+
// Currently, "env" and "env_inherit" are not added to Starlark-defined rules (unlike "args"),
468+
// in order to avoid breaking existing Starlark rules that use those attribute names.
469+
// TODO(brandjon): Support "env" and "env_inherit" for Starlark-defined rules.
470+
boolean isNativeRule =
471+
ruleContext.getRule().getRuleClassObject().getRuleDefinitionEnvironmentLabel() == null;
472+
if (!isNativeRule
473+
|| (!ruleContext.getRule().isAttrDefined("env", Type.STRING_DICT)
474+
&& !ruleContext.getRule().isAttrDefined("env_inherit", Type.STRING_LIST))) {
469475
return ActionEnvironment.EMPTY;
470476
}
471477
TreeMap<String, String> fixedEnv = new TreeMap<>();

0 commit comments

Comments
 (0)