forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 1
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
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
- Loading branch information
Showing
19 changed files
with
440 additions
and
140 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
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
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
Oops, something went wrong.