From e23e2f0dcb033d2345927a0834ba33abd3e08b1f Mon Sep 17 00:00:00 2001 From: Vedant Bhayani Date: Fri, 9 Oct 2026 11:23:38 +0530 Subject: [PATCH] Start test environment from host env instead of build env --- cibuildwheel/platforms/macos.py | 11 ++++++++++- cibuildwheel/platforms/windows.py | 10 +++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cibuildwheel/platforms/macos.py b/cibuildwheel/platforms/macos.py index 21d48e10d..96678e0b5 100644 --- a/cibuildwheel/platforms/macos.py +++ b/cibuildwheel/platforms/macos.py @@ -650,6 +650,14 @@ def build(options: Options, tmp_path: Path) -> None: else: testing_archs = ["x86_64"] + # Start the test environment again from the host environment, + # rather than the build environment, so that build-specific + # variables (such as _PYTHON_HOST_PLATFORM, ARCHFLAGS and + # SDKROOT) don't leak into the test environment. See #1863. + # The user's `environment` option still applies, since it + # covers the build and test phases. + test_base_env = build_options.environment.as_dictionary(prev_environment=os.environ) + for testing_arch in testing_archs: if config_is_universal2: arch_specific_identifier = f"{config.identifier}:{testing_arch}" @@ -718,7 +726,7 @@ def build(options: Options, tmp_path: Path) -> None: venv_dir, None, use_uv=use_uv, - env=env, + env=test_base_env, pip_version=pip_version, ) if use_uv: @@ -727,6 +735,7 @@ def build(options: Options, tmp_path: Path) -> None: pip_install = functools.partial(call_with_arch, *pip, "install") virtualenv_env["MACOSX_DEPLOYMENT_TARGET"] = get_test_macosx_deployment_target() + virtualenv_env["CIBUILDWHEEL_BUILD_IDENTIFIER"] = config.identifier virtualenv_env = build_options.test_environment.as_dictionary( prev_environment=virtualenv_env diff --git a/cibuildwheel/platforms/windows.py b/cibuildwheel/platforms/windows.py index 364cae3af..f1b44a8e0 100644 --- a/cibuildwheel/platforms/windows.py +++ b/cibuildwheel/platforms/windows.py @@ -595,6 +595,13 @@ def build(options: Options, tmp_path: Path) -> None: # skip this test elif test_selected and build_options.test_command: log.step("Testing wheel...") + # Start the test environment again from the host environment, + # rather than the build environment, so that build-specific + # variables (such as PYTHON_VERSION and PYTHON_ARCH) don't + # leak into the test environment. See #1863. The user's + # `environment` option still applies, since it covers the + # build and test phases. + test_base_env = build_options.environment.as_dictionary(prev_environment=os.environ) # set up a virtual environment to install and test from, to make sure # there are no dependencies that were pulled in at build time. venv_dir = identifier_tmp_dir / "venv-test" @@ -604,10 +611,11 @@ def build(options: Options, tmp_path: Path) -> None: venv_dir, None, use_uv=use_uv, - env=env, + env=test_base_env, pip_version=pip_version, ) + virtualenv_env["CIBUILDWHEEL_BUILD_IDENTIFIER"] = config.identifier virtualenv_env = build_options.test_environment.as_dictionary( prev_environment=virtualenv_env )