Skip to content

Guard entrypoint() against OSError from an unreadable .env - #125

Merged
woutervh merged 1 commit into
mainfrom
fix/entrypoint-oserror-guard
Aug 16, 2026
Merged

Guard entrypoint() against OSError from an unreadable .env#125
woutervh merged 1 commit into
mainfrom
fix/entrypoint-oserror-guard

Conversation

@woutervh

Copy link
Copy Markdown
Contributor

Summary

get_dotenv_path() only checks is_file() before entrypoint() hands the path to
dotenv.load_dotenv(). A permission-denied (or otherwise unreadable) .env passes that
check but fails to open, and the resulting OSError was previously uncaught - it
propagates 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 an except OSError as exc: branch around
    dotenv.load_dotenv(...) that warns via simple_warning() instead of crashing.
  • While in there: fixed the existing "django-dotenv installed instead of python-dotenv"
    AttributeError branch to also go through simple_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: new chmod(0o000)-based test asserting entrypoint() warns
    instead of raising, and that nothing gets loaded into os.environ. Skipped on Windows,
    where permission-denial via chmod isn't reliable.

Testing

  • pytest: 19 passed, 100% coverage (>= 90% required) - the new OSError branch is
    genuinely exercised, not pragma: no coverd.
  • ruff check .: clean
  • ty check: clean

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

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a8f5708) to head (4065173).

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     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@woutervh
woutervh merged commit ae5a4f7 into main Aug 16, 2026
19 checks passed
@woutervh
woutervh deleted the fix/entrypoint-oserror-guard branch August 16, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant