fix(config): preserve literal $ in config values#2363
Open
jluocsa wants to merge 1 commit into
Open
Conversation
Switch _parse_env_variables from Template.substitute to Template.safe_substitute so that a bare `$` in a config value (for example the regex anchor in `file_pattern: '.*\\.md$'`) no longer crashes the loader with `ValueError: Invalid placeholder in string`. Missing env-var references are still detected explicitly via `Template.get_identifiers` so callers continue to see `ConfigParsingError` when a referenced placeholder has no value -- existing behaviour and tests are preserved. Adds a regression test `test_load_config_preserves_literal_dollar_sign` covering bare `\$` in `str` values, `\$\$` escape, and a mixed regex + env var payload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2349.
_parse_env_variablesingraphrag-commoncallsstring.Template(text).substitute(os.environ).substitute()treatsevery
$character as a placeholder prefix and raisesValueError: Invalid placeholder in stringfor any$that is notfollowed by a valid Python identifier or
{ident}/$$escape. Commonconfig values fall into that trap, most notably regex anchors like
file_pattern: ".*\\.md$".Reproduction
Fix
Swap
substitute()forsafe_substitute().safe_substitute()leavesunrecognized
$sequences as literal text, which is the expectedbehaviour for arbitrary string config values.
To keep the existing "missing env var raises
ConfigParsingError"contract (and the test that exercises it), the function now also
inspects
Template.get_identifiers()(Python 3.11+, which is graphrag'sminimum) and raises
ConfigParsingErrorup-front if any referencedidentifier has no corresponding entry in
os.environ.Net result:
file_pattern: ".*\\.md$"ValueErrorcrashval: $$literal$literal$literal(unchanged)val: $MY_VAR(set)val: ${MISSING}ConfigParsingErrorConfigParsingError(unchanged)Tests
test_load_config_preserves_literal_dollar_signcovering bare
$in a regex-like value,$$escape, and a mixedpayload that combines a regex
$with a real env-var reference.test_load_config_validation(which expectsConfigParsingErrorfor a missing env var) still passes — theget_identifiers()pre-check keeps that path intact.Local run:
ruff checkandruff format --checkare clean on the changed files.Notes
.semversioner/next-release/patch-…jsonperCONTRIBUTING.md.change for any code path that was previously succeeding.