diff --git a/packages/graphrag-common/graphrag_common/config/load_config.py b/packages/graphrag-common/graphrag_common/config/load_config.py index c8929149f1..156523d446 100644 --- a/packages/graphrag-common/graphrag_common/config/load_config.py +++ b/packages/graphrag-common/graphrag_common/config/load_config.py @@ -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: diff --git a/tests/unit/load_config/test_load_config.py b/tests/unit/load_config/test_load_config.py index 0945cf214f..b6aa700ee1 100644 --- a/tests/unit/load_config/test_load_config.py +++ b/tests/unit/load_config/test_load_config.py @@ -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