From bee632273fdce48c7abd32325c8b25cf7177e87d Mon Sep 17 00:00:00 2001 From: Rhevin Date: Tue, 10 Nov 2026 09:36:29 +0200 Subject: [PATCH] fix: recreate when host, protocol, or mount mode changes The config hash ignored those fields, so up kept containers created with the old run args. Go 1.27 is required because compose-go v2.16.2 exposes service fields only through embedded structs. --- go.mod | 2 +- internal/backend/config.go | 24 ++++++++++-- internal/backend/config_test.go | 69 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index e36ec17..d161ff8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/rhevin/apple-compose -go 1.26.4 +go 1.27.0 require ( github.com/compose-spec/compose-go/v2 v2.16.2 diff --git a/internal/backend/config.go b/internal/backend/config.go index bc58ef9..8f8647b 100644 --- a/internal/backend/config.go +++ b/internal/backend/config.go @@ -27,7 +27,15 @@ func serviceConfigHash(project string, svc types.ServiceConfig) string { hashWrite(h, "envfile:"+path) } if svc.Entrypoint != nil { - hashWrite(h, "entrypoint:"+strings.Join(svc.Entrypoint, " ")) + if len(svc.Entrypoint) <= 1 { + hashWrite(h, "entrypoint:"+strings.Join(svc.Entrypoint, " ")) + } else { + // First element is --entrypoint; the rest are args. A space-join hashed the same as one string. + hashWrite(h, "entrypoint:"+svc.Entrypoint[0]) + for _, a := range svc.Entrypoint[1:] { + hashWrite(h, "entryarg:"+a) + } + } } hashWrite(h, "user:"+svc.User) hashWrite(h, "workdir:"+svc.WorkingDir) @@ -128,10 +136,10 @@ func sortedVolumeKeys(project string, vols []types.ServiceVolumeConfig) []string for _, v := range vols { switch v.Type { case "bind": - keys = append(keys, fmt.Sprintf("bind:%s:%s", v.Source, v.Target)) + keys = append(keys, volumeHashKey("bind:"+v.Source, v.Target, v.ReadOnly)) case "volume", "": if v.Source != "" { - keys = append(keys, fmt.Sprintf("named:%s:%s:%s", project, v.Source, v.Target)) + keys = append(keys, volumeHashKey("named:"+project+":"+v.Source, v.Target, v.ReadOnly)) } default: keys = append(keys, fmt.Sprintf("%s:%s:%s", v.Type, v.Source, v.Target)) @@ -188,10 +196,18 @@ func sortedEnvPairs(env types.MappingWithEquals) []string { return pairs } +func volumeHashKey(source, target string, readOnly bool) string { + key := source + ":" + target + if readOnly { + key += ":ro" + } + return key +} + func composePortKeys(ports []types.ServicePortConfig) []string { keys := make([]string, 0, len(ports)) for _, p := range ports { - keys = append(keys, fmt.Sprintf("%s:%d", p.Published, p.Target)) + keys = append(keys, publishSpec(p)) } sort.Strings(keys) return keys diff --git a/internal/backend/config_test.go b/internal/backend/config_test.go index 3bdccc5..d44f17c 100644 --- a/internal/backend/config_test.go +++ b/internal/backend/config_test.go @@ -74,6 +74,75 @@ func TestServiceConfigHash_ChangesOnPlatform(t *testing.T) { } } +func TestServiceConfigHash_ChangesOnHostIP(t *testing.T) { + a := types.ServiceConfig{ + Name: "web", + Image: "nginx:alpine", + Ports: []types.ServicePortConfig{{Published: "8080", Target: 80}}, + } + b := a + b.Ports = []types.ServicePortConfig{{HostIP: "127.0.0.1", Published: "8080", Target: 80}} + if serviceConfigHash("myapp", a) == serviceConfigHash("myapp", b) { + t.Fatal("expected different hash when host IP is set") + } +} + +func TestServiceConfigHash_TCPPortDoesNotChangeHash(t *testing.T) { + a := types.ServiceConfig{ + Name: "web", + Image: "nginx:alpine", + Ports: []types.ServicePortConfig{{Published: "8080", Target: 80}}, + } + b := a + b.Ports = []types.ServicePortConfig{{Published: "8080", Target: 80, Protocol: "tcp"}} + if serviceConfigHash("myapp", a) != serviceConfigHash("myapp", b) { + t.Fatal("tcp is the CLI default and must not change the hash") + } +} + +func TestServiceConfigHash_ChangesOnProtocol(t *testing.T) { + a := types.ServiceConfig{ + Name: "dns", + Image: "nginx:alpine", + Ports: []types.ServicePortConfig{{Published: "5353", Target: 53, Protocol: "tcp"}}, + } + b := a + b.Ports = []types.ServicePortConfig{{Published: "5353", Target: 53, Protocol: "udp"}} + if serviceConfigHash("myapp", a) == serviceConfigHash("myapp", b) { + t.Fatal("expected different hash for udp") + } +} + +func TestServiceConfigHash_ChangesOnVolumeReadOnly(t *testing.T) { + a := types.ServiceConfig{ + Name: "db", + Image: "postgres:16", + Volumes: []types.ServiceVolumeConfig{ + {Type: "bind", Source: "/host/data", Target: "/data"}, + }, + } + b := a + b.Volumes = []types.ServiceVolumeConfig{ + {Type: "bind", Source: "/host/data", Target: "/data", ReadOnly: true}, + } + if serviceConfigHash("myapp", a) == serviceConfigHash("myapp", b) { + t.Fatal("expected different hash for a read-only mount") + } +} + +func TestServiceConfigHash_EntrypointArgsNotJoined(t *testing.T) { + split := types.ServiceConfig{ + Name: "web", + Image: "nginx:alpine", + Entrypoint: types.ShellCommand{"sh", "-c", "echo hi"}, + } + joined := split + joined.Entrypoint = types.ShellCommand{"sh -c echo hi"} + if serviceConfigHash("myapp", split) == serviceConfigHash("myapp", joined) { + t.Fatal("multi-element entrypoint must not hash like one joined string") + } +} + func TestServiceConfigHash_ChangesOnDNS(t *testing.T) { a := types.ServiceConfig{Name: "web", Image: "nginx:alpine", DNS: types.StringList{"1.1.1.1"}} b := types.ServiceConfig{Name: "web", Image: "nginx:alpine", DNS: types.StringList{"8.8.8.8"}}