Skip to content
Closed
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
7 changes: 6 additions & 1 deletion src/dotenv/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ 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 ""
# POSIX ${name:-default} also applies when the variable is set but empty.
if result == "" and self.default is not None:
return self.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"}),
# Defined but empty: ${name:-default} applies (POSIX semantics)
({"b": ""}, "a=${b:-d}", True, {"a": "d"}),
({}, "b=\na=${b:-d}", True, {"a": "d", "b": ""}),
({"b": ""}, "a=${b}", True, {"a": ""}),
# With quotes
({"b": "c"}, 'a="${b}"', True, {"a": "c"}),
({"b": "c"}, "a='${b}'", True, {"a": "c"}),
Expand Down
Loading