facebook / facebook/buck2

cxx_python_extension references python_toolchain.extension_linker_flags, but system_python_toolchain does not define it

Open
#840 0 comments 1 reaction 0 assignees View on GitHub
prelude python
Dominant language
Rust
Stars
4.4k
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Summary

When using the built-in cxx_python_extension rule and the default system_python_toolchain, Buck2 fails during analysis because the rule references a field (extension_linker_flags) that the standard Python toolchain does not define. This leads to a type error (NoneType vs. list) whenever cxx_python_extension tries to read extension_linker_flags.

Steps to reproduce

Create a minimal BUCK2 project with a simple cxx_python_extension target and the default “system” Python toolchain:
```

cxx_python_extension(
name = "my_python_extension",
module_name = "my_python_extension",
srcs = ["my_extension.cpp"],
headers = ["my_extension.h"],
deps = [],
visibility = ["PUBLIC"],
)
```
In toolchains//BUCK (or wherever the system python toolchain is declared), define:
```

system_python_toolchain(
name = "python",
# no ex
tension_linker_flags param, just the defaults
)

```

`Run buck2 build //path/to:my_python_extension.
`
Observed behavior

Buck2 fails with an error similar to:

```
Value `None` of type `NoneType` does not match the type annotation `list` for argument `extra_link_flags`
--> prelude/python/cxx_python_extension.bzl:XXX
It points to python_toolchain.extension_linker_flags being None when cxx_python_extension tries to assign it to extra_link_flags.
```

If we try to fix this by passing extension_linker_flags = [] into system_python_toolchain, Buck2 immediately complains:

Found `extension_linker_flags` extra named parameter(s) for call to system_python_toolchain
indicating that system_python_toolchain does not accept or define that parameter.

Expected behavior

According to Buck2’s documentation, a simple cxx_python_extension target should “just work” out of the box with no special configuration. The rule’s reference to extension_linker_flags should either:

Be optional (and default to an empty list) if not defined in the Python toolchain, or
Be included as a recognized parameter in system_python_toolchain.
Additional context

Buck2 version: (please fill in or “latest stable at time of writing”).
Platform: e.g. Ubuntu 22.04, macOS 13.x, etc.
The mismatch between cxx_python_extension.bzl (which uses python_toolchain.extension_linker_flags) and system_python_toolchain (which does not define that field) breaks the default usage scenario.
Workarounds include creating a custom Python toolchain that defines extension_linker_flags, or patching cxx_python_extension to default to an empty list if the field is None.
Proposed fix

Option A: Update cxx_python_extension to handle None gracefully, e.g.:
extra_link_flags = python_toolchain.extension_linker_flags or []
Option B: Modify system_python_toolchain so it always provides (or allows specifying) extension_linker_flags, defaulting to [].
Option C: Remove or conditionally skip the code that references extension_linker_flags if it’s not set.
Thank you for taking a look at this!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.