-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let Starlark rules specify their environment via RunEnvironmentInfo
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.
- Loading branch information
Showing
19 changed files
with
418 additions
and
119 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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
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 com.google.devtools.build.lib.starlarkbuildapi.test.TestEnvironmentInfoApi; | ||
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; | ||
|
||
@Immutable | ||
public final class RunEnvironmentInfo extends NativeInfo implements RunEnvironmentInfoApi, | ||
TestEnvironmentInfoApi { | ||
|
||
/** | ||
* 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 Map<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")), | ||
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
63 changes: 0 additions & 63 deletions
63
src/main/java/com/google/devtools/build/lib/analysis/test/TestEnvironmentInfo.java
This file was deleted.
Oops, something went wrong.
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.