Skip to content

fix(service): support Compose 2.x dict form for env_file entries#124

Merged
Cyb3rDudu merged 1 commit into
Mcrich23:mainfrom
ryan106:fix/env-file-dict-form
Jul 1, 2026
Merged

fix(service): support Compose 2.x dict form for env_file entries#124
Cyb3rDudu merged 1 commit into
Mcrich23:mainfrom
ryan106:fix/env-file-dict-form

Conversation

@ryan106

@ryan106 ryan106 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Problem

The Compose spec allows env_file to be written in three forms:

# 1. plain string
env_file: .env

# 2. list of strings
env_file:
  - config/common.env
  - config/service.env

# 3. list of objects (Compose v2 dict form)
env_file:
  - path: config/common.env
    required: true
  - path: config/optional.env
    required: false

container-compose currently only handles forms 1 and 2. Form 3, generated by Docker Compose v2 projects (and required when required: false is needed), causes a decoding failure.

Solution

Add a nested EnvFileEntry Decodable that accepts both string and dict forms via Swift's singleValueContainer / keyed-container fallback pattern, then normalise everything to [String] so the rest of the codebase is unchanged.

Changes

  • Sources/Container-Compose/Codable Structs/Service.swift — replace the two-branch env_file decoder with a three-way decode via EnvFileEntry
  • Tests/Container-Compose-StaticTests/EnvFileDictFormTests.swift — 7 new tests covering all three forms and a mixed array; regression test for the existing string/array forms

Tests

swift test --filter EnvFileDictFormTests

All 7 tests pass; existing tests unaffected.

The Compose spec allows env_file to be a string, a list of strings, or
a list of {path:, required:} objects. container-compose only handled the
string and [string] forms; the dict form (used by Docker Compose v2
projects) caused a decoding failure.

This change adds a nested Decodable helper (EnvFileEntry) that accepts
both string and dict forms and normalises them to [String] so the rest
of the codebase is unchanged.

Tests cover: plain string, array of strings (regression), single dict
entry, dict with required:, dict without required:, multiple dict entries,
and a mixed array. Test helper updated to use flatMap({ $0 }) when
unwrapping from services[String: Service?].
@Cyb3rDudu Cyb3rDudu merged commit 1e9a8eb into Mcrich23:main Jul 1, 2026
1 check passed
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.

3 participants