|
28 | 28 | import com.google.common.collect.Interner;
|
29 | 29 | import com.google.common.collect.Iterables;
|
30 | 30 | import com.google.common.util.concurrent.ListenableFuture;
|
| 31 | +import com.google.common.collect.Sets; |
31 | 32 | import com.google.devtools.build.lib.actions.AbstractAction;
|
32 | 33 | import com.google.devtools.build.lib.actions.ActionContinuationOrResult;
|
33 | 34 | import com.google.devtools.build.lib.actions.ActionEnvironment;
|
@@ -683,7 +684,6 @@ public static class Builder {
|
683 | 684 | private final List<Artifact> outputs = new ArrayList<>();
|
684 | 685 | private final List<RunfilesSupplier> inputRunfilesSuppliers = new ArrayList<>();
|
685 | 686 | private ResourceSetOrBuilder resourceSetOrBuilder = AbstractAction.DEFAULT_RESOURCE_SET;
|
686 |
| - private ActionEnvironment actionEnvironment = null; |
687 | 687 | private ImmutableMap<String, String> environment = ImmutableMap.of();
|
688 | 688 | private ImmutableSet<String> inheritedEnvironment = ImmutableSet.of();
|
689 | 689 | private ImmutableMap<String, String> executionInfo = ImmutableMap.of();
|
@@ -717,7 +717,6 @@ public Builder(Builder other) {
|
717 | 717 | this.outputs.addAll(other.outputs);
|
718 | 718 | this.inputRunfilesSuppliers.addAll(other.inputRunfilesSuppliers);
|
719 | 719 | this.resourceSetOrBuilder = other.resourceSetOrBuilder;
|
720 |
| - this.actionEnvironment = other.actionEnvironment; |
721 | 720 | this.environment = other.environment;
|
722 | 721 | this.executionInfo = other.executionInfo;
|
723 | 722 | this.isShellCommand = other.isShellCommand;
|
@@ -762,12 +761,31 @@ public SpawnAction build(ActionOwner owner, BuildConfigurationValue configuratio
|
762 | 761 | result.addCommandLine(pair);
|
763 | 762 | }
|
764 | 763 | CommandLines commandLines = result.build();
|
765 |
| - ActionEnvironment env = |
766 |
| - actionEnvironment != null |
767 |
| - ? actionEnvironment |
768 |
| - : useDefaultShellEnvironment |
769 |
| - ? configuration.getActionEnvironment() |
770 |
| - : ActionEnvironment.create(environment, inheritedEnvironment); |
| 764 | + ActionEnvironment env; |
| 765 | + if (useDefaultShellEnvironment && environment != null) { |
| 766 | + // Inherited variables override fixed variables in ActionEnvironment. Since we want the |
| 767 | + // fixed part of the action-provided environment to override the inherited part of the |
| 768 | + // user-provided environment, we have to explicitly filter the inherited part. |
| 769 | + var userFilteredInheritedEnv = |
| 770 | + ImmutableSet.copyOf( |
| 771 | + Sets.difference( |
| 772 | + configuration.getActionEnvironment().getInheritedEnv(), environment.keySet())); |
| 773 | + // Do not create a new ActionEnvironment in the common case where no vars have been filtered |
| 774 | + // out. |
| 775 | + if (userFilteredInheritedEnv.equals( |
| 776 | + configuration.getActionEnvironment().getInheritedEnv())) { |
| 777 | + env = configuration.getActionEnvironment(); |
| 778 | + } else { |
| 779 | + env = |
| 780 | + ActionEnvironment.create( |
| 781 | + configuration.getActionEnvironment().getFixedEnv(), userFilteredInheritedEnv); |
| 782 | + } |
| 783 | + env = env.withAdditionalFixedVariables(environment); |
| 784 | + } else if (useDefaultShellEnvironment) { |
| 785 | + env = configuration.getActionEnvironment(); |
| 786 | + } else { |
| 787 | + env = ActionEnvironment.create(environment, inheritedEnvironment); |
| 788 | + } |
771 | 789 | return buildSpawnAction(
|
772 | 790 | owner, commandLines, configuration.getCommandLineLimits(), configuration, env);
|
773 | 791 | }
|
@@ -984,13 +1002,6 @@ public Builder setResources(ResourceSetOrBuilder resourceSetOrBuilder) {
|
984 | 1002 | return this;
|
985 | 1003 | }
|
986 | 1004 |
|
987 |
| - /** Sets the action environment. */ |
988 |
| - @CanIgnoreReturnValue |
989 |
| - public Builder setEnvironment(ActionEnvironment actionEnvironment) { |
990 |
| - this.actionEnvironment = actionEnvironment; |
991 |
| - return this; |
992 |
| - } |
993 |
| - |
994 | 1005 | /**
|
995 | 1006 | * Sets the map of environment variables. Do not use! This makes the builder ignore the 'default
|
996 | 1007 | * shell environment', which is computed from the --action_env command line option.
|
@@ -1075,6 +1086,18 @@ public Builder executeUnconditionally() {
|
1075 | 1086 | return this;
|
1076 | 1087 | }
|
1077 | 1088 |
|
| 1089 | + /** |
| 1090 | + * Same as {@link #useDefaultShellEnvironment()}, but additionally sets the provided fixed |
| 1091 | + * environment variables, which take precedence over the variables contained in the default |
| 1092 | + * shell environment. |
| 1093 | + */ |
| 1094 | + @CanIgnoreReturnValue |
| 1095 | + public Builder useDefaultShellEnvironmentWithOverrides(Map<String, String> environment) { |
| 1096 | + this.environment = ImmutableMap.copyOf(environment); |
| 1097 | + this.useDefaultShellEnvironment = true; |
| 1098 | + return this; |
| 1099 | + } |
| 1100 | + |
1078 | 1101 | /**
|
1079 | 1102 | * Sets the executable path; the path is interpreted relative to the execution root, unless it's
|
1080 | 1103 | * a bare file name.
|
|
0 commit comments