os.getcwd() as default for reload_dirs
is flaky
#1382
Replies: 3 comments 2 replies
-
use --reload-dirs or there's something I'm missing ? |
Beta Was this translation helpful? Give feedback.
-
@euri10 yes, I believe you are missing something: I'm saying that the default logic can be improved if no --reload-dirs arg is supplied. Again... I am happy to implement a PR for this myself. Perhaps can this issue be re-opened for further discussion if you believe that the existing default logic cannot be improved upon and is perfect the way it is? I'd like to understand better. CWD is (often) the same directory that your app is located in, but the two have nothing to do with each other in general so I'm a bit confused why you wouldn't want to use the actual directory containing the app module as the default??? |
Beta Was this translation helpful? Give feedback.
-
ok let's see what other think of this |
Beta Was this translation helpful? Give feedback.
-
Checklist
master
.Describe the bug
IF your current working directory doesn't happen to match the app directory (which is often the case in IDEs such as intellij, where the current working directory by default is set to the directory containing the python file you are running, not the directory containing the associated python package, e.g. the
src
directory... OR if you've simply invoked your python script from a different directory than the one it's in....) then the default ofos.getcwd()
forreload_dirs
makes no sense.There is a "smarter" implementation to be had here, in 2 possible ways:
Case 1) User supplies
app_dir
: In this case, the defaultreload_dirs
could simply beapp_dir
! (I would tend to avoid this approach in favor of Case (2) which works for all cases.)Case 2) User does OR does not supply
app_dir
:IF we are reloading, then we know
app
must be a string containing the fully qualified module name of the app! We can now look up the module path! By walking back the module path to the beginning of the fully qualified module name, we can find the source directory which is what we should be watching for source changes by default!Here is an example (simplified) implementation for case 2. If this idea is pleasing, I will turn it into a PR with more bells and whistles.
Steps to reproduce the bug
With the following app structure:
and
cwd
set tosrc/main/main_app
(or to some sibling of{project_dir}
),run:
Now make a change to the file
main_util.py
.Now make a change to the file
ext_util.py
.Expected behavior
I personally would expect the app to reload after either
main_util.py
was changed orext_util.py
was changed, regardless of where your cwd is.Actual behavior
The app reloads after neither change. (And though the current behavior is documented, it is both suboptimal and surprising IMO, and easily fixed by my proposed tweak.)
Environment
Running uvicorn 0.16.0 with CPython 3.8.10 on Darwin
Additional context
ADDENDUM: I see by the mention in the original issue that I am not the only one who feels
cwd
as a default is insufficiently robust 😅Beta Was this translation helpful? Give feedback.
All reactions