Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/dotenv/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ def __hash__(self) -> int:
def resolve(self, env: Mapping[str, Optional[str]]) -> str:
default = self.default if self.default is not None else ""
result = env.get(self.name, default)
return result if result is not None else ""
if result is None:
return ""
if result == "" and self.default is not None:
# POSIX: ${name:-default} falls back to the default when name
# is unset or empty (issue #715)
return default
return result


def parse_variables(value: str) -> Iterator[Atom]:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,10 @@ def test_dotenv_values_file(dotenv_path):
# Undefined
({}, "a=${b}", True, {"a": ""}),
({}, "a=${b:-d}", True, {"a": "d"}),
# Empty value with default: ${name:-default} treats empty as unset (#715)
({}, "b=\na=${b:-d}", True, {"a": "d", "b": ""}),
({"b": ""}, "a=${b:-d}", True, {"a": "d"}),
({}, "b=\na=${b}", True, {"a": "", "b": ""}),
# With quotes
({"b": "c"}, 'a="${b}"', True, {"a": "c"}),
({"b": "c"}, "a='${b}'", True, {"a": "c"}),
Expand Down
Loading