Skip to content

Commit 0f5dfb4

Browse files
brandjonsiberex
authored andcommitted
Expose PyRuntimeInfo from py_binary and py_test rules
This allows targets that depend on an executable Python target to see what runtime was chosen from the toolchain. The returned `PyRuntimeInfo` provider is the same type of value as what's returned by the `py_runtime` rule. For example, the path to the system interpreter (if not using an in-workspace interpreter) can be accessed from a dependency as `mydep[PyRuntimeInfo].interpreter_path`. The Python major version for a target can also indirectly be obtained from the version of its runtime, as `mydep[PyRuntimeInfo].python_version`. Fixes bazelbuild#7805. RELNOTES: None PiperOrigin-RevId: 254212408
1 parent 15276cd commit 0f5dfb4

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java

+6
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ public Artifact getPythonStubArtifactForWindows(Artifact executable) {
825825
public void addCommonTransitiveInfoProviders(
826826
RuleConfiguredTargetBuilder builder, NestedSet<Artifact> filesToBuild) {
827827

828+
// Add PyInfo and/or legacy "py" struct provider.
828829
boolean createLegacyPyProvider =
829830
!ruleContext.getFragment(PythonConfiguration.class).disallowLegacyPyProvider();
830831
PyProviderUtils.builder(createLegacyPyProvider)
@@ -835,6 +836,11 @@ public void addCommonTransitiveInfoProviders(
835836
.setHasPy3OnlySources(hasPy3OnlySources)
836837
.buildAndAddToTarget(builder);
837838

839+
// Add PyRuntimeInfo if this is an executable rule.
840+
if (runtimeFromToolchain != null) {
841+
builder.addNativeDeclaredProvider(runtimeFromToolchain);
842+
}
843+
838844
builder
839845
.addNativeDeclaredProvider(
840846
InstrumentedFilesCollector.collect(

src/test/java/com/google/devtools/build/lib/rules/python/PyExecutableConfiguredTargetTestBase.java

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ private String ruleDeclWithPyVersionAttr(String name, String version) {
129129
")");
130130
}
131131

132+
@Test
133+
public void pyRuntimeInfoIsPresent() throws Exception {
134+
useConfiguration("--incompatible_use_python_toolchains=true");
135+
scratch.file(
136+
"pkg/BUILD", //
137+
ruleName + "(",
138+
" name = 'foo',",
139+
" srcs = [':foo.py'],",
140+
")");
141+
assertThat(getConfiguredTarget("//pkg:foo").get(PyRuntimeInfo.PROVIDER)).isNotNull();
142+
}
143+
132144
@Test
133145
public void oldVersionAttr_UnknownValue() throws Exception {
134146
useConfiguration("--incompatible_remove_old_python_version_api=false");

src/test/java/com/google/devtools/build/lib/rules/python/PyLibraryConfiguredTargetTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public PyLibraryConfiguredTargetTest() {
3333
super("py_library");
3434
}
3535

36+
@Test
37+
public void pyRuntimeInfoIsNotPresent() throws Exception {
38+
useConfiguration("--incompatible_use_python_toolchains=true");
39+
scratch.file(
40+
"pkg/BUILD", //
41+
"py_library(",
42+
" name = 'foo',",
43+
" srcs = [':foo.py'],",
44+
")");
45+
assertThat(getConfiguredTarget("//pkg:foo").get(PyRuntimeInfo.PROVIDER)).isNull();
46+
}
47+
3648
@Test
3749
public void canBuildWithIncompatibleSrcsVersionUnderNewSemantics() throws Exception {
3850
// See PyBaseConfiguredTargetTestBase for the analogous test under the old semantics, which

0 commit comments

Comments
 (0)