Skip to content

Fix CLI 404: replace importlib.resources.files() with pathlib.__file__ for package directory resolution#23

Merged
pearsonkyle merged 2 commits intofeature/cli-tool-and-docsfrom
copilot/sub-pr-21-again
Mar 13, 2026
Merged

Fix CLI 404: replace importlib.resources.files() with pathlib.__file__ for package directory resolution#23
pearsonkyle merged 2 commits intofeature/cli-tool-and-docsfrom
copilot/sub-pr-21-again

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

importlib.resources.files("docbuddy") behaves inconsistently across Python versions and install methods — str() on the returned traversable can resolve to unexpected paths, causing the HTTP server to serve the wrong directory and standalone.html to return 404.

Changes

  • cli.py: Replace importlib.resources.files() with pathlib.Path(__file__).parent, which unconditionally resolves to the installed package directory. Extract into _pkg_dir() helper for testability. Drop the now-unused importlib.resources import.
# Before — unreliable on some Python versions / install methods
pkg_ref = files("docbuddy")
pkg_dir = str(pkg_ref)  # str() may not yield a valid filesystem path

# After — always points to the directory containing the installed cli.py
def _pkg_dir() -> pathlib.Path:
    return pathlib.Path(__file__).parent
  • tests/test_plugin.py: Update CLI tests to monkeypatch _pkg_dir instead of files. Use tmp_path fixture for filesystem-based error-path testing. Add assertion that directory= kwarg passed to SimpleHTTPRequestHandler matches the expected package path.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…to resolve package directory

Co-authored-by: pearsonkyle <4556546+pearsonkyle@users.noreply.github.com>
Copilot AI changed the title [WIP] Add docbuddy command to launch local docs webpage Fix CLI 404: replace importlib.resources.files() with pathlib.__file__ for package directory resolution Mar 13, 2026
Copilot AI requested a review from pearsonkyle March 13, 2026 06:20
@pearsonkyle pearsonkyle marked this pull request as ready for review March 13, 2026 14:30
@pearsonkyle pearsonkyle merged commit 98a2c9a into feature/cli-tool-and-docs Mar 13, 2026
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.

2 participants