Skip to content

refactor: use compose-spec/compose-go/v2 for fixupComposeYaml - #7422

Merged
stasadev merged 6 commits into
mainfrom
20250702_stasadev_compose_spec
Jul 22, 2025
Merged

stasadev merged 6 commits into
mainfrom
20250702_stasadev_compose_spec

Conversation

@stasadev

@stasadev stasadev commented Jul 2, 2025 •

Copy link
Copy Markdown
Member

The Issue

It's hard to edit .ddev/.ddev-docker-compose-full.yaml by doing manual YAML parsing.

How This PR Solves The Issue

Uses github.com/compose-spec/compose-go/v2 (adds ~2 MB to the ddev binary size).

Changes the app.ComposeYaml:

-ComposeYaml map[string]interface{} `yaml:"-"`
+ComposeYaml *composeTypes.Project  `yaml:"-"`

Bumps DockerComposeVersionConstraint from >= 2.5.1 to >= 2.24.3 (which is used in Docker Desktop with Docker Engine 25.0, see https://docs.docker.com/desktop/release-notes/) (and rename to ComposeVersionConstraint)
Docker 25.0 is our current recommended minimum.

Manual Testing Instructions

Automated Testing Overview

Release/Deployment Notes

It's important to keep github.com/compose-spec/compose-go/v2 library in sync with what is used in docker-compose.

@github-actions github-actions Bot added dependencies Pull requests that update a dependency file maintenance labels Jul 2, 2025
@github-actions

github-actions Bot commented Jul 2, 2025 •

Copy link
Copy Markdown

@stasadev

stasadev commented Jul 2, 2025

Copy link
Copy Markdown
Member Author

@rfay, from the git history I see you left compose-spec/compose-go comments in the imports here:

Looks like I'm making a second attempt to implement it.

@rfay

rfay commented Jul 2, 2025

Copy link
Copy Markdown
Member

Ambitious!

@rfay

rfay commented Jul 2, 2025

Copy link
Copy Markdown
Member

Pretty amazing that you seem to have gotten that all the way to clean with a very modest set of line changes.

@stasadev

stasadev commented Jul 2, 2025 •

Copy link
Copy Markdown
Member Author

And it will be much better after the next refactor:

-ComposeYaml map[string]interface{} `yaml:"-"`
+ComposeYaml *composeTypes.Project  `yaml:"-"`

We parse app.ComposeYaml manually in many places.

@stasadev
stasadev force-pushed the 20250702_stasadev_compose_spec branch 3 times, most recently from 43cf7b8 to a7468b4 Compare July 19, 2025 17:25
@stasadev
stasadev marked this pull request as ready for review July 20, 2025 08:06
@stasadev
stasadev requested review from a team as code owners July 20, 2025 08:06
@stasadev

Copy link
Copy Markdown
Member Author

I see potential for further refactoring, but it's better to leave that for other PRs. Ready for review.

@stasadev
stasadev requested a review from rfay July 21, 2025 13:19
@stasadev
stasadev force-pushed the 20250702_stasadev_compose_spec branch from a7468b4 to 7001416 Compare July 21, 2025 14:35
@rfay

rfay commented Jul 21, 2025 •

Copy link
Copy Markdown
Member

Claude Code Summary of Changes

This PR modernizes how DDEV handles Docker Compose YAML files by switching from manual parsing to using the official Docker Compose library.

Key Changes:

1. New Library Integration

  • Added github.com/compose-spec/compose-go/v2 - the official Docker Compose Go library
  • This is the same library Docker Compose itself uses internally

2. Type System Upgrade

  • Before: ComposeYaml was a generic map[string]interface{} - basically unstructured data
  • After: ComposeYaml is now *composeTypes.Project - a properly structured type with fields for services, networks, volumes, etc.

3. Better YAML Processing (pkg/ddevapp/compose_yaml.go:97-137)

  • New createComposeProject() function uses the official library to parse YAML
  • Automatically handles all the complex Docker Compose specification details
  • Ensures all required fields (Networks, Services, Volumes) are properly initialized

4. Updated Docker Compose Version Requirement

  • Before: Required Docker Compose >= 2.5.1
  • After: Now requires >= 2.24.3 (aligns with current Docker Desktop)

Why This Matters:

For Users: More reliable handling of complex Docker Compose configurations, especially when editing .ddev/.ddev-docker-compose-full.yaml

For Developers:

  • Type safety - the compiler catches errors that would previously only show up at runtime
  • Better IntelliSense/autocomplete when working with compose data
  • Less manual YAML parsing code to maintain

Trade-off: The binary size increases by ~2MB due to the additional library, but this provides much more robust YAML handling.

This is essentially DDEV saying "instead of reinventing the wheel for Docker Compose parsing, let's use the official tools that Docker itself uses."

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, it's beautiful, what can I say! Congratulations! Thank you!

I opened #7472, which is certainly not the fault of this PR. Stepping through your lovely compose PR I note that the project is first created in cmd/pull.go's init(). Yuck to inits! Then it happens again in push.go's init(). Then it finally happens in root.go's init(), snapshot_restore.go, start.go, ddevapp.go's Start() (probably the first time it was actually needed).


// createComposeProject creates a compose project from a string
func createComposeProject(yamlStr string) (*composeTypes.Project, error) {
project, err := composeLoader.LoadWithContext(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So great!

Comment on lines +111 to +119
if project.Networks == nil {
project.Networks = composeTypes.Networks{}
}
if project.Services == nil {
project.Services = composeTypes.Services{}
}
if project.Volumes == nil {
project.Volumes = composeTypes.Volumes{}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be weird if any of these things happened, true?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, but to avoid some random panic, we need to have these.

Comment thread pkg/ddevapp/ddevapp.go
XHGuiHTTPPort string `yaml:"xhgui_http_port,omitempty"`
HostXHGuiPort string `yaml:"host_xhgui_port,omitempty"`
XHProfMode types.XHProfMode `yaml:"xhprof_mode,omitempty"`
ComposeYaml *composeTypes.Project `yaml:"-"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yay!

Comment thread pkg/ddevapp/ddevapp.go
return err
}

// CheckAddonIncompatibilities looks for problems with docker-compose.*.yaml 3rd-party services

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this has all been taken care of because we used compose to load?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, and it could be deleted even earlier, the networks were added automatically anyway.

Comment thread pkg/ddevapp/traefik.go
for serviceName, s := range services.(map[string]interface{}) {
service := s.(map[string]interface{})
if env, ok := service["environment"].(map[string]interface{}); ok {
var virtualHost string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So great to lose all this!

@rfay
rfay force-pushed the 20250702_stasadev_compose_spec branch from 7001416 to 097e119 Compare July 21, 2025 21:41
@rfay

rfay commented Jul 21, 2025

Copy link
Copy Markdown
Member

Rebased to get Windows test fixup.

@stasadev
stasadev merged commit a6c75a0 into main Jul 22, 2025
@stasadev
stasadev deleted the 20250702_stasadev_compose_spec branch July 22, 2025 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file maintenance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants