Skip to content

fix: merge attributes of repeated variable occurrences in ExtractVariables - #911

Open
ndeloof wants to merge 1 commit into
compose-spec:mainfrom
ndeloof:extract-variables-merge-occurrences
Open

fix: merge attributes of repeated variable occurrences in ExtractVariables#911
ndeloof wants to merge 1 commit into
compose-spec:mainfrom
ndeloof:extract-variables-merge-occurrences

Conversation

@ndeloof

@ndeloof ndeloof commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

ExtractVariables merged 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 the Required flag extracted from a ${VAR:?} one, so docker compose config --variables reported Required: false for a variable that interpolation actually requires (docker/compose#13718).

Merge occurrences instead:

  • a variable is required as soon as one of its occurrences is — interpolation of that occurrence fails when the variable is unset, whatever the other occurrences declare;
  • the first non-empty default/presence value is kept, so a plain occurrence doesn't erase them.

Verified end-to-end against docker/compose: config --quiet fails with "required variable PIHOLE_DOMAIN is missing a value" while config --variables used to report Required: false; with this change it reports Required: true.

Fixes docker/compose#13718

…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>
@ndeloof
ndeloof requested review from glours and a lite review from Copilot August 20, 2026 06:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Required if 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 thread template/variables.go
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] env variable detected as not required even though it is

2 participants