Fix #688: routes/controllers auto-import crash when app is installed as a package#689
Open
pctablet505 wants to merge 1 commit into
Open
Fix #688: routes/controllers auto-import crash when app is installed as a package#689pctablet505 wants to merge 1 commit into
pctablet505 wants to merge 1 commit into
Conversation
…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
marked this pull request as ready for review
July 17, 2026 12:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
import_child_modules(used by theroutes/andcontrollers/folder auto-import feature) builds the dotted module path withos.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: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/controllersfolder 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.pythat reproduces the reported scenario: a package folder with aroutessubpackage, imported while the process cwd is an unrelated directory. Verified manually against the issue's exact repro (uv tool installstyle layout) that the crash is fixed and the route is registered correctly, and confirmed the previous code reproduces the sameModuleNotFoundErrorin the same harness.Full test suite passes locally (1923 passed, 1 skipped, unrelated).
Fixes #688.