diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java index ee4b74b280076e..5f94e4b114451c 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java @@ -177,9 +177,6 @@ private static void createStubFile( Substitution.of("%is_zipfile%", boolToLiteral(isForZipFile)), Substitution.of( "%import_all%", boolToLiteral(bazelConfig.getImportAllRepositories())), - Substitution.of( - "%enable_host_version_warning%", - boolToLiteral(common.shouldWarnAboutHostVersionUponFailure())), Substitution.of("%target%", ruleContext.getRule().getLabel().getCanonicalForm()), Substitution.of( "%python_version_from_config%", versionToLiteral(common.getVersion())), diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt index e6dc9392e7a3d9..eb31c8dd1e4a48 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/python/python_stub_template.txt @@ -205,84 +205,6 @@ def RunfilesEnvvar(module_space): return (None, None) -# TODO(#6443): Remove this once there's no longer a host configuration for -# Python targets to appear in. -def MaybeEmitHostVersionWarning(ret_code): - """Warns the user if a failure may be due to the host config's version. - - This emits a message to stderr if - 1) ret_code is non-zero, - 2) the target was built in the host config and with toolchains enabled, and - 3) at analysis time we detected a mismatch between the host config's version - and this target's explicitly declared version, or else this target did - not explicitly declare its version. (The former diagnoses targets - affected by #6443, and the latter diagnoses targets that are broken by - fixing #4815.) - - See also #7899, #8549, and PyCommon#shouldWarnAboutHostVersionUponFailure. - - Since this warning is emitted here in the stub script and not in Bazel itself, - it will be present in all failing runs of affected targets, even when executed - directly and not via `bazel run`. However, note that this warning is never - added to non-host-configured targets, and that it can be disabled by ensuring - the correct Python version is passed to --host_force_python and declared in - tools' python_version attributes. - - Args: - ret_code: The exit code of the payload user program - """ - if ret_code == 0: - return - if not %enable_host_version_warning%: - return - - host_version = %python_version_from_config% - target_version = %python_version_from_attr% - opposite_of_host_version = '2' if host_version == '3' else '3' - - if %python_version_specified_explicitly%: - # Mismatch with explicitly declared version. - diagnostic = """\ -Note: The failure of target {target} (with exit code {ret_code}) may have been \ -caused by the fact that it is a Python {target_version} program that was built \ -in the host configuration, which uses Python {host_version}. You can change \ -the host configuration (for the entire build) to instead use Python \ -{target_version} by setting --host_force_python=PY{target_version}.\ -""".format( - target='%target%', - ret_code=ret_code, - target_version=target_version, - host_version=host_version) - else: - diagnostic = """\ -Note: The failure of target {target} (with exit code {ret_code}) may have been \ -caused by the fact that it is running under Python {host_version} instead of \ -Python {opposite_of_host_version}. Examine the error to determine if that \ -appears to be the problem. Since this target is built in the host \ -configuration, the only way to change its version is to set \ ---host_force_python=PY{opposite_of_host_version}, which affects the entire \ -build.\ -""".format( - target='%target%', - ret_code=ret_code, - host_version=host_version, - opposite_of_host_version=opposite_of_host_version) - - # TODO(brandjon): Change the wording "You are likely seeing this message - # because" to something less strong after a few releases from 0.27. By that - # point, migration for toolchains won't be the main reason this error is seen - # by users. - message = """\ ----------------- -{diagnostic} - -If this error started occurring in Bazel 0.27 and later, it may be because the \ -Python toolchain now enforces that targets analyzed as PY2 and PY3 run under a \ -Python 2 and Python 3 interpreter, respectively. See \ -https://github.com/bazelbuild/bazel/issues/7899 for more information. -----------------""".format(diagnostic=diagnostic) - print(message, file=sys.stderr) - def Deduplicate(items): """Efficiently filter out duplicates, keeping the first element only.""" seen = set() @@ -379,19 +301,12 @@ def Main(): os.chdir(os.path.join(module_space, '%workspace_name%')) ret_code = subprocess.call(args) shutil.rmtree(os.path.dirname(module_space), True) - MaybeEmitHostVersionWarning(ret_code) sys.exit(ret_code) else: # On Windows, os.execv doesn't handle arguments with spaces correctly, # and it actually starts a subprocess just like subprocess.call. - # - # If we may need to emit a host config warning after execution, don't - # execv because we need control to return here. This only happens for - # targets built in the host config, so other targets still get to take - # advantage of the performance benefits of execv. - if IsWindows() or %enable_host_version_warning%: + if IsWindows(): ret_code = subprocess.call(args) - MaybeEmitHostVersionWarning(ret_code) sys.exit(ret_code) else: os.execv(args[0], args) diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java index a1b5ce196dc113..be1fe8afa8dc55 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java @@ -580,68 +580,6 @@ public PythonVersion getSourcesVersion() { return sourcesVersion; } - /** - * Returns whether, in the case that a user Python program fails, the stub script should emit a - * warning that the failure may have been caused by the host configuration using the wrong Python - * version. - * - *
This method should only be called for executable Python rules. - * - *
Background: Historically, Bazel did not necessarily launch a Python interpreter whose - * version corresponded to the one determined by the analysis phase (#4815). Enabling Python - * toolchains fixed this bug. However, this caused some builds to break due to targets that - * contained Python-2-only code yet got analyzed for (and now run with) Python 3. This is - * particularly problematic for the host configuration, where the value of {@code - * --host_force_python} overrides the declared or implicit Python version of the target. - * - *
Our mitigation for this is to warn users when a Python target has a non-zero exit code and - * the failure could be due to a bad Python version in the host configuration. In this case, - * instead of just giving the user a confusing traceback of a PY2 vs PY3 error, we append a - * diagnostic message to stderr. See #7899 and especially #8549 for context. - * - *
This method returns true when all of the following hold: - * - *