Guard entrypoint() against OSError from an unreadable .env - #125
Merged
Conversation
get_dotenv_path() only checks is_file(); it can't catch a permission-denied or otherwise unreadable .env, which previously propagated out of dotenv.load_dotenv() and crashed every Python process started in the venv (including recovery commands like pip/uv). entrypoint() now catches OSError and warns via simple_warning(), consistent with the other failure paths in that function. Also fixed the existing "django-dotenv installed instead of python-dotenv" warning to go through simple_warning() too (it previously bypassed it), and cleaned up a stray-whitespace bug in that message's line-continuation. Covered by a chmod(0o000)-based test (skipped on Windows, where permission-denial via chmod isn't reliable).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #125 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 66 69 +3
Branches 3 3
=========================================
+ Hits 66 69 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
get_dotenv_path()only checksis_file()beforeentrypoint()hands the path todotenv.load_dotenv(). A permission-denied (or otherwise unreadable).envpasses thatcheck but fails to open, and the resulting
OSErrorwas previously uncaught - itpropagates out of
entrypoint()and crashes every Python process started in the venv,including recovery commands like
pip/uv.Changes
src/autoread_dotenv/__init__.py: added anexcept OSError as exc:branch arounddotenv.load_dotenv(...)that warns viasimple_warning()instead of crashing.AttributeErrorbranch to also go throughsimple_warning()(it bypassed it before,inconsistent with the other two failure paths in the function), and cleaned up a
stray-whitespace bug from a line-continuation in that message.
docs/changes.md: logged under## Unreleased.tests/test_autoread.py: newchmod(0o000)-based test assertingentrypoint()warnsinstead of raising, and that nothing gets loaded into
os.environ. Skipped on Windows,where permission-denial via
chmodisn't reliable.Testing
pytest: 19 passed, 100% coverage (>= 90% required) - the newOSErrorbranch isgenuinely exercised, not
pragma: no coverd.ruff check .: cleanty check: clean