fix: merge attributes of repeated variable occurrences in ExtractVariables - #911
Open
ndeloof wants to merge 1 commit into
Open
fix: merge attributes of repeated variable occurrences in ExtractVariables#911ndeloof wants to merge 1 commit into
ndeloof wants to merge 1 commit into
Conversation
…ables
A variable used several times in a configuration had its extracted
attributes taken from whichever occurrence happened to be processed
last (map iteration made it non-deterministic): a plain ${VAR}
occurrence could erase the Required flag of a ${VAR:?} one, so
'docker compose config --variables' reported Required=false for a
variable interpolation actually requires
(docker/compose#13718).
Merge occurrences instead: a variable is required as soon as one of
its occurrences is, and keeps the first non-empty default/presence
value.
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes non-deterministic handling of repeated variable occurrences during ExtractVariables traversal by merging attributes instead of overwriting the last-seen occurrence, ensuring Required (and default/presence values) aren’t accidentally lost.
Changes:
- Replace overwrite-based variable collection with a merge helper (
combineVariable) when extracting variables recursively. - Preserve
Requiredif any occurrence requires the variable, and keep the first non-empty default/presence values. - Add regression tests covering required/plain and default/plain repeated occurrences.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| template/variables.go | Introduces merged variable aggregation via combineVariable to avoid overwrite-driven loss of attributes. |
| template/variables_test.go | Adds test cases to confirm merged behavior for repeated occurrences (required/plain, default/plain). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+72
to
+76
| // occurrences is (interpolation of that occurrence fails when the variable is | ||
| // unset, whatever the other occurrences declare), and keeps the default and | ||
| // presence values of the first occurrence defining one, so a plain `${VAR}` | ||
| // occurrence doesn't erase the attributes of a `${VAR:?}` or `${VAR:-value}` | ||
| // one. |
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.
ExtractVariablesmerged repeated occurrences of the same variable by plain overwrite: whichever occurrence happened to be processed last won, and map iteration made the outcome non-deterministic. A plain${VAR}occurrence could erase theRequiredflag extracted from a${VAR:?}one, sodocker compose config --variablesreportedRequired: falsefor a variable that interpolation actually requires (docker/compose#13718).Merge occurrences instead:
Verified end-to-end against docker/compose:
config --quietfails with "required variable PIHOLE_DOMAIN is missing a value" whileconfig --variablesused to reportRequired: false; with this change it reportsRequired: true.Fixes docker/compose#13718