Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Eddie the Engineer
3a7da46afd fix(config): step over list items when unsetting a dotted key (mc-2nfr1)
`bd config unset dolt.host` on a config.yaml whose `dolt:` section is a
list of mappings commented out the first item's `host:` line and reported
success, although `dolt.host` was never set: `bd config get dolt.host`
reads "" before and after.

commentOutYamlKey fed the nested-key walk only the lines yamlKeyOnLine
can name. A `- ` list item, or any other content line it rejects, never
reached the walk, so the item's continuation line read as a direct child
of the matched section.

Such a line now advances the walk through nestedKeyWalk.stepOver. A
dotted key names mappings only, so nothing nested under the line can
continue the match, and the walk skips that subtree as it skips one under
a key that does not match. The skip is armed only while none is:
narrowing an armed skip would reopen a section the key does not own
partway through it, and widening one would trust a line that need not end
anything (yaml.v3 accepts a quoted value continued at column 0, and the
target can follow it). Blank lines and comments are not content and leave
the walk alone.

TestUnsetRemovesADottedKeyInEveryShape gains the list shapes (indented,
and at the section's own indentation), one case pinning each guard
(no narrowing, no widening, blank lines and comments), and an assertion
that unsetting a key that was never set leaves the file byte for byte.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 17:29:11 +00:00
2 changed files with 73 additions and 1 deletions

View file

@ -1223,7 +1223,10 @@ func commentOutYamlKey(content, key string) (string, error) {
continue
}
if name, indent, ok := yamlKeyOnLine(line); ok && walk.step(name, indent) {
name, indent, ok := yamlKeyOnLine(line)
if !ok {
walk.stepOver(line)
} else if walk.step(name, indent) {
if err := mappingValueUnsetRefusal(lines, i, indent, key); err != nil {
return "", err
}
@ -1386,6 +1389,31 @@ func (w *nestedKeyWalk) step(name string, indent int) bool {
return false
}
// stepOver advances the walk by one line that has content but declares no key
// yamlKeyOnLine can read, a list item above all. A dotted key names mappings
// only, so nothing nested under such a line can continue the match, and the
// walk skips that subtree as it does one under a key that does not match. These
// lines used to bypass the walk entirely, so unsetting `dolt.host` on
//
// dolt:
// - port: 3307
// host: 10.0.0.1
//
// read the item's `host:` as a direct child of `dolt:` and commented out a key
// the file never set, reporting success (mc-2nfr1).
//
// The skip is armed only while none is. Narrowing an armed one would end the
// skip over a section the key does not own partway through it, and widening
// one would trust a line that need not end anything: yaml.v3 accepts a quoted
// value continued at column 0, and the target can follow it.
func (w *nestedKeyWalk) stepOver(line string) {
trimmed := strings.TrimSpace(line)
if trimmed == "" || strings.HasPrefix(trimmed, "#") || w.opaque >= 0 {
return
}
w.opaque = lineIndent(line)
}
// unsupportedUnsetShape reports the two shapes named in the changelog — a key
// under a flow-style mapping, and a key whose value is a block scalar — in
// whichever spelling the file uses. Both used to be silent: the flow-style one

View file

@ -410,6 +410,44 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) {
key: "dolt.host",
survivors: map[string]string{"other.dolt.host": "should-survive"},
},
{
// The key's own section spelled as a list of mappings. A dotted key
// names mappings only, so nothing here is dolt.host, but the walk never
// saw the `- ` line, read the item's `host:` as the section's direct
// child, and commented it out with a success exit (mc-2nfr1).
name: "section spelled as a list of mappings",
seed: "dolt:\n - port: 3307\n host: 10.0.0.1\n - port: 3308\n host: 10.0.0.2\n",
key: "dolt.host",
},
{
// YAML lets the list sit at the section's own indentation.
name: "section spelled as a list at its own indentation",
seed: "dolt:\n- port: 3307\n host: 10.0.0.1\n",
key: "dolt.host",
},
{
// A list nested in a section the key does not own must not end the
// skip over that section: the leaf name after it is still other.host.
name: "list inside a section the key does not own",
seed: "dolt:\n other:\n list:\n - a\n host: keep\n",
key: "dolt.host",
survivors: map[string]string{"dolt.other.host": "keep"},
},
{
// Blank lines and comments declare no key either, but they are not
// content: the key beneath them is still the target.
name: "blank line and comment between the section and the key",
seed: "sync:\n\n# where bd pushes\n remote: \"file:///origin.git\"\n",
key: "sync.remote",
},
{
// yaml.v3 accepts a quoted value continued at column 0. That line
// declares no key, and it must not hide the target that follows it.
name: "quoted value continued at column 0 before the target",
seed: "dolt:\n note: \"line one\nline two\"\n host: kill-me\n",
key: "dolt.host",
survivors: map[string]string{"dolt.note": "line one line two"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
@ -448,6 +486,12 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) {
if wasSet && !strings.Contains(string(body), leaf+":") {
t.Errorf("unset removed the key instead of commenting it out:\n%s", body)
}
// A key that was never set leaves nothing to comment out. Checked on
// the text because a key inside a list item has no dotted spelling
// the survivors below could read it back by.
if !wasSet && string(body) != tc.seed {
t.Errorf("unset of a key that was never set edited the file:\n%s", body)
}
// Anything the unset was not asked about is none of its business.
for key, want := range tc.survivors {
if got := GetStringFromDir(dir, key); got != want {