Skip to content

Fix dotenv get exiting with code 1 for empty string values - #700

Merged
theskumar merged 4 commits into
theskumar:mainfrom
ShamikOfficial:fix/cli-get-empty-value
Oct 1, 2026
Merged

theskumar merged 4 commits into
theskumar:mainfrom
ShamikOfficial:fix/cli-get-empty-value

Conversation

@ShamikOfficial

Copy link
Copy Markdown
Contributor

Fixes #699.

Problem

dotenv get KEY uses if stored_value: so an empty string is treated as missing and the command exits with code 1 — the same as an absent key.

>>> from dotenv import set_key, get_key
>>> set_key(".env", "EMPTY", "")
>>> get_key(".env", "EMPTY")   # library API is fine
''
>>> # dotenv get EMPTY  -> exit code 1

Empty values (KEY= / KEY="") are valid; only the CLI was wrong.

Fix

Exit only when the key is absent or the stored value is None (bare key with no value). Empty strings are echoed and exit 0.

Tests

  • New test_get_empty_string_value pins exit code 0 and a blank line of output for a=.
  • Existing get tests still pass.

pytest tests/test_cli.py::test_get_existing_value tests/test_cli.py::test_get_empty_string_value tests/test_cli.py::test_get_non_existent_value — 3 passed.

ShamikOfficial and others added 2 commits September 12, 2026 12:32
Empty KEY= values are valid; the CLI truthiness check treated them as missing.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@theskumar

Copy link
Copy Markdown
Owner

Thanks @ShamikOfficial, the fix looks right. It matches get_key, which returns '' for KEY= and None for a bare KEY. I pushed two commits on top of yours without changing your commits:

Why the extra test: I tried a few deliberately broken versions of get. test_get_empty_string_value catches the original truthiness bug, but two plausible regressions got past the existing tests:

Mutant Caught before Caught after
if not values.get(key): (original bug) ✅ ✅
if key not in values: (drops the None check, prints None) ❌ ✅
never sys.exit(1) ❌ ✅

Locally: 260 passed, ruff check/ruff format --check clean, mypy --python-version 3.10 and 3.14 clean.

@theskumar
theskumar merged commit f215c02 into theskumar:main Oct 1, 2026
9 checks passed
theskumar added a commit that referenced this pull request Oct 1, 2026
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.

dotenv get exits with code 1 for empty string values

2 participants