forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let Starlark executable rules specify their environment (bazelbuild#1…
…5766) * Specify fixedEnv/inheritedEnv interaction in ActionEnvironment Previously, ActionEnvironment did not publicly document how fixed and inherited environment variables interact, but still cautioned users to keep the two sets disjoint without enforcing this. As a result, neither could users rely on the interaction nor could ActionEnvironment benefit from the additional freedom of not specifying the behavior. With this commit, ActionEnvironment explicitly specifies that the values of environment variable inherited from the client environment take precedence over fixed values and codifies this behavior in a test. This has been the effective behavior all along and has the advantage that users can provide overrideable defaults for environment variables. Closes bazelbuild#15170. PiperOrigin-RevId: 439315634 * Intern trivial ActionEnvironment and EnvironmentVariables instances When an ActionEnvironment is constructed out of an existing one, the ActionEnvironment and EnvironmentVariables instances, which are immutable, can be reused if no variables are added. Also renames addVariables and addFixedVariables to better reflect that they are operating on an immutable type. Closes bazelbuild#15171. PiperOrigin-RevId: 440312159 * Let Starlark executable rules specify their environment The new RunEnvironmentInfo provider allows any executable or test Starlark rule to specify the environment for when it is executed, either as part of a test action or via the run command. Refactors testing.TestEnvironment to construct a RunEnvironmentInfo and adds a warning (but not an error) if the provider constructed in this way is returned from a non-executable non-test rule. If a RunEnvironmentInfo is constructed directly via the Starlark constructor, this warning becomes an error. Fixes bazelbuild#7364 Fixes bazelbuild#15224 Fixes bazelbuild#15225 Closes bazelbuild#15232. PiperOrigin-RevId: 448185352 * Fix strict deps violation ``` src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java:990: error: [strict] Using type com.google.devtools.build.lib.cmdline.LabelValidator from an indirect dependency (TOOL_INFO: "//src/main/java/com/google/devtools/build/lib/cmdline:LabelValidator"). See command below ** LabelValidator.parseAbsoluteLabel(labelString); ^ ```
- Loading branch information
Showing
24 changed files
with
519 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/google/devtools/build/lib/analysis/RunEnvironmentInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.analysis; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; | ||
import com.google.devtools.build.lib.packages.BuiltinProvider; | ||
import com.google.devtools.build.lib.packages.NativeInfo; | ||
import com.google.devtools.build.lib.starlarkbuildapi.RunEnvironmentInfoApi; | ||
import java.util.List; | ||
import java.util.Map; | ||
import net.starlark.java.eval.Dict; | ||
import net.starlark.java.eval.EvalException; | ||
import net.starlark.java.eval.Sequence; | ||
import net.starlark.java.eval.StarlarkList; | ||
|
||
/** | ||
* Provider containing any additional environment variables for use in the executable or test | ||
* action. | ||
*/ | ||
@Immutable | ||
public final class RunEnvironmentInfo extends NativeInfo implements RunEnvironmentInfoApi { | ||
|
||
/** Singleton instance of the provider type for {@link DefaultInfo}. */ | ||
public static final RunEnvironmentInfoProvider PROVIDER = new RunEnvironmentInfoProvider(); | ||
|
||
private final ImmutableMap<String, String> environment; | ||
private final ImmutableList<String> inheritedEnvironment; | ||
private final boolean shouldErrorOnNonExecutableRule; | ||
|
||
/** Constructs a new provider with the given fixed and inherited environment variables. */ | ||
public RunEnvironmentInfo( | ||
Map<String, String> environment, | ||
List<String> inheritedEnvironment, | ||
boolean shouldErrorOnNonExecutableRule) { | ||
this.environment = ImmutableMap.copyOf(Preconditions.checkNotNull(environment)); | ||
this.inheritedEnvironment = | ||
ImmutableList.copyOf(Preconditions.checkNotNull(inheritedEnvironment)); | ||
this.shouldErrorOnNonExecutableRule = shouldErrorOnNonExecutableRule; | ||
} | ||
|
||
@Override | ||
public RunEnvironmentInfoProvider getProvider() { | ||
return PROVIDER; | ||
} | ||
|
||
/** | ||
* Returns environment variables which should be set when the target advertising this provider is | ||
* executed. | ||
*/ | ||
@Override | ||
public ImmutableMap<String, String> getEnvironment() { | ||
return environment; | ||
} | ||
|
||
/** | ||
* Returns names of environment variables whose value should be inherited from the shell | ||
* environment when the target advertising this provider is executed. | ||
*/ | ||
@Override | ||
public ImmutableList<String> getInheritedEnvironment() { | ||
return inheritedEnvironment; | ||
} | ||
|
||
/** | ||
* Returns whether advertising this provider on a non-executable (and thus non-test) rule should | ||
* result in an error or a warning. The latter is required to not break testing.TestEnvironment, | ||
* which is now implemented via RunEnvironmentInfo. | ||
*/ | ||
public boolean shouldErrorOnNonExecutableRule() { | ||
return shouldErrorOnNonExecutableRule; | ||
} | ||
|
||
/** Provider implementation for {@link RunEnvironmentInfoApi}. */ | ||
public static class RunEnvironmentInfoProvider extends BuiltinProvider<RunEnvironmentInfo> | ||
implements RunEnvironmentInfoApi.RunEnvironmentInfoApiProvider { | ||
|
||
private RunEnvironmentInfoProvider() { | ||
super("RunEnvironmentInfo", RunEnvironmentInfo.class); | ||
} | ||
|
||
@Override | ||
public RunEnvironmentInfoApi constructor( | ||
Dict<?, ?> environment, Sequence<?> inheritedEnvironment) throws EvalException { | ||
return new RunEnvironmentInfo( | ||
Dict.cast(environment, String.class, String.class, "environment"), | ||
StarlarkList.immutableCopyOf( | ||
Sequence.cast(inheritedEnvironment, String.class, "inherited_environment")), | ||
/* shouldErrorOnNonExecutableRule */ true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.