## Summary

`tvm_ffi.libinfo._find_library_by_basename(package, target_name)` already
accepts a `package` parameter, but the dev-mode fallback search anchored
its candidate paths on `tvm_ffi/libinfo.py`'s own `__file__` (via
`_rel_top_directory()` / `_dev_top_directory()`). Foreign callers, e.g.
TVM passing `package="tvm"`, therefore walked the tvm_ffi tree instead
of their own and had to ship a separate dev-mode lookup.

This PR resolves the calling package's filesystem root via
`importlib.util.find_spec(package)` and anchors the candidate list on
that directory:

```
package_root / "lib"
package_root / "build" / "lib"
package_root.parent / "build" / "lib"
package_root.parent / "lib"
package_root.parent.parent / "build" / "lib"
package_root.parent.parent / "lib"
```

## Notes

- **Distribution name vs import name.** `package` is the *distribution*
  name (e.g. `apache-tvm-ffi`) for the RECORD walk, while `find_spec`
  wants the *import* name (e.g. `tvm_ffi`). When direct resolution
  fails, the implementation reads `top_level.txt` from the dist-info
  to map dist name -> import name and retries `find_spec`.
- **Back-compat.** If the package can't be resolved, the original
  `_rel_top_directory` / `_dev_top_directory` candidates are still
  searched, so the self-call from `find_libtvm_ffi` keeps working in
  unusual environments.
- **Latent bug fix.** The unconditional `im.distribution(package)` at
  the top of the function used to raise `PackageNotFoundError` when
  the dist wasn't installed (e.g. `python/tvm_ffi` placed directly on
  `PYTHONPATH`), aborting before the dev fallback could run. The
  RECORD walk is now wrapped in `try/except (PackageNotFoundError, OSError)`
  so the fallback is reachable.
- **Downstream cleanup.** Once this lands and downstream submodules
  bump, `_load_lib_dev_fallback` in apache/tvm#19443 becomes deletable.

## Test plan

- [x] `pytest -vvs tests/python/test_libinfo.py` (15 passed, including
      2 new tests).
- [x] `pytest tests/python` (full Python suite: 2299 passed; 3
      pre-existing CUDA/ROCm failures unrelated to libinfo).
- [x] `pre-commit run --all-files` clean.

New tests:

- `test_find_library_by_basename_uses_calling_package_root`: builds a
  fake `tmp_path/python/fakepkg/` package and a
  `tmp_path/build/lib/lib<target>.{so,dylib,dll}` (per platform),
  prepends the package parent to `sys.path`, and asserts the resolved
  library lives under the temp tree, not under tvm_ffi's tree.
- `test_find_library_by_basename_self_call_unchanged`: regression test
  for `package="apache-tvm-ffi"` against `find_libtvm_ffi()`.
You can view, comment on, or merge this pull request online at:

  https://github.com/apache/tvm-ffi/pull/570

-- Commit Summary --

  * [REFACTOR] libinfo: anchor dev fallback on calling package root

-- File Changes --

    M python/tvm_ffi/libinfo.py (96)
    M tests/python/test_libinfo.py (52)

-- Patch Links --

https://github.com/apache/tvm-ffi/pull/570.patchhttps://github.com/apache/tvm-ffi/pull/570.diff
-- 
Reply to this email directly or view it on GitHub:
https://github.com/apache/tvm-ffi/pull/570
You are receiving this because you are subscribed to this thread.

Message ID: <apache/tvm-ffi/pull/[email protected]>

Reply via email to