Skip to content

Fix #688: routes/controllers auto-import crash when app is installed as a package#689

Open
pctablet505 wants to merge 1 commit into
Neoteroi:mainfrom
pctablet505:fix-688-routes-auto-import-cwd
Open

Fix #688: routes/controllers auto-import crash when app is installed as a package#689
pctablet505 wants to merge 1 commit into
Neoteroi:mainfrom
pctablet505:fix-688-routes-auto-import-cwd

Conversation

@pctablet505

Copy link
Copy Markdown

Problem

import_child_modules (used by the routes/ and controllers/ folder auto-import feature) builds the dotted module path with os.path.relpath(path), which resolves relative to the process's current working directory.

This works fine when the app is run from its own project folder, but breaks when the app is installed as a package (e.g. with uv tool install) and started from a different directory: the relative path is computed against the wrong base, producing a bogus dotted path and a crash like:

ModuleNotFoundError: No module named '.local'

Fix

Derive the dotted module path from the folder's own location on disk instead of the process cwd: walk up the parent directories of the routes/controllers folder while they contain __init__.py, building the fully qualified package name from that. This reflects the actual package structure and no longer depends on where the process happens to be started from.

Testing

Added a regression test in tests/test_utils.py that reproduces the reported scenario: a package folder with a routes subpackage, imported while the process cwd is an unrelated directory. Verified manually against the issue's exact repro (uv tool install style layout) that the crash is fixed and the route is registered correctly, and confirmed the previous code reproduces the same ModuleNotFoundError in the same harness.

Full test suite passes locally (1923 passed, 1 skipped, unrelated).

Fixes #688.

…location, not cwd

import_child_modules used os.path.relpath(path) to build the dotted module
path for auto-imported routes/controllers, which resolves relative to
os.getcwd(). When an app is installed as a package (e.g. via `uv tool
install`) and started from an unrelated working directory, this produces a
bogus dotted path and raises ModuleNotFoundError instead of importing the
routes.

Build the dotted path from the folder's own location instead: walk up its
parent directories while they contain __init__.py, so the result reflects
the actual package structure and is independent of the process's cwd.
@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:47
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.

routes/ auto-import crashes when app is installed as a package (with uv tool)

1 participant