Skip to content

Commit 14925b5

Browse files
Wyveraldcomius
andauthored
Always use target's attributes to set Python version (bazelbuild#16959)
Fixes: bazelbuild#16935 RELNOTES[INC]: This changes the behavior of Python version in exec/host configuration. Mitigation is to set Python version on the targets. PiperOrigin-RevId: 493804390 Change-Id: I3a4d787e7075d2b76835faf04d4c4e04c9de85b4 Co-authored-by: Googler <[email protected]>
1 parent cccb0d6 commit 14925b5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ public String getTypeDescription() {
248248
+ "https://github.com/bazelbuild/bazel/issues/10076.")
249249
public boolean incompatibleDefaultToExplicitInitPy;
250250

251+
// Helper field to store hostForcePython in exec configuration
252+
private PythonVersion defaultPythonVersion = null;
253+
251254
@Override
252255
public Map<OptionDefinition, SelectRestriction> getSelectRestrictions() {
253256
// TODO(brandjon): Instead of referencing the python_version target, whose path depends on the
@@ -256,7 +259,7 @@ public Map<OptionDefinition, SelectRestriction> getSelectRestrictions() {
256259
restrictions.put(
257260
PYTHON_VERSION_DEFINITION,
258261
new SelectRestriction(
259-
/*visibleWithinToolsPackage=*/ true,
262+
/* visibleWithinToolsPackage= */ true,
260263
"Use @bazel_tools//python/tools:python_version instead."));
261264
restrictions.put(
262265
FORCE_PYTHON_DEFINITION,
@@ -276,6 +279,9 @@ public Map<OptionDefinition, SelectRestriction> getSelectRestrictions() {
276279
* a version should be built for.
277280
*/
278281
public PythonVersion getDefaultPythonVersion() {
282+
if (defaultPythonVersion != null) {
283+
return defaultPythonVersion;
284+
}
279285
return incompatiblePy3IsDefault ? PythonVersion.PY3 : PythonVersion.PY2;
280286
}
281287

@@ -320,8 +326,11 @@ public void setPythonVersion(PythonVersion version) {
320326
@Override
321327
public FragmentOptions getHost() {
322328
PythonOptions hostPythonOptions = (PythonOptions) getDefault();
323-
PythonVersion hostVersion =
324-
(hostForcePython != null) ? hostForcePython : getDefaultPythonVersion();
329+
PythonVersion hostVersion = getDefaultPythonVersion();
330+
if (hostForcePython != null) {
331+
hostVersion = hostForcePython;
332+
hostPythonOptions.defaultPythonVersion = hostForcePython;
333+
}
325334
hostPythonOptions.setPythonVersion(hostVersion);
326335
hostPythonOptions.incompatiblePy3IsDefault = incompatiblePy3IsDefault;
327336
hostPythonOptions.incompatiblePy2OutputsAreSuffixed = incompatiblePy2OutputsAreSuffixed;

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.devtools.build.lib.analysis.config.BuildOptions;
2222
import com.google.devtools.build.lib.analysis.config.BuildOptionsCache;
2323
import com.google.devtools.build.lib.analysis.config.BuildOptionsView;
24-
import com.google.devtools.build.lib.analysis.config.CoreOptions;
2524
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
2625
import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition;
2726
import com.google.devtools.build.lib.events.EventHandler;
@@ -69,17 +68,12 @@ private PythonVersionTransition() {}
6968

7069
@Override
7170
public ImmutableSet<Class<? extends FragmentOptions>> requiresOptionFragments() {
72-
return ImmutableSet.of(PythonOptions.class, CoreOptions.class);
71+
return ImmutableSet.of(PythonOptions.class);
7372
}
7473

7574
@Override
7675
public BuildOptions patch(BuildOptionsView options, EventHandler eventHandler) {
77-
// If this happens after exec transition, keep the same version (to reproduce and keep behaviour
78-
// of the host transition, that happens after this one)
79-
PythonVersion newVersion =
80-
options.get(CoreOptions.class).isExec
81-
? options.get(PythonOptions.class).getPythonVersion()
82-
: determineNewVersion(options);
76+
PythonVersion newVersion = determineNewVersion(options);
8377
checkArgument(newVersion.isTargetValue(), newVersion);
8478

8579
PythonOptions opts = options.get(PythonOptions.class);

0 commit comments

Comments
 (0)