diff --git a/CHANGELOG.md b/CHANGELOG.md index 00f08f58..8b74dfac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed +- `dotenv get` no longer exits with code 1 for empty string values (`KEY=`) by [@ShamikOfficial] in [#700] - 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] ## [1.2.3] - 2026-08-16 @@ -448,6 +449,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]). [#640]: https://github.com/theskumar/python-dotenv/pull/640 [#663]: https://github.com/theskumar/python-dotenv/pull/663 [#680]: https://github.com/theskumar/python-dotenv/pull/680 +[#700]: https://github.com/theskumar/python-dotenv/pull/700 [790c5c0]: https://github.com/theskumar/python-dotenv/commit/790c5c02991100aa1bf41ee5330aca75edc51311 @@ -494,6 +496,7 @@ os.PathLike]` instead of just `os.PathLike` (#347 by [@bbc2]). [@randomseed42]: https://github.com/randomseed42 [@sammck]: https://github.com/sammck [@samwyma]: https://github.com/samwyma +[@ShamikOfficial]: https://github.com/ShamikOfficial [@sidharth-sudhir]: https://github.com/sidharth-sudhir [@snobu]: https://github.com/snobu [@techalchemy]: https://github.com/techalchemy diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 79613e28..cd0b0311 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -141,11 +141,10 @@ def get(ctx: click.Context, key: Any) -> None: with stream_file(file) as stream: values = dotenv_values(stream=stream) - stored_value = values.get(key) - if stored_value: - click.echo(stored_value) - else: + # Empty strings are valid values; only missing keys / bare keys (None) fail. + if key not in values or values[key] is None: sys.exit(1) + click.echo(values[key]) @cli.command() diff --git a/tests/test_cli.py b/tests/test_cli.py index d4e3ad4d..7a8897ef 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -67,6 +67,15 @@ def test_get_existing_value(cli, dotenv_path): assert (result.exit_code, result.output) == (0, "b\n") +def test_get_empty_string_value(cli, dotenv_path): + """Empty string values must not be treated as missing (truthiness trap).""" + dotenv_path.write_text("a=\n") + + result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"]) + + assert (result.exit_code, result.output) == (0, "\n") + + def test_get_non_existent_value(cli, dotenv_path): result = cli.invoke(dotenv_cli, ["--file", dotenv_path, "get", "a"])