Skip to content

py_extension on Windows passes both CPython import libraries as one linker argument #4426

Open

Description

@TrapsterDK

What happened

On Windows, py_extension fails to link when the Python toolchain's libpython holds both import libraries, which is the case for the hermetic interpreters from python.toolchain (python3.lib and python312.lib).

py_extension_macro.bzl passes the libs with

user_link_flags = ["$(locations :_<name>_win_libs)"]

cc_shared_library does not split that entry, so link.exe or lld-link get one argument with two paths:

lld-link: error: could not open 'external/.../libs/python3.lib external/.../libs/python312.lib': no such file or directory

Repro

Windows (x86_64 or arm64), rules_python 2.4.6, bzlmod with a registered python.toolchain(python_version = "3.12"):

load("@rules_python//python/cc:py_extension.bzl", "py_extension")

py_extension(
    name = "ext",
    srcs = ["ext.cc"],
)

bazel build //:ext fails at the link step as above. main and 2.5.0-rc0 have the same code.

Proposed fix

Link one import library: the stable ABI one (python3.lib, python3t.lib) when py_limited_api is set, else the versioned one. Pass py_limited_api from the macro to py_extension_libs and filter there:

abi3_files = [f for f in link_files if f.basename in ("python3.lib", "python3t.lib")]
if ctx.attr.py_limited_api:
    link_files = abi3_files or link_files
else:
    link_files = [f for f in link_files if f not in abi3_files] or link_files

We carry this as a patch downstream and can send a PR with an analysis test.

Assisted by Claude.

Activity

  1. TrapsterDK commented on Nov 7, 2026

    @TrapsterDK
    Author

    PR: #4427

    Assisted by Claude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions