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
11 changes: 6 additions & 5 deletions packages/graphrag-common/graphrag_common/config/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ def _get_parser_for_file(file_path: str | Path) -> Callable[[str], dict[str, Any

def _parse_env_variables(text: str) -> str:
"""Parse environment variables in the configuration text."""
try:
return Template(text).substitute(os.environ)
except KeyError as error:
msg = f"Environment variable not found: {error}"
raise ConfigParsingError(msg) from error
for match in Template.pattern.finditer(text):
env_var = match.group("named") or match.group("braced")
if env_var is not None and env_var not in os.environ:
msg = f"Environment variable not found: {env_var!r}"
raise ConfigParsingError(msg)
return Template(text).safe_substitute(os.environ)


def _recursive_merge_dicts(dest: dict[str, Any], src: dict[str, Any]) -> None:
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/load_config/test_load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,30 @@ def test_load_config():
assert config_with_env_vars.nested_list[0].nested_int == 7
assert config_with_env_vars.nested_list[1].nested_str == "list_value_2"
assert config_with_env_vars.nested_list[1].nested_int == 8


def test_load_config_preserves_literal_dollar_signs(tmp_path: Path) -> None:
"""Literal dollar signs such as regex anchors should not be parsed as env vars."""
pattern = r".*\.md$"
config_path = tmp_path / "settings.yaml"
config_path.write_text(
f"""
name: '{pattern}'
value: 100
nested:
nested_str: "nested_value"
nested_int: 42
nested_list:
- nested_str: "list_value_1"
nested_int: 7
""",
encoding="utf-8",
)

config = load_config(
config_initializer=TestConfigModel,
config_path=config_path,
set_cwd=False,
)

assert config.name == pattern