Skip to content

Commit 316488d

Browse files
authored
Fix import module order issue (#652)
1 parent 5858de8 commit 316488d

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

aiohttp_devtools/runserver/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def import_app_factory(self) -> AppFactory:
136136
rel_py_file = self.py_file.relative_to(self.python_path)
137137
module_path = '.'.join(rel_py_file.with_suffix('').parts)
138138

139-
sys.path.append(str(self.python_path))
139+
sys.path.insert(0, str(self.python_path))
140140
module = import_module(module_path)
141141
# Rewrite the package name, so it will appear the same as running the app.
142142
if module.__package__:

tests/test_runserver_main.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
from aiohttp import ClientTimeout
88
from pytest_toolbox import mktree
99

10+
from multiprocessing import set_start_method
11+
1012
from aiohttp_devtools.runserver import runserver
1113
from aiohttp_devtools.runserver.config import Config
1214
from aiohttp_devtools.runserver.serve import (
1315
WS, create_auxiliary_app, create_main_app, modify_main_app, src_reload, start_main_app)
16+
from aiohttp_devtools.runserver.watch import AppTask
1417

1518
from .conftest import SIMPLE_APP, forked
1619

@@ -112,6 +115,42 @@ async def hello(request):
112115
assert len(aux_app.cleanup_ctx) == 1
113116

114117

118+
@forked
119+
def test_start_runserver_with_multi_app_modules(tmpworkdir, event_loop, capfd):
120+
mktree(tmpworkdir, {
121+
"app.py": f"""\
122+
from aiohttp import web
123+
import sys
124+
sys.path.insert(0, "{tmpworkdir}/libs/l1")
125+
126+
async def hello(request):
127+
return web.Response(text="<h1>hello world</h1>", content_type="text/html")
128+
129+
async def create_app():
130+
a = web.Application()
131+
a.router.add_get("/", hello)
132+
return a
133+
""",
134+
"libs": {
135+
"l1": {
136+
"__init__.py": "",
137+
"app.py": "print('wrong_import')"
138+
}
139+
}
140+
})
141+
142+
set_start_method("spawn")
143+
config = Config(app_path="app.py", root_path=tmpworkdir, main_port=0, app_factory_name="create_app")
144+
config.import_app_factory()
145+
app_task = AppTask(config)
146+
147+
app_task._start_dev_server()
148+
app_task._process.join(2)
149+
150+
captured = capfd.readouterr()
151+
assert captured.out == ""
152+
153+
115154
@forked
116155
async def test_run_app_aiohttp_client(tmpworkdir, aiohttp_client):
117156
mktree(tmpworkdir, SIMPLE_APP)

0 commit comments

Comments
 (0)