Skip to content

Commit f215c02

Browse files
fix: dotenv get exits 0 for empty string values (#700)
Fixes #699
1 parent 58f2d7c commit f215c02

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
99

1010
### Fixed
1111

12+
- `dotenv get` no longer exits with code 1 for empty string values (`KEY=`) by [@ShamikOfficial] in [#700]
1213
- An unquoted empty value followed by an inline comment (e.g. `KEY= # comment`) is now parsed as an empty string instead of the comment text by [@Noethix55555] in [#663]
1314
- `dotenv run --no-override` now expands variable references with the same precedence as `load_dotenv(override=False)`, so a value like `${BASE}/suffix` uses the existing `BASE` from the environment instead of the one from the `.env` file by [@ROTl24] in [#698]
1415

@@ -450,6 +451,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]).
450451
[#663]: https://github.com/theskumar/python-dotenv/pull/663
451452
[#680]: https://github.com/theskumar/python-dotenv/pull/680
452453
[#698]: https://github.com/theskumar/python-dotenv/pull/698
454+
[#700]: https://github.com/theskumar/python-dotenv/pull/700
453455
[790c5c0]: https://github.com/theskumar/python-dotenv/commit/790c5c02991100aa1bf41ee5330aca75edc51311
454456

455457
<!-- contributors -->
@@ -497,6 +499,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]).
497499
[@randomseed42]: https://github.com/randomseed42
498500
[@sammck]: https://github.com/sammck
499501
[@samwyma]: https://github.com/samwyma
502+
[@ShamikOfficial]: https://github.com/ShamikOfficial
500503
[@sidharth-sudhir]: https://github.com/sidharth-sudhir
501504
[@snobu]: https://github.com/snobu
502505
[@techalchemy]: https://github.com/techalchemy

‎src/dotenv/cli.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ def get(ctx: click.Context, key: Any) -> None:
141141
with stream_file(file) as stream:
142142
values = dotenv_values(stream=stream)
143143

144-
stored_value = values.get(key)
145-
if stored_value:
146-
click.echo(stored_value)
147-
else:
144+
# Empty strings are valid values; only missing keys / bare keys (None) fail.
145+
if key not in values or values[key] is None:
148146
sys.exit(1)
147+
click.echo(values[key])
149148

150149

151150
@cli.command()

‎tests/test_cli.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ def test_get_existing_value(cli, dotenv_path):
6868
assert (result.exit_code, result.output) == (0, "b\n")
6969

7070

71+
def test_get_empty_string_value(cli, dotenv_path):
72+
"""Empty string values must not be treated as missing (truthiness trap)."""
73+
dotenv_path.write_text("a=\n")
74+
75+
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"])
76+
77+
assert (result.exit_code, result.output) == (0, "\n")
78+
79+
80+
def test_get_value_without_equals(cli, dotenv_path):
81+
dotenv_path.write_text("a")
82+
83+
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"])
84+
85+
assert (result.exit_code, result.output) == (1, "")
86+
87+
7188
def test_get_non_existent_value(cli, dotenv_path):
7289
result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"])
7390

0 commit comments

Comments
 (0)