Treat empty env vars as unset in config expansion

This commit is contained in:
Yiorgis Gozadinos 2026-06-25 12:41:04 +03:00
parent 73beae5a4c
commit 2fd025951c
No known key found for this signature in database
3 changed files with 19 additions and 3 deletions

View file

@ -1,6 +1,10 @@
# Changelog
## [Unreleased]
### Changed
- A bare `${VAR}` in YAML config now raises `MissingEnvVarError` when the variable is set but empty, matching the unset case. Use `${VAR:-default}` to allow an empty/absent value.
## [0.61.2] - 2026-06-24
### Fixed

View file

@ -12,7 +12,7 @@ _ENV_VAR_PATTERN = re.compile(r"\$\$|\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\
class MissingEnvVarError(ValueError):
"""A ${VAR} in the config references an unset environment variable."""
"""A ${VAR} in the config references an unset or empty environment variable."""
def _expand_str(value: str) -> str:
@ -20,12 +20,12 @@ def _expand_str(value: str) -> str:
if match.group(0) == "$$":
return "$"
name, default = match.group(1), match.group(2)
if name in os.environ and (default is None or os.environ[name] != ""):
if os.environ.get(name, "") != "":
return os.environ[name]
if default is not None:
return default
raise MissingEnvVarError(
f"Config references unset environment variable ${{{name}}}. "
f"Config references unset or empty environment variable ${{{name}}}. "
f"Set it, or use ${{{name}:-default}} to provide a fallback."
)

View file

@ -419,6 +419,18 @@ def test_expand_env_var_unset_raises(tmp_path, monkeypatch):
load_yaml_config(config_file)
def test_expand_env_var_empty_raises(tmp_path, monkeypatch):
"""A bare ${VAR} set to an empty string is treated as unset and raises."""
from haiku.rag.config.loader import MissingEnvVarError
monkeypatch.setenv("HAIKU_TEST_EMPTY", "")
config_file = tmp_path / "test.yaml"
config_file.write_text("api_key: ${HAIKU_TEST_EMPTY}")
with pytest.raises(MissingEnvVarError, match="HAIKU_TEST_EMPTY"):
load_yaml_config(config_file)
def test_expand_env_var_default_when_unset(tmp_path, monkeypatch):
"""${VAR:-default} falls back to the default when VAR is unset."""
monkeypatch.delenv("HAIKU_TEST_MISSING", raising=False)