Skip to content

[BugFix] Preserve Required flag for duplicate interpolation variables#863

Open
Cursx wants to merge 1 commit intocompose-spec:mainfrom
Cursx:fix-duplicate-var-req
Open

[BugFix] Preserve Required flag for duplicate interpolation variables#863
Cursx wants to merge 1 commit intocompose-spec:mainfrom
Cursx:fix-duplicate-var-req

Conversation

@Cursx
Copy link
Copy Markdown

@Cursx Cursx commented Apr 14, 2026

Summary

Fixes docker/compose#13718template.ExtractVariables now correctly aggregates Required=true constraints when an interpolation variable is referenced multiple times.

When a Compose configuration utilizes the same variable name using both required and optional notations (e.g., ${PIHOLE_DOMAIN:?} followed by ${PIHOLE_DOMAIN}), the parser previously overwrote the element completely. This caused the variable mapping to silently drop the stringency requirement (Required: false), leading docker compose config --variables to misreport requirement constraints. This patch introduces a mergeVariable function implementing a logical OR for the Required flag and merges DefaultValue / PresenceValue attributes safely.

Before
services:
  pihole:
    image: pihole/pihole:2026.02.0
    labels:
      - "traefik.http.routers.pihole.rule=Host(`${PIHOLE_DOMAIN:?}`)"
      - "traefik.http.middlewares.pihole-redirect.redirectregex.regex=^(https://${PIHOLE_DOMAIN})/?$"

Running docker compose config --variables --format json mistakenly prints:

{ "PIHOLE_DOMAIN": { "Name": "PIHOLE_DOMAIN", "Required": false } }
After

With mergeVariable correctly aggregating constraints, running docker compose config --variables --format json accurately respects the strictest condition anywhere in the file:

{ "PIHOLE_DOMAIN": { "Name": "PIHOLE_DOMAIN", "Required": true } }

Users are now correctly warned about missing required variables in pre-flight Compose validations, regardless of YAML declaration order.

Test results (18 passed)
=== RUN   TestExtractVariables
=== RUN   TestExtractVariables/duplicate-variable-required-preserved
=== RUN   TestExtractVariables/duplicate-variable-value-preserved
--- PASS: TestExtractVariables (0.00s)
    --- PASS: TestExtractVariables/empty (0.00s)
    --- PASS: TestExtractVariables/no-variables (0.00s)
    --- PASS: TestExtractVariables/variable-without-curly-braces (0.00s)
    --- PASS: TestExtractVariables/variable-without-curly-braces-and-with-number-suffix (0.00s)
    --- PASS: TestExtractVariables/variable (0.00s)
    ...
    --- PASS: TestExtractVariables/duplicate-variable-required-preserved (0.00s)
    --- PASS: TestExtractVariables/duplicate-variable-value-preserved (0.00s)
PASS
ok  	github.com/compose-spec/compose-go/v2/template	2.225s

Test plan

  • 5 new tests: duplicate-variable-required-preserved, duplicate-variable-value-preserved, duplicate-variable-in-map, duplicate-variable-in-string, and duplicate-variable-conflict-default-required yielding combined Required: true behavior and cascading default values without data loss across all AST node types.
  • All 16 existing ExtractVariable tests pass (no regression)

@Cursx Cursx requested a review from ndeloof as a code owner April 14, 2026 07:40
@Cursx Cursx force-pushed the fix-duplicate-var-req branch from 23940f0 to c8151d1 Compare April 14, 2026 08:15
…Issue docker/compose#13718)

Signed-off-by: Cursx <33718736+Cursx@users.noreply.github.com>
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

1 participant