From 14925b574aa63aad098f2c3d95e14b46ffd295ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?X=C3=B9d=C5=8Dng=20Y=C3=A1ng?= Date: Thu, 8 Dec 2022 11:20:05 -0500 Subject: [PATCH] Always use target's attributes to set Python version (#16959) Fixes: https://github.com/bazelbuild/bazel/issues/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 --- .../build/lib/rules/python/PythonOptions.java | 15 ++++++++++++--- .../lib/rules/python/PythonVersionTransition.java | 10 ++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonOptions.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonOptions.java index 357c9dc1ee5d0a..34c580e4f8796c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PythonOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonOptions.java @@ -248,6 +248,9 @@ public String getTypeDescription() { + "https://github.com/bazelbuild/bazel/issues/10076.") public boolean incompatibleDefaultToExplicitInitPy; + // Helper field to store hostForcePython in exec configuration + private PythonVersion defaultPythonVersion = null; + @Override public Map getSelectRestrictions() { // TODO(brandjon): Instead of referencing the python_version target, whose path depends on the @@ -256,7 +259,7 @@ public Map getSelectRestrictions() { restrictions.put( PYTHON_VERSION_DEFINITION, new SelectRestriction( - /*visibleWithinToolsPackage=*/ true, + /* visibleWithinToolsPackage= */ true, "Use @bazel_tools//python/tools:python_version instead.")); restrictions.put( FORCE_PYTHON_DEFINITION, @@ -276,6 +279,9 @@ public Map getSelectRestrictions() { * a version should be built for. */ public PythonVersion getDefaultPythonVersion() { + if (defaultPythonVersion != null) { + return defaultPythonVersion; + } return incompatiblePy3IsDefault ? PythonVersion.PY3 : PythonVersion.PY2; } @@ -320,8 +326,11 @@ public void setPythonVersion(PythonVersion version) { @Override public FragmentOptions getHost() { PythonOptions hostPythonOptions = (PythonOptions) getDefault(); - PythonVersion hostVersion = - (hostForcePython != null) ? hostForcePython : getDefaultPythonVersion(); + PythonVersion hostVersion = getDefaultPythonVersion(); + if (hostForcePython != null) { + hostVersion = hostForcePython; + hostPythonOptions.defaultPythonVersion = hostForcePython; + } hostPythonOptions.setPythonVersion(hostVersion); hostPythonOptions.incompatiblePy3IsDefault = incompatiblePy3IsDefault; hostPythonOptions.incompatiblePy2OutputsAreSuffixed = incompatiblePy2OutputsAreSuffixed; diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java b/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java index 481009df4700a3..df57fbf66b90da 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PythonVersionTransition.java @@ -21,7 +21,6 @@ import com.google.devtools.build.lib.analysis.config.BuildOptions; import com.google.devtools.build.lib.analysis.config.BuildOptionsCache; import com.google.devtools.build.lib.analysis.config.BuildOptionsView; -import com.google.devtools.build.lib.analysis.config.CoreOptions; import com.google.devtools.build.lib.analysis.config.FragmentOptions; import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition; import com.google.devtools.build.lib.events.EventHandler; @@ -69,17 +68,12 @@ private PythonVersionTransition() {} @Override public ImmutableSet> requiresOptionFragments() { - return ImmutableSet.of(PythonOptions.class, CoreOptions.class); + return ImmutableSet.of(PythonOptions.class); } @Override public BuildOptions patch(BuildOptionsView options, EventHandler eventHandler) { - // If this happens after exec transition, keep the same version (to reproduce and keep behaviour - // of the host transition, that happens after this one) - PythonVersion newVersion = - options.get(CoreOptions.class).isExec - ? options.get(PythonOptions.class).getPythonVersion() - : determineNewVersion(options); + PythonVersion newVersion = determineNewVersion(options); checkArgument(newVersion.isTargetValue(), newVersion); PythonOptions opts = options.get(PythonOptions.class);