From 7a51c60806ca8f76a5956bcade22b0d38dc1884e Mon Sep 17 00:00:00 2001 From: Perry Gibson Date: Wed, 1 Apr 2026 11:32:15 +0100 Subject: [PATCH 1/3] test: add test for library loading when importlib.metadata unavailable This test verifies that rtree can still locate and load the bundled libspatialindex library when importlib.metadata.files() returns None, which happens in sandboxed environments like Bazel. --- tests/test_finder.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_finder.py b/tests/test_finder.py index 4a05ad2f..266717e5 100644 --- a/tests/test_finder.py +++ b/tests/test_finder.py @@ -1,5 +1,7 @@ +import sys from ctypes import CDLL from pathlib import Path +from unittest import mock from rtree import finder @@ -9,6 +11,22 @@ def test_load(): assert isinstance(lib, CDLL) +def test_load_without_importlib_metadata(): + """Test that library loading works when importlib.metadata.files() is unavailable. + + This can happen in sandboxed environments like Bazel where package metadata + isn't properly exposed. + """ + # Reload finder module with mocked importlib.metadata.files returning None + with mock.patch("importlib.metadata.files", return_value=None): + # Need to reload the module to re-run the load logic + import importlib + + importlib.reload(finder) + lib = finder.load() + assert isinstance(lib, CDLL) + + def test_get_include(): incl = finder.get_include() assert isinstance(incl, str) From db3964bb5f78dba1a8fa29a51527b7b56a48670d Mon Sep 17 00:00:00 2001 From: Perry Gibson Date: Wed, 1 Apr 2026 11:32:31 +0100 Subject: [PATCH 2/3] fix: add fallback for finding bundled library when importlib.metadata unavailable When running in sandboxed environments like Bazel, importlib.metadata.files() may return None or fail to locate files correctly. This adds a fallback that searches for the bundled libspatialindex library relative to the package directory (in rtree.libs/), which works regardless of metadata availability. --- rtree/finder.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rtree/finder.py b/rtree/finder.py index 0c9c67ed..9b7b6355 100644 --- a/rtree/finder.py +++ b/rtree/finder.py @@ -86,6 +86,14 @@ def load() -> ctypes.CDLL: except importlib.metadata.PackageNotFoundError: pass + # Fallback: search relative to package directory + # (works when importlib.metadata is unavailable, e.g. Bazel, some venvs) + libs_dir = _cwd.parent / "rtree.libs" + if libs_dir.exists(): + for so_file in libs_dir.glob("libspatialindex*.so"): + _candidates.insert(1, so_file) + break + for cand in _candidates: if cand.is_dir(): # if our candidate is a directory use best guess From 6157fbbebaab7a4b69ec5e58cc8a4ddc3552d135 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 10:35:49 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_finder.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_finder.py b/tests/test_finder.py index 266717e5..230f05be 100644 --- a/tests/test_finder.py +++ b/tests/test_finder.py @@ -1,4 +1,3 @@ -import sys from ctypes import CDLL from pathlib import Path from unittest import mock