From dd046131f029dfe43a7b97a62ece9a932b6d8f24 Mon Sep 17 00:00:00 2001 From: Ben S George <73480087+AlbertEinsteinTG@users.noreply.github.com> Date: Sun, 9 Aug 2026 12:16:20 +0000 Subject: [PATCH] fix(types): name the clashing service and make the error stable CheckContainerNameUnicity tracked seen container names in a utils.Set[string], which is a map[string]struct{}. Indexing it yields the zero struct rather than the service that claimed the name, so the %s verb rendered it as "{}". The format string also ended with an unbalanced quote: services.a: container name "shared" is already in use by service {}" Track container_name -> declaring service in a map[string]string and print it with %q so both services are named. Iterate via ServiceNames() instead of ranging over the map: Go randomises map iteration, so the same project surfaced a different pair on each run, which made the error unreproducible in bug reports and impossible to assert exactly. services.a: container name "shared" is already in use by service "b" The existing test asserted a substring ending one word before the broken segment, so neither defect was covered. It now asserts the whole message, which the stable ordering makes possible. Signed-off-by: Ben S George <73480087+AlbertEinsteinTG@users.noreply.github.com> --- loader/validate_test.go | 2 +- types/project.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/loader/validate_test.go b/loader/validate_test.go index 2dda05eaf..1c5a738d3 100644 --- a/loader/validate_test.go +++ b/loader/validate_test.go @@ -277,7 +277,7 @@ func TestValidateContainerName(t *testing.T) { }, } err := project.CheckContainerNameUnicity() - assert.Assert(t, strings.Contains(err.Error(), `container name "mycontainer" is already in use by`)) + assert.Assert(t, strings.Contains(err.Error(), `container name "mycontainer" is already in use by service "myservice`)) } func TestValidateWatch(t *testing.T) { diff --git a/types/project.go b/types/project.go index 66365faed..d82acdea7 100644 --- a/types/project.go +++ b/types/project.go @@ -905,13 +905,14 @@ func (p *Project) WithServicesTransform(fn func(name string, s ServiceConfig) (S // CheckContainerNameUnicity validate project doesn't have services declaring the same container_name func (p *Project) CheckContainerNameUnicity() error { - names := utils.Set[string]{} - for name, s := range p.Services { + names := map[string]string{} + for _, name := range p.ServiceNames() { + s := p.Services[name] if s.ContainerName != "" { if existing, ok := names[s.ContainerName]; ok { - return fmt.Errorf(`services.%s: container name %q is already in use by service %s"`, name, s.ContainerName, existing) + return fmt.Errorf(`services.%s: container name %q is already in use by service %q`, name, s.ContainerName, existing) } - names.Add(s.ContainerName) + names[s.ContainerName] = name } } return nil