Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/cases/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bazel_dep(name = "tar.bzl", version = "0.10.1")
bazel_dep(name = "rules_python", version = "1.9.0")
bazel_dep(name = "platforms", version = "1.0.0")
bazel_dep(name = "llvm", version = "0.8.3")
bazel_dep(name = "nanobind_bazel", version = "2.12.0")
bazel_dep(name = "rules_oci", version = "2.2.7")
bazel_dep(name = "rules_rust", version = "0.68.1")
bazel_dep(name = "rules_shell", version = "0.4.1")
Expand Down
44 changes: 44 additions & 0 deletions e2e/cases/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions e2e/cases/nanobind-py-cc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
load("@aspect_rules_py//py:defs.bzl", "py_test")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension")

# Regression coverage for https://github.com/aspect-build/rules_py/issues/1095.
#
# `python_interpreters` is positioned as a drop-in for rules_python's
# `python.toolchain()`. For native Python extensions to build, it must register a
# `@rules_python//python/cc:toolchain_type` (py_cc) toolchain over the PBS
# interpreter: nanobind and pybind11 resolve their Python headers through
# `@rules_python//python/cc:current_py_cc_headers`, which consumes that toolchain
# type. Before the registration landed, analysis of any nanobind_extension in a
# graph whose only Python toolchains come from `python_interpreters` failed with:
#
# No matching toolchains found for types:
# @@rules_python+//python/cc:toolchain_type
#
# `pbs-cc-toolchain` already covers the bare `cc_binary` + `current_py_cc_headers`
# / `current_py_cc_libs` path. This case adds coverage for the nanobind consumer,
# which is how most real-world native extensions reach `current_py_cc_headers`.

nanobind_extension(
name = "example_ext",
srcs = ["example_ext.cpp"],
)

# Core guard: a nanobind_extension only *analyzes* if python_interpreters
# registers the py_cc toolchain. This build_test fails at analysis otherwise,
# regardless of runtime import-path details.
build_test(
name = "example_ext_build_test",
targets = [":example_ext"],
)

# End-to-end: compile + link + load + call the extension under a PBS runtime.
py_test(
name = "example_ext_test",
size = "small",
srcs = ["example_ext_test.py"],
imports = ["."], # put this package dir on sys.path so `import example_ext` finds the .so
main = "example_ext_test.py",
python_version = "3.13",
deps = [":example_ext"],
)
12 changes: 12 additions & 0 deletions e2e/cases/nanobind-py-cc/example_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>

// A minimal nanobind extension. nanobind resolves the Python headers it needs
// through @rules_python//python/cc:current_py_cc_headers, which consumes the
// @rules_python//python/cc:toolchain_type (py_cc) toolchain. In a graph whose
// only Python toolchains come from python_interpreters, that toolchain must be
// registered by python_interpreters itself or this fails to build (issue #1095).
NB_MODULE(example_ext, m) {
m.def("add", [](int a, int b) { return a + b; });
m.def("greet", [](const std::string &name) { return "Hello, " + name + "!"; });
}
6 changes: 6 additions & 0 deletions e2e/cases/nanobind-py-cc/example_ext_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import example_ext

assert example_ext.add(2, 3) == 5, example_ext.add(2, 3)
assert example_ext.greet("World") == "Hello, World!", example_ext.greet("World")

print("nanobind extension imported and called successfully")
Loading