Skip to content

fix: add fallback for finding bundled library when importlib.metadata unavailable#404

Open
Wheest wants to merge 3 commits into
Toblerity:mainfrom
Wheest:fix-importlib-metadata-fallback
Open

fix: add fallback for finding bundled library when importlib.metadata unavailable#404
Wheest wants to merge 3 commits into
Toblerity:mainfrom
Wheest:fix-importlib-metadata-fallback

Conversation

@Wheest
Copy link
Copy Markdown

@Wheest Wheest commented Apr 1, 2026

Summary

When running in sandboxed environments like Bazel (with rules_python), importlib.metadata.files() may return None or fail to locate files correctly, even though the bundled libspatialindex library exists in the wheel.

This PR adds a fallback that searches for the bundled library relative to the package directory (rtree.libs/), which works regardless of metadata availability.

Problem

In Bazel's sandboxed test environment:

OSError: Could not load libspatialindex_c library

The library exists at rtree.libs/libspatialindex-*.so but importlib.metadata.files("rtree") returns None because Bazel doesn't properly expose package metadata.

Solution

After the existing importlib.metadata lookup, add a simple fallback:

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

This uses the already-defined _cwd (Path(__file__).parent) to locate the library relative to the package, which works in any environment.

Testing

  • Added test that mocks importlib.metadata.files() returning None
  • Verified fix works in Bazel environment (commit 1 fails, commit 2 passes)

Wheest and others added 3 commits April 1, 2026 11:32
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.
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant