Repository navigation
fix: resolve dotenv run commands against the .env PATH on Windows - #730
Open
summeriver13 wants to merge 2 commits into
Open
summeriver13 wants to merge 2 commits into
summeriver13 wants to merge 2 commits into
Conversation
Windows' CreateProcess looks up the executable using the calling process' environment, ignoring the one passed to it. So `dotenv run` did not find commands only reachable through the PATH set in .env, and a bare `python` resolved to the interpreter that started the process rather than the virtualenv's. Resolve the command with shutil.which() against the environment we are about to pass instead. Fixes theskumar#373
This branch has not been deployed
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.
What this fixes
On Windows,
dotenv runignores thePATHfrom.env:CreateProcessresolves theexecutable against the calling process's environment, so the one passed to
Popenis notused (
os.execvpeon POSIX does use it). With a venv on thatPATH,dotenv run -- python -c "import sys; print(sys.executable)"prints the launchinginterpreter.
The fix
executable=shutil.which(command[0], path=cmd_env.get("PATH"))—path=is required,otherwise
shutil.whichfalls back toos.environ["PATH"]. Not found →None, so theexisting
Command not found: ...path is unchanged;executable=(not replacingcommand[0]) keeps the child'sargv[0].Verification
test_run_resolves_command_using_dotenv_pathputs a stub command in a directory reachableonly through the
.envPATH: it fails without the fix on Windows (Command not found: dotenv-test-cmd) and passes with it elsewhere.pytest tests/: 237 passed, 52 skipped, noregressions;
tox -e lint: ruff passes,mypyreports only the pre-existingtest_fifo_dotenv.py:15os.mkfifoerror.#508 uses the same call without
path=(it is alsoCONFLICTING), so the.envPATHstaysignored.
AI disclosure: an AI agent (pi) prepared the root cause, patch and test; I reproduced the bug
and ran the checks above.
Fixes #373