Skip to content

Commit 690e022

Browse files
ndeloofclaude
authored andcommitted
loader: honor !reset on keys of fields normalized into KEY=VALUE sequences
A `!reset` on a mapping key (environment.FOO, labels.x, …) is recorded as a key path, then applied to the accumulated model before merging the file that carries it. But once two earlier files contributed the same field, merging has normalized the mapping into a sorted "KEY=VALUE" sequence (override.mergeToSequence), so the key-deletion branch of applyNullOverrides finds a sequence it cannot address and the reset was silently ignored. Resetting a whole field kept working; per-key resets only failed once a second file touched the field, which made the bug look like a mysterious three-file issue. Teach applyNullOverrides to translate such patterns: when a recorded path extends a sequence's path by exactly one segment, drop the entries whose key part matches — "KEY=VALUE" and bare "KEY" passthrough forms alike. Path segments un-escape through tree.Path so dotted keys (labels) match their raw entry form. Fixes docker/compose#11816 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 648fd7a commit 690e022

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

‎loader/merge_reset_test.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,62 @@ func Test_LoadWithReset(t *testing.T) {
6363
})
6464
}
6565

66+
// Test_LoadWithResetOnKeysNormalizedToSequence is the regression test for
67+
// docker/compose#11816: when two files contribute the same mapping field
68+
// (environment, labels, …), merging normalizes it into a "KEY=VALUE" sequence,
69+
// and a later `!reset` on one of its keys used to be silently ignored.
70+
func Test_LoadWithResetOnKeysNormalizedToSequence(t *testing.T) {
71+
p, err := LoadWithContext(t.Context(), types.ConfigDetails{
72+
ConfigFiles: []types.ConfigFile{
73+
{
74+
Filename: "base.yml",
75+
Content: []byte(`
76+
name: test
77+
services:
78+
foo:
79+
image: foo
80+
environment:
81+
FOO: BAR
82+
KEEP: ME
83+
labels:
84+
com.example.foo: bar
85+
`),
86+
},
87+
{
88+
Filename: "extra.yml",
89+
Content: []byte(`
90+
services:
91+
foo:
92+
environment:
93+
- ANOTHER=BAR
94+
- PASSTHROUGH
95+
labels:
96+
com.example.extra: bar
97+
`),
98+
},
99+
{
100+
Filename: "override.yml",
101+
Content: []byte(`
102+
services:
103+
foo:
104+
environment:
105+
FOO: !reset
106+
ANOTHER: !reset
107+
PASSTHROUGH: !reset
108+
labels:
109+
com.example.foo: !reset
110+
`),
111+
},
112+
},
113+
}, func(options *Options) {
114+
options.SkipNormalization = true
115+
})
116+
assert.NilError(t, err)
117+
me := "ME"
118+
assert.DeepEqual(t, p.Services["foo"].Environment, types.MappingWithEquals{"KEEP": &me})
119+
assert.DeepEqual(t, p.Services["foo"].Labels, types.Labels{"com.example.extra": "bar"})
120+
}
121+
66122
func Test_DuplicateReset(t *testing.T) {
67123
_, err := LoadWithContext(t.Context(), types.ConfigDetails{
68124
ConfigFiles: []types.ConfigFile{

‎loader/reset.go‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package loader
1919
import (
2020
"errors"
2121
"fmt"
22+
"slices"
2223
"strconv"
2324
"strings"
2425

@@ -291,14 +292,15 @@ func (p *ResetProcessor) applyNullOverrides(target any, path tree.Path) error {
291292
switch v := target.(type) {
292293
case map[string]any:
293294
KEYS:
294-
for k, e := range v {
295+
for k := range v {
295296
next := path.Next(k)
296297
for _, pattern := range p.paths {
297298
if next.Matches(pattern) {
298299
delete(v, k)
299300
continue KEYS
300301
}
301302
}
303+
e := p.pruneResetKeys(v, k, next)
302304
err := p.applyNullOverrides(e, next)
303305
if err != nil {
304306
return err
@@ -323,6 +325,51 @@ func (p *ResetProcessor) applyNullOverrides(target any, path tree.Path) error {
323325
return nil
324326
}
325327

328+
// pruneResetKeys returns the value of the key of a mapping, and, when it is a
329+
// sequence, removes from it (and from the mapping) the entries a `!reset`
330+
// targets, see removeResetKeys.
331+
func (p *ResetProcessor) pruneResetKeys(parent map[string]any, key string, path tree.Path) any {
332+
e := parent[key]
333+
if seq, ok := e.([]any); ok {
334+
e = p.removeResetKeys(seq, path)
335+
parent[key] = e
336+
}
337+
return e
338+
}
339+
340+
// removeResetKeys drops from a sequence the entries whose key part is targeted
341+
// by a recorded `!reset` path. A `!reset` on a mapping key (e.g.
342+
// `environment.FOO`) records a key path, but once several files contributed
343+
// the field, merging has normalized it into a "KEY=VALUE" sequence (see
344+
// override.mergeToSequence: environment, labels, build.args, …), so the
345+
// mapping branch of applyNullOverrides can no longer reach the key
346+
// (docker/compose#11816). Match such patterns against the key part of each
347+
// entry — "KEY=VALUE" or bare "KEY" — and remove it.
348+
func (p *ResetProcessor) removeResetKeys(seq []any, path tree.Path) []any {
349+
var keys []string
350+
for _, pattern := range p.paths {
351+
last := pattern.Last()
352+
if last == tree.PathMatchAll || !path.Matches(pattern.Parent()) {
353+
continue
354+
}
355+
// path segments escape "." (see tree.Path.Next); entries hold raw keys
356+
keys = append(keys, tree.Path(last).String())
357+
}
358+
if len(keys) == 0 {
359+
return seq
360+
}
361+
filtered := make([]any, 0, len(seq))
362+
for _, e := range seq {
363+
if s, ok := e.(string); ok && slices.ContainsFunc(keys, func(key string) bool {
364+
return s == key || strings.HasPrefix(s, key+"=")
365+
}) {
366+
continue
367+
}
368+
filtered = append(filtered, e)
369+
}
370+
return filtered
371+
}
372+
326373
func (p *ResetProcessor) checkForCycle(node *yaml.Node, path tree.Path) error {
327374
paths := p.visitedNodes[node]
328375

0 commit comments

Comments
 (0)