Skip to content

Commit 14a1036

Browse files
ctcjabclaude
andauthored
test(e2e): cover nanobind_extension against the PBS py_cc toolchain (#1299)
Adds an e2e case that builds a real `nanobind_extension` in a graph whose only Python toolchains come from `python_interpreters` (the rules_python `python.toolchain()` drop-in), exercising the `@rules_python//python/cc:current_py_cc_headers` path that nanobind and pybind11 use to resolve Python headers. `pbs-cc-toolchain` already covers the bare `cc_binary` + `current_py_cc_headers`/`current_py_cc_libs` path; this adds the **nanobind** consumer, which is how most real-world native extensions reach `current_py_cc_headers`. It guards the py_cc toolchain registration (issue #1095) against regression. Two targets: - `example_ext_build_test` — a `build_test` over the `nanobind_extension`; fails at analysis if `python_interpreters` does not register `@rules_python//python/cc:toolchain_type`. - `example_ext_test` — compiles, links, loads and calls the extension under a PBS runtime (py 3.13). **End-user visible:** no (test-only). **Breaking:** no. **Test plan:** `bazel test //nanobind-py-cc/...` in `e2e/cases` passes locally (build_test + runtime import/call; Linux x86_64, llvm toolchain). Context: came out of testing the py_cc gap for a real nanobind consumer (discussed in #1131); @jbedard suggested opening this. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b9296a9 commit 14a1036

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

e2e/cases/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bazel_dep(name = "tar.bzl", version = "0.10.1")
99
bazel_dep(name = "rules_python", version = "1.9.0")
1010
bazel_dep(name = "platforms", version = "1.0.0")
1111
bazel_dep(name = "llvm", version = "0.8.3")
12+
bazel_dep(name = "nanobind_bazel", version = "2.12.0")
1213
bazel_dep(name = "rules_oci", version = "2.2.7")
1314
bazel_dep(name = "rules_shell", version = "0.4.1")
1415

e2e/cases/MODULE.bazel.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
load("@aspect_rules_py//py:defs.bzl", "py_test")
2+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
3+
load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension")
4+
5+
# Regression coverage for https://github.com/aspect-build/rules_py/issues/1095:
6+
# a nanobind_extension resolves Python headers through
7+
# @rules_python//python/cc:current_py_cc_headers, so it only analyzes if
8+
# python_interpreters registers the py_cc toolchain over the PBS interpreter.
9+
10+
nanobind_extension(
11+
name = "example_ext",
12+
srcs = ["example_ext.cpp"],
13+
)
14+
15+
build_test(
16+
name = "example_ext_build_test",
17+
targets = [":example_ext"],
18+
)
19+
20+
py_test(
21+
name = "example_ext_test",
22+
size = "small",
23+
srcs = ["example_ext_test.py"],
24+
imports = ["."], # so `import example_ext` finds the .so
25+
main = "example_ext_test.py",
26+
python_version = "3.13",
27+
deps = [":example_ext"],
28+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <nanobind/nanobind.h>
2+
#include <nanobind/stl/string.h>
3+
4+
// A minimal nanobind extension. See BUILD.bazel for what it guards (issue #1095).
5+
NB_MODULE(example_ext, m) {
6+
m.def("add", [](int a, int b) { return a + b; });
7+
m.def("greet", [](const std::string &name) { return "Hello, " + name + "!"; });
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import example_ext
2+
3+
assert example_ext.add(2, 3) == 5, example_ext.add(2, 3)
4+
assert example_ext.greet("World") == "Hello, World!", example_ext.greet("World")
5+
6+
print("nanobind extension imported and called successfully")

0 commit comments

Comments
 (0)