diff --git a/CHANGELOG.md b/CHANGELOG.md index d291eb727..e2f6684ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1195,6 +1195,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 no longer counts an issue its batch filter skipped (create-only, a `ShouldPush` hook, parent, type, or conflict) as skipped twice. Notion's batch dry-run was already correct. +- **`bd config unset` no longer reports success while the key stays set, and + now names where the unset actually landed.** A key present in + `config.yaml` but not claimed by `IsYamlOnlyKey` was deleted from the + database only, so it stayed effective; it is now cleared from both, and the + reported location - `Unset (in database, config.yaml)` - is derived + from what the writes actually changed rather than from a pre-check of viper's + merged value, which counted defaults and environment variables as present in + the file. A yaml-only key that was not in the file prints ` was not set + in config.yaml` instead of claiming a write. A workspace with no project + `config.yaml` at all is an answer rather than a failure, so a database-backed + unset there no longer deletes the row and then exits non-zero. Unsetting a + key whose value is the indented block beneath it (a mapping or a list), which + used to comment out the key line and orphan the block into a `config.yaml` + that no longer parsed, is now refused alongside the flow-style and + block-scalar shapes above. An unset also keeps the file's trailing newline, + and an unset that changes nothing now leaves the file byte-identical - on a + CRLF `config.yaml` it previously reported a write for a key that was never + there and rewrote every line ending to LF. The stderr side-effect hint is + gated on the write too, so a no-op no longer announces a removal it did not + make, and the `--json` payload for a yaml-only key carries a `changed` boolean + so the machine branch can tell an absent key from an unpopulated field. + ## [1.3.0] - 2026-09-15 The first tested release off `main` since the 1.1 line. [1.2.2] was a recovery diff --git a/cmd/bd/config.go b/cmd/bd/config.go index ca7e1971f..a475f89d2 100644 --- a/cmd/bd/config.go +++ b/cmd/bd/config.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "fmt" "os" "os/exec" @@ -797,13 +798,14 @@ func showConfigYAMLOverrides(dbConfig map[string]string) { // cleanup runs either way, and a failed YAML unset carries the row's fate in // its error rather than dropping it. func runConfigUnsetYamlOnly(key string) error { - location := "config.yaml" + file := "config.yaml" + var changed bool var unsetErr error if config.IsUserGlobalKey(key) { - unsetErr = config.UnsetUserYamlConfig(key) - location = config.UserConfigYamlDisplayPath() + changed, unsetErr = config.UnsetUserYamlConfig(key) + file = config.UserConfigYamlDisplayPath() } else { - unsetErr = config.UnsetYamlConfig(key) + changed, unsetErr = config.UnsetYamlConfig(key) } cleanup := unsetLeakedSecretRow(key) @@ -812,10 +814,23 @@ func runConfigUnsetYamlOnly(key string) error { return HandleError("unsetting config: %v%s", unsetErr, cleanup.rowNote(key)) } + // The location is a report of the write, not a guess made before it: an + // absent file or a key that was never written there is a no-op, and saying + // "Unset (in config.yaml)" over a file that never held it is the + // untrustworthy message this command's fix set out to remove. + location := "" + if changed { + location = file + } + if jsonOutput { payload := map[string]interface{}{ "key": key, "location": location, + // The human branch gets an explicit "was not set" sentence; without + // this the machine branch cannot tell that no-op apart from an + // unpopulated field, since both render location as "". + "changed": changed, } if config.IsSecretKey(key) { cleanup.jsonFields(payload) @@ -823,11 +838,22 @@ func runConfigUnsetYamlOnly(key string) error { if err := outputJSON(payload); err != nil { return err } - } else { + } else if changed { fmt.Printf("Unset %s (in %s)\n", key, location) cleanup.describe(key, location) + } else { + fmt.Printf("%s was not set in %s\n", key, file) + cleanup.describe(key, file) + } + // Gate the hint on the write, uniformly across all three arms above. Every + // key in checkConfigUnsetSideEffects is phrased in the completed past tense + // ("Backup config removed...") and three of the four hand the operator a + // follow-up command, so on the no-op branch the command contradicted the + // line it had just printed. This covers the jsonOutput arm too: the hint + // goes to stderr, so --json never suppressed it. + if changed { + printConfigSideEffects(checkConfigUnsetSideEffects(key)) } - printConfigSideEffects(checkConfigUnsetSideEffects(key)) return nil } @@ -885,14 +911,44 @@ var configUnsetCmd = &cobra.Command{ } noteDirectConfigWrite() + // Clear the config.yaml layer unconditionally and report what the + // write actually did. Pre-checking with GetYamlConfig would read + // viper's *merged* value - SetDefault values and AutomaticEnv + // included - so a key with a non-empty default (no-hooks, json, + // events-journal-retain-days, ...) looked present in config.yaml even + // in a workspace with no config.yaml at all. That produced a more + // specific false claim than the message this fix removed, and, with no + // project config.yaml, failed the command after the database row was + // already gone. + // + // A config.yaml the unset refuses to edit (a flow-style mapping, a + // block value) is reported as exactly that: any database row is + // already gone, but the key is still effective from the file, so this + // still fails. "Any": UnsetSettingResult cannot say whether a row + // existed, since the storage seam discards the affected-row count. + location := "database" + yamlCleared, err := config.UnsetYamlConfig(result.Key) + // A workspace with no project config.yaml has no YAML layer to clear, so + // the database write above is the whole unset and this succeeds - that is + // the case that used to fail the command after the row was already gone. + // A config.yaml that exists and refused the edit is the opposite: the key + // is still effective from the file, so that still fails. + if err != nil && !errors.Is(err, config.ErrNoProjectConfigYaml) { + return HandleError("%s is still set in config.yaml (any database row was removed): %v", result.Key, err) + } + if yamlCleared { + location = "database, config.yaml" + } + if jsonOutput { if err := outputJSON(map[string]string{ - "key": result.Key, + "key": result.Key, + "location": location, }); err != nil { return err } } else { - fmt.Printf("Unset %s\n", result.Key) + fmt.Printf("Unset %s (in %s)\n", result.Key, location) } printConfigSideEffects(checkConfigUnsetSideEffects(result.Key)) return nil diff --git a/cmd/bd/config_embedded_test.go b/cmd/bd/config_embedded_test.go index 5838b59eb..07ebbc091 100644 --- a/cmd/bd/config_embedded_test.go +++ b/cmd/bd/config_embedded_test.go @@ -7,6 +7,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "strings" "sync" "testing" @@ -159,6 +160,189 @@ func TestEmbeddedConfig(t *testing.T) { } }) + // A key that config.yaml carries but IsYamlOnlyKey does not claim was + // unset from the database alone, so it stayed effective while bd reported + // success. + t.Run("config_unset_clears_yaml_layer", func(t *testing.T) { + configPath := filepath.Join(dir, ".beads", "config.yaml") + original, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("read config.yaml: %v", err) + } + if err := os.WriteFile(configPath, append(original, []byte("\ntest.fromyaml: yamlvalue\n")...), 0600); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + + out := bdConfig(t, bd, dir, "unset", "test.fromyaml") + if !strings.Contains(out, "config.yaml") { + t.Errorf("unset output should name config.yaml as a cleared location: %s", out) + } + + after, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("read config.yaml after unset: %v", err) + } + if strings.Contains(string(after), "\ntest.fromyaml: yamlvalue") { + t.Errorf("test.fromyaml still set in config.yaml after unset:\n%s", after) + } + if !strings.Contains(string(after), "# test.fromyaml: yamlvalue") { + t.Errorf("expected the unset key to remain as a comment:\n%s", after) + } + }) + + // The location bd reports must be a record of the writes it made, not a + // guess made before them. GetYamlConfig reads viper's MERGED value - + // SetDefault values and AutomaticEnv included - so a database-backed key + // with a non-empty default looked present in config.yaml even in a + // workspace whose config.yaml never mentioned it, and bd claimed to have + // cleared a file it did not touch. + t.Run("config_unset_does_not_claim_a_yaml_write_it_did_not_make", func(t *testing.T) { + configPath := filepath.Join(dir, ".beads", "config.yaml") + before, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("read config.yaml: %v", err) + } + // no-hooks is not a yaml-only key and carries the default "false", so + // GetYamlConfig("no-hooks") is non-empty with or without a + // config.yaml entry. It is absent from this workspace's config.yaml. + if strings.Contains(string(before), "no-hooks") { + t.Skipf("this workspace's config.yaml already carries no-hooks; the probe needs a key it does not") + } + + bdConfig(t, bd, dir, "set", "no-hooks", "true") + out := bdConfig(t, bd, dir, "unset", "no-hooks") + + if strings.Contains(out, "config.yaml") { + t.Errorf("unset named config.yaml as a cleared location, but the key was never written there: %s", out) + } + if !strings.Contains(out, "in database") { + t.Errorf("unset should report the database as the cleared location: %s", out) + } + + after, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("read config.yaml after unset: %v", err) + } + if string(after) != string(before) { + t.Errorf("config.yaml was rewritten by an unset that had nothing to clear:\nbefore:\n%s\nafter:\n%s", before, after) + } + }) + + // With no project config.yaml at all, a database-backed unset of a + // defaulted key used to delete the row and then fail on "no + // .beads/config.yaml found" - a non-zero exit over a half-applied unset, + // where before this PR bd printed "Unset " and exited 0. "Not in + // config.yaml" is an answer, not a failure. The key has to be one with a + // non-empty default (no-hooks), since that is what made the old + // GetYamlConfig pre-check believe there was a config.yaml layer to clear. + t.Run("config_unset_succeeds_with_no_project_config_yaml", func(t *testing.T) { + configPath := filepath.Join(dir, ".beads", "config.yaml") + original, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("read config.yaml: %v", err) + } + t.Cleanup(func() { + if err := os.WriteFile(configPath, original, 0600); err != nil { + t.Fatalf("restore config.yaml: %v", err) + } + }) + + bdConfig(t, bd, dir, "set", "no-hooks", "true") + if err := os.Remove(configPath); err != nil { + t.Fatalf("remove config.yaml: %v", err) + } + + out := bdConfig(t, bd, dir, "unset", "no-hooks") + if strings.Contains(out, "config.yaml") { + t.Errorf("unset named config.yaml with no config.yaml present: %s", out) + } + if _, err := os.Stat(configPath); !os.IsNotExist(err) { + t.Errorf("unset recreated config.yaml: %v", err) + } + }) + + // Both --json routes changed shape in this PR and neither was pinned. The + // yaml-only payload carries `changed` because `location` is "" both when + // the key was never set and when a field is merely unpopulated — the human + // branch got an explicit "was not set" sentence for exactly that reason and + // the machine branch had no way to tell the two apart. The stderr + // side-effect hint is gated on the same bool: every hint is phrased in the + // completed past tense ("Backup config removed..."), so printing one on the + // no-op branch contradicts the line printed just above it. + t.Run("config_unset_json_payloads_and_no_op_hint", func(t *testing.T) { + configPath := filepath.Join(dir, ".beads", "config.yaml") + original, err := os.ReadFile(configPath) + if err != nil { + t.Fatalf("read config.yaml: %v", err) + } + t.Cleanup(func() { + if err := os.WriteFile(configPath, original, 0600); err != nil { + t.Fatalf("restore config.yaml: %v", err) + } + }) + if strings.Contains(string(original), "backup.enabled") { + t.Skipf("this workspace's config.yaml already carries backup.enabled; the no-op probe needs a key it does not") + } + + run := func(args ...string) (string, string) { + t.Helper() + cmd := exec.Command(bd, append([]string{"config"}, args...)...) + cmd.Dir = dir + cmd.Env = bdEnv(dir) + stdout, stderr, err := runCommandBuffers(t, cmd) + if err != nil { + t.Fatalf("bd config %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String()) + } + return stdout.String(), stderr.String() + } + decode := func(out string) map[string]interface{} { + t.Helper() + var payload map[string]interface{} + if err := json.Unmarshal([]byte(out), &payload); err != nil { + t.Fatalf("unset --json did not emit an object: %v\n%s", err, out) + } + return payload + } + + // yaml-only route, key absent: no write, so no location, no hint. + stdout, stderr := run("unset", "--json", "backup.enabled") + payload := decode(stdout) + if changed, ok := payload["changed"].(bool); !ok || changed { + t.Errorf("yaml-only --json payload changed = %v (present=%v), want false: %s", payload["changed"], ok, stdout) + } + if payload["location"] != "" { + t.Errorf("yaml-only --json payload named a location for a key that was never set: %s", stdout) + } + if strings.Contains(stderr, "Hint:") { + t.Errorf("the no-op branch printed a side-effect hint asserting a removal that never happened:\n%s", stderr) + } + + // yaml-only route, key present: the write is reported, and the hint + // that describes it is the one thing that should appear on stderr. + if err := os.WriteFile(configPath, append(original, []byte("\nbackup.enabled: true\n")...), 0600); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + stdout, stderr = run("unset", "--json", "backup.enabled") + payload = decode(stdout) + if changed, ok := payload["changed"].(bool); !ok || !changed { + t.Errorf("yaml-only --json payload changed = %v, want true for a key the file carried: %s", payload["changed"], stdout) + } + if payload["location"] != "config.yaml" { + t.Errorf("yaml-only --json payload location = %v, want config.yaml: %s", payload["location"], stdout) + } + if !strings.Contains(stderr, "Backup config removed") { + t.Errorf("a real removal dropped its side-effect hint:\n%s", stderr) + } + + // database-backed route: its payload names the layers it cleared. + run("set", "no-hooks", "true") + stdout, _ = run("unset", "--json", "no-hooks") + payload = decode(stdout) + if loc, _ := payload["location"].(string); !strings.Contains(loc, "database") { + t.Errorf("database-backed --json payload location = %v, want it to name the database: %s", payload["location"], stdout) + } + }) + // ===== Validate ===== // Note: config validate checks dolt server connectivity which doesn't // apply to embedded mode, so we skip it here. diff --git a/cmd/bd/dolt.go b/cmd/bd/dolt.go index c7a3e4633..b6764cb75 100644 --- a/cmd/bd/dolt.go +++ b/cmd/bd/dolt.go @@ -1824,7 +1824,7 @@ var doltRemoteRemoveCmd = &cobra.Command{ if name == "origin" { if current := config.GetYamlConfig("sync.remote"); current != "" { - if err := config.UnsetYamlConfig("sync.remote"); err != nil { + if _, err := config.UnsetYamlConfig("sync.remote"); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to clear sync.remote from config.yaml: %v\n", err) } if isGitRepo() { diff --git a/cmd/bd/dolt_remote_proxied_server.go b/cmd/bd/dolt_remote_proxied_server.go index 1ed7f9469..b4b8e165e 100644 --- a/cmd/bd/dolt_remote_proxied_server.go +++ b/cmd/bd/dolt_remote_proxied_server.go @@ -31,7 +31,7 @@ func runDoltRemoteRemoveProxied(ctx context.Context, name string) error { if name == "origin" { if current := config.GetYamlConfig("sync.remote"); current != "" { - if err := config.UnsetYamlConfig("sync.remote"); err != nil { + if _, err := config.UnsetYamlConfig("sync.remote"); err != nil { fmt.Fprintf(os.Stderr, "Warning: failed to clear sync.remote from config.yaml: %v\n", err) } if isGitRepo() { diff --git a/internal/config/user_config_path_test.go b/internal/config/user_config_path_test.go index 33b2cd020..07056704d 100644 --- a/internal/config/user_config_path_test.go +++ b/internal/config/user_config_path_test.go @@ -215,7 +215,7 @@ func TestRelativeUserRootsNeverReachImplicitOrExplicitFilesystemPaths(t *testing t.Errorf("SetUserYamlConfig error %q does not name %s", setErr, variable) } } - if err := UnsetUserYamlConfig("metrics.disabled"); err == nil { + if _, err := UnsetUserYamlConfig("metrics.disabled"); err == nil { t.Fatal("UnsetUserYamlConfig returned nil error for unsafe roots") } diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index 50c2f436a..8cf62abfb 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -2,6 +2,7 @@ package config import ( "bufio" + "errors" "fmt" "os" "os/exec" @@ -15,6 +16,14 @@ import ( "gopkg.in/yaml.v3" ) +// ErrNoProjectConfigYaml reports that the workspace has no project config.yaml +// for a YAML-only write to edit - an external BEADS_DIR, or a database-only +// workspace. It is a sentinel because the answer is caller-dependent: a caller +// whose whole job is the YAML edit has failed and must say so, while a caller +// mirroring a database write into the YAML layer has nothing to clear and has +// not. +var ErrNoProjectConfigYaml = errors.New("no project config.yaml in this workspace") + // YamlOnlyKeys are configuration keys that must be stored in config.yaml // rather than the database. These are "startup" settings that are // read before the database is opened. @@ -617,32 +626,49 @@ func MetricsNoticeShownByUserConfig() bool { return shown } -func UnsetUserYamlConfig(key string) error { +// UnsetUserYamlConfig comments out key in the user-global config.yaml. The bool +// reports whether the file was actually changed, on the same terms as +// UnsetYamlConfig: an absent file or an absent key is (false, nil), not an +// error. +func UnsetUserYamlConfig(key string) (bool, error) { configPath, err := UserConfigYamlPath() if err != nil { - return err + return false, err } content, err := os.ReadFile(configPath) //nolint:gosec // configPath is a validated absolute user config path if err != nil { if os.IsNotExist(err) { - return nil + return false, nil } - return fmt.Errorf("failed to read user config.yaml: %w", err) + return false, fmt.Errorf("failed to read user config.yaml: %w", err) } - newContent, err := commentOutYamlKey(string(content), key) + // Normalize before the call, not after it. commentOutYamlKey rewrites CRLF + // to LF as its first statement, so comparing its result against the raw + // bytes asks whether the bytes moved rather than whether the key was + // commented out: on any CRLF file those always differ, and an unset of an + // absent key reported "Unset (in config.yaml)" — the false claim this + // path exists to remove — while rewriting the whole file to LF. Comparing + // against the same normalized form the callee answered from keeps the bool + // meaning "the key was there", and skipping the write on false leaves a + // CRLF file byte-identical. + normalized := strings.ReplaceAll(string(content), "\r\n", "\n") + newContent, err := commentOutYamlKey(normalized, key) if err != nil { - return err + return false, err + } + if newContent == normalized { + return false, nil } // Preserve the owner-private 0600 posture every other user-global writer // uses (SetUserYamlConfig, setYamlConfigAtPath, the metrics bootstrap); // rewriting at 0644 would relax this shared user config to world-readable. if err := os.WriteFile(configPath, []byte(newContent), 0o600); err != nil { //nolint:gosec // configPath is from UserConfigYamlPath - return fmt.Errorf("failed to write user config.yaml: %w", err) + return false, fmt.Errorf("failed to write user config.yaml: %w", err) } - return nil + return true, nil } func SetUserYamlConfig(key, value string) error { @@ -696,29 +722,56 @@ func GetYamlConfig(key string) string { return v.GetString(key) } -// UnsetYamlConfig removes a configuration value from the project's config.yaml file. -// The key line is commented out (prefixed with "# ") to preserve it as documentation. -func UnsetYamlConfig(key string) error { +// UnsetYamlConfig removes a configuration value from the project's config.yaml +// file. The key line is commented out (prefixed with "# ") to preserve it as +// documentation. +// +// The bool reports whether the file was actually changed, so a caller can name +// where the unset landed rather than asserting a write it did not make. It is +// false, with a nil error, when the key is not present in the file: "the key is +// not in config.yaml" is the correct answer to give there, not a failure. +// +// A workspace with no project config.yaml at all is reported separately, as +// ErrNoProjectConfigYaml, because the callers need different answers for it and +// only they can tell which applies. The two `bd config unset` routes need +// opposite ones — the yaml-only route reports it, the database-backed route +// tolerates it via errors.Is, since there the database write is the whole unset. +// The two `sync.remote` cleanup sites (cmd/bd/dolt.go and +// cmd/bd/dolt_remote_proxied_server.go) take a third: they deliberately keep +// their base warn-on-any-error behavior. Resolution stays here so none of them +// re-derives it: pre-checking in the caller is what produced the false +// "(in config.yaml)" claims this whole path exists to remove. +func UnsetYamlConfig(key string) (bool, error) { configPath, err := findProjectConfigYaml() if err != nil { - return err + return false, fmt.Errorf("%w: %w", ErrNoProjectConfigYaml, err) } content, err := os.ReadFile(configPath) //nolint:gosec // configPath is from findProjectConfigYaml if err != nil { - return fmt.Errorf("failed to read config.yaml: %w", err) + if os.IsNotExist(err) { + return false, nil + } + return false, fmt.Errorf("failed to read config.yaml: %w", err) } - newContent, err := commentOutYamlKey(string(content), key) + // Normalize before the call so the comparison below answers "was the key + // commented out", not "did the bytes move" — see UnsetUserYamlConfig for + // the CRLF false-positive this avoids. + normalized := strings.ReplaceAll(string(content), "\r\n", "\n") + newContent, err := commentOutYamlKey(normalized, key) if err != nil { - return err + return false, err + } + if newContent == normalized { + return false, nil } if err := os.WriteFile(configPath, []byte(newContent), 0600); err != nil { //nolint:gosec // configPath is validated - return fmt.Errorf("failed to write config.yaml: %w", err) + return false, fmt.Errorf("failed to write config.yaml: %w", err) } - return nil + return true, nil } // findProjectConfigYaml finds the active config.yaml path for YAML-only config writes. @@ -1104,6 +1157,13 @@ func scalarStyleFor(value string) yaml.Style { } func commentOutYamlKey(content, key string) (string, error) { + // Normalize CRLF up front. The old implementation scanned with + // bufio.Scanner, whose ScanLines drops a trailing "\r", so a CRLF file came + // back LF-only; splitting on "\n" instead (below, to keep the file's final + // newline) would otherwise carry every "\r" through and change that + // long-standing behavior. + content = strings.ReplaceAll(content, "\r\n", "\n") + if err := unsupportedUnsetShape(content, key); err != nil { return "", err } @@ -1125,10 +1185,19 @@ func commentOutYamlKey(content, key string) (string, error) { // `remote: keep-this` inside it is prose, and commenting it out edits their // data. Matching by line has to skip those lines to stay honest. blockIndent := -1 - scanner := bufio.NewScanner(strings.NewReader(content)) - for scanner.Scan() { - line := scanner.Text() - + // Split rather than scan so the file's trailing newline survives: a + // scanner drops it, so every unset rewrote the file without its final + // newline, and the unset callers could not tell "nothing matched" from a + // write by comparing content. + lines := strings.Split(content, "\n") + // A flat key is a TOP-LEVEL key whose name contains the dots, so it only + // matches at the document's top-level indentation. Without this, a + // single-segment key such as `enabled` matched a nested ` enabled:` line + // under some other section and commented out a key the caller never named. + // The top level is usually column 0, but yaml.v3 accepts a document whose + // whole mapping is indented, so it is read from the first key line. + topIndent := yamlTopLevelIndent(lines) + for i, line := range lines { if blockIndent >= 0 { if strings.TrimSpace(line) == "" || lineIndent(line) > blockIndent { result = append(result, line) @@ -1138,12 +1207,18 @@ func commentOutYamlKey(content, key string) (string, error) { } blockIndent = blockScalarIndent(line) - if matches := flatPattern.FindStringSubmatch(line); matches != nil { + if matches := flatPattern.FindStringSubmatch(line); matches != nil && len(matches[1]) == topIndent { + if err := mappingValueUnsetRefusal(lines, i, len(matches[1]), key); err != nil { + return "", err + } result = append(result, matches[1]+"# "+strings.TrimLeft(line, " \t")) continue } if name, indent, ok := yamlKeyOnLine(line); ok && walk.step(name, indent) { + if err := mappingValueUnsetRefusal(lines, i, indent, key); err != nil { + return "", err + } result = append(result, strings.Repeat(" ", indent)+"# "+strings.TrimLeft(line, " \t")) continue } @@ -1172,6 +1247,72 @@ func commentOutYamlKey(content, key string) (string, error) { return strings.TrimRight(out, "\n") + content[len(strings.TrimRight(content, "\n")):], nil } +// mappingValueUnsetRefusal refuses a matched key line whose value continues on +// the lines beneath it rather than sitting on the key's own line: a nested +// mapping, a list, or a plain scalar continued on the next line. Commenting +// that one line out would orphan those lines, leaving a config.yaml that no +// longer parses. The value is judged by the first following line that carries +// content, so a key with an empty value and nothing under it is still +// commented as before. +// +// Nothing on the key's own line but a comment, an anchor or a tag (`b: # +// note`, `base: &b`, `m: !!map`) still leaves the value to the lines beneath. +// A list may sit at the key's OWN indentation (`types.custom:` then `- step`), +// which YAML allows for a sequence value, so a `-` item at that indentation +// belongs to the key too. +func mappingValueUnsetRefusal(lines []string, idx, indent int, key string) error { + _, rest, _ := strings.Cut(strings.TrimLeft(lines[idx], " \t"), ":") + if !valueContinuesBelow(rest) { + return nil + } + for _, next := range lines[idx+1:] { + // TrimSpace, not TrimLeft: a CRLF file's blank line is a lone "\r", + // which must not read as content at column 0 and end the value early. + trimmed := strings.TrimSpace(next) + if trimmed == "" || strings.HasPrefix(trimmed, "#") { + continue + } + nextIndent := lineIndent(next) + if nextIndent < indent { + return nil + } + if nextIndent == indent && trimmed != "-" && !strings.HasPrefix(trimmed, "- ") { + return nil + } + return fmt.Errorf("cannot unset %q: its value continues on the lines beneath it, and commenting the key out would orphan them; remove it by hand, or unset its keys individually if it is a mapping", key) + } + return nil +} + +// valueContinuesBelow reports whether the text after a key's colon leaves the +// value to the following lines: it is empty once a trailing comment is dropped, +// or holds only anchors and tags. +func valueContinuesBelow(rest string) bool { + rest = strings.TrimSpace(rest) + if i := strings.Index(rest, " #"); i >= 0 { + rest = rest[:i] + } else if strings.HasPrefix(rest, "#") { + rest = "" + } + for _, tok := range strings.Fields(rest) { + if !strings.HasPrefix(tok, "&") && !strings.HasPrefix(tok, "!") { + return false + } + } + return true +} + +// yamlTopLevelIndent is the indentation of the document's first key line, which +// every top-level key shares, or 0 when the document declares no key. +func yamlTopLevelIndent(lines []string) int { + for _, line := range lines { + if _, indent, ok := yamlKeyOnLine(line); ok { + return indent + } + } + return 0 +} + // nestedKeyWalk tracks how much of a dotted key a line-by-line scan has matched // so far, so a leaf is commented out only when it is nested under its OWN // parents rather than under some other section that happens to repeat a name. @@ -1245,17 +1386,20 @@ func (w *nestedKeyWalk) step(name string, indent int) bool { // direction already refuses what it cannot write correctly, so unset says so too. // // This is a list of known shapes, not a decision procedure for the whole class: -// a mapping-valued key and a value that spans lines in flow or quoted style land -// their bodies in the same place and are still silent. +// a value that spans lines in flow or quoted style lands its body in the same +// place and is still silent. A mapping-valued key is refused by the line +// matcher itself (mappingValueUnsetRefusal), which also covers single-segment +// keys this function never walks. // // Only a key that is actually present can be refused: unsetting a key that was // never there has always been a successful no-op, and staying silent about a // shape nobody asked to touch is the point. func unsupportedUnsetShape(content, key string) error { + // Single-segment keys are checked too. The database-backed unset clears + // the config.yaml layer for every key, so `notes: |` with an indented body + // reaches this path, and commenting its key line out orphans the body + // exactly as it does for a dotted key. segments := strings.Split(key, ".") - if len(segments) < 2 { - return nil - } var root yaml.Node if err := yaml.Unmarshal([]byte(content), &root); err != nil || len(root.Content) == 0 { // Nothing to walk. A file yaml.v3 cannot parse is not this function's diff --git a/internal/config/yaml_config_test.go b/internal/config/yaml_config_test.go index 72d2244e1..897f66485 100644 --- a/internal/config/yaml_config_test.go +++ b/internal/config/yaml_config_test.go @@ -1157,6 +1157,77 @@ func TestCommentOutYamlKey(t *testing.T) { key: "backup.enabled", expected: " # backup.enabled: true\nother: value", }, + { + // A flat key is a top-level key. A single-segment key must not + // match the same name nested under some other section: that is a + // different key, and the database-backed unset path would + // otherwise comment out backup.enabled to unset `enabled`. + name: "single-segment key does not match a nested line", + content: "backup:\n enabled: true\n", + key: "enabled", + expected: "backup:\n enabled: true\n", + }, + { + // Nor refuse on one: the nested `mode:` block is not this key's. + // A dedent ends the value: `k:` has nothing beneath it, since the + // next content line belongs to the parent's level, so it is + // commented like any empty-valued key. + name: "empty nested key followed by a dedent is still commented", + content: "outer:\n k:\nz: 1\n", + key: "outer.k", + expected: "outer:\n # k:\nz: 1\n", + }, + { + name: "single-segment key ignores a nested block of the same name", + content: "dolt:\n mode:\n x: 1\n", + key: "mode", + expected: "dolt:\n mode:\n x: 1\n", + }, + { + name: "nested key preserves siblings and comments", + content: "# Backup settings\nbackup:\n enabled: false\n interval: 15m\n", + key: "backup.enabled", + expected: "# Backup settings\nbackup:\n # enabled: false\n interval: 15m\n", + }, + { + name: "nested key three levels deep", + content: "a:\n b:\n c: 1\n", + key: "a.b.c", + expected: "a:\n b:\n # c: 1\n", + }, + { + name: "key with an empty value and no block is still commented", + content: "actor:\nother: value", + key: "actor", + expected: "# actor:\nother: value", + }, + { + name: "key with an empty value at end of file is still commented", + content: "other: value\nactor:", + key: "actor", + expected: "other: value\n# actor:", + }, + { + // A match must keep the file's trailing newline, and so must a + // miss: UnsetYamlConfig reports a write by comparing content, so a + // no-op that dropped the final newline would read as a change. + name: "flat key preserves the trailing newline", + content: "a: 1\n", + key: "a", + expected: "# a: 1\n", + }, + { + name: "no match preserves the trailing newline", + content: "other: value\n", + key: "backup.enabled", + expected: "other: value\n", + }, + { + name: "flat key without a trailing newline stays without one", + content: "a: 1", + key: "a", + expected: "# a: 1", + }, } for _, tt := range tests { @@ -1214,9 +1285,13 @@ other-setting: value defer os.Chdir(oldWd) // Test UnsetYamlConfig - if err := UnsetYamlConfig("backup.enabled"); err != nil { + changed, err := UnsetYamlConfig("backup.enabled") + if err != nil { t.Fatalf("UnsetYamlConfig() error = %v", err) } + if !changed { + t.Error("UnsetYamlConfig() changed = false, want true for a key present in config.yaml") + } // Read back and verify content, err := os.ReadFile(configPath) @@ -1340,9 +1415,13 @@ func TestSetAndUnsetYamlConfig_WithBEADS_DIR_FromOutsideRepo(t *testing.T) { t.Fatalf("expected runtime config to contain no-git-ops: true, got:\n%s", contentStr) } - if err := UnsetYamlConfig("no-git-ops"); err != nil { + changed, err := UnsetYamlConfig("no-git-ops") + if err != nil { t.Fatalf("UnsetYamlConfig() error = %v", err) } + if !changed { + t.Error("UnsetYamlConfig() changed = false, want true for a key present in config.yaml") + } content, err = os.ReadFile(configPath) if err != nil { t.Fatalf("Failed to read runtime config.yaml after unset: %v", err) diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go index ab0d0ef58..df464551a 100644 --- a/internal/config/yaml_nested_roundtrip_test.go +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -424,9 +424,16 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) { wasSet := GetStringFromDir(dir, tc.key) != "" t.Setenv("BEADS_DIR", dir) - if err := UnsetYamlConfig(tc.key); err != nil { + changed, err := UnsetYamlConfig(tc.key) + if err != nil { t.Fatalf("UnsetYamlConfig: %v", err) } + // The bool is what `bd config unset` reports as the location, so + // it must say a write happened exactly when there was a key to + // comment out. + if changed != wasSet { + t.Errorf("UnsetYamlConfig changed = %v, want %v", changed, wasSet) + } body, err := os.ReadFile(path) if err != nil { @@ -475,6 +482,31 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { // so the whole file stops parsing rather than one section. {name: "block scalar under the flat spelling", seed: "dolt.host: |\n 10.0.0.1\nnode_id: mini\n", key: "dolt.host", want: "block scalar"}, {name: "folded block scalar under the flat spelling", seed: "dolt.host: >\n 10.0.0.1\nnode_id: mini\n", key: "dolt.host", want: "block scalar"}, + // A key whose value is the mapping beneath it: commenting its line out + // orphans the children at an indentation no key introduces, and the + // file stops parsing. Nested, flat single-segment, and a list value. + {name: "mapping-valued nested key", seed: "dolt:\n limits:\n host: mini\n", key: "dolt.limits", want: "lines beneath it"}, + {name: "mapping-valued single-segment key", seed: "backup:\n enabled: false\n interval: 15m\n", key: "backup", want: "lines beneath it"}, + {name: "mapping-valued key behind a comment", seed: "backup:\n # whether to back up\n enabled: false\n", key: "backup", want: "lines beneath it"}, + {name: "list-valued nested key", seed: "sync:\n remotes:\n - a\n - b\n", key: "sync.remotes", want: "lines beneath it"}, + // YAML lets a sequence value sit at the key's own indentation; the + // items still belong to the key, and orphaning them leaves a file that + // every later command fails to parse (#5760 review). + {name: "list at the key's own indentation", seed: "types.custom:\n- step\n- slot\n", key: "types.custom", want: "lines beneath it"}, + // Only a comment, an anchor or a tag after the colon still leaves the + // value to the lines beneath. + {name: "trailing comment on a mapping-valued key", seed: "backup: # note\n enabled: false\n", key: "backup", want: "lines beneath it"}, + {name: "anchored mapping-valued key", seed: "base: &b\n c: 1\n", key: "base", want: "lines beneath it"}, + {name: "anchored mapping-valued key with a comment", seed: "base: &b # shared\n c: 1\n", key: "base", want: "lines beneath it"}, + // A tag alone after the colon still leaves the value below. + {name: "tagged mapping-valued key", seed: "m: !!map\n a: 1\n", key: "m", want: "lines beneath it"}, + // A single-segment key's block scalar is refused like a dotted one's: + // the database-backed unset reaches this path for every key. + {name: "single-segment block scalar", seed: "notes: |\n hi\nnode_id: mini\n", key: "notes", want: "block scalar"}, + // A plain scalar continued on the next line is orphaned the same way. + {name: "plain scalar on the next line", seed: "dolt.mode:\n server\n", key: "dolt.mode", want: "lines beneath it"}, + // A CRLF blank line is a lone carriage return, not content at column 0. + {name: "mapping behind a CRLF blank line", seed: "backup:\r\n\r\n enabled: false\r\n", key: "backup", want: "lines beneath it"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { @@ -485,7 +517,7 @@ func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { } t.Setenv("BEADS_DIR", dir) - err := UnsetYamlConfig(tc.key) + _, err := UnsetYamlConfig(tc.key) if err == nil { body, _ := os.ReadFile(path) //nolint:errcheck // diagnostic t.Fatalf("unsetting %s in a %s reported success\n%s", tc.key, tc.name, body) @@ -517,9 +549,13 @@ func TestUnsetOfAnAbsentKeyStillSucceeds(t *testing.T) { } t.Setenv("BEADS_DIR", dir) - if err := UnsetYamlConfig("sync.remote"); err != nil { + changed, err := UnsetYamlConfig("sync.remote") + if err != nil { t.Fatalf("UnsetYamlConfig on an unset key: %v", err) } + if changed { + t.Error("UnsetYamlConfig on an unset key reported a write") + } body, err := os.ReadFile(path) if err != nil { t.Fatalf("read: %v", err) @@ -561,7 +597,7 @@ func TestSettingADottedKeyAgainAfterUnsetStaysNested(t *testing.T) { if err := SetYamlConfigInDir(dir, "sync.remote", "file:///a.git"); err != nil { t.Fatalf("first set: %v", err) } - if err := UnsetYamlConfig("sync.remote"); err != nil { + if _, err := UnsetYamlConfig("sync.remote"); err != nil { t.Fatalf("unset: %v", err) } if got := GetStringFromDir(dir, "sync.remote"); got != "" { @@ -696,7 +732,7 @@ func TestUnsetLeavesBlockScalarTextAlone(t *testing.T) { } t.Setenv("BEADS_DIR", dir) - if err := UnsetYamlConfig("sync.remote"); err != nil { + if _, err := UnsetYamlConfig("sync.remote"); err != nil { t.Fatalf("UnsetYamlConfig: %v", err) } diff --git a/internal/config/yaml_unset_newline_test.go b/internal/config/yaml_unset_newline_test.go index 16552a557..56064c3d2 100644 --- a/internal/config/yaml_unset_newline_test.go +++ b/internal/config/yaml_unset_newline_test.go @@ -15,8 +15,10 @@ import ( // in someone's review. // // The guarantee is scoped to LF documents. A CRLF file is still normalized to -// LF by any unset — that predates this fix and is pinned below as -// known-and-accepted rather than quietly claimed away. +// LF by an unset that actually comments a key out — that predates this fix and +// is pinned below as known-and-accepted rather than quietly claimed away. An +// unset that changes nothing no longer rewrites the file at all; see +// TestUnsetOfAnAbsentKeyLeavesACRLFFileUntouched. func TestCommentOutYamlKeyPreservesTheTrailingNewline(t *testing.T) { for _, tc := range []struct { name, content, key string @@ -91,7 +93,7 @@ func TestUnsetThroughTheFileKeepsTheTrailingNewline(t *testing.T) { t.Fatal(err) } t.Setenv("BEADS_DIR", beadsDir) - if err := UnsetYamlConfig("dolt.mode"); err != nil { + if _, err := UnsetYamlConfig("dolt.mode"); err != nil { t.Fatalf("UnsetYamlConfig: %v", err) } after, err := os.ReadFile(path) @@ -103,3 +105,149 @@ func TestUnsetThroughTheFileKeepsTheTrailingNewline(t *testing.T) { t.Errorf("unset rewrote the end of the file: trailing run %q, want %q\n%q", got, want, string(after)) } } + +// An unset of a key the document does not contain must not report a write. The +// bool that says so used to come from comparing commentOutYamlKey's result +// against the raw file bytes, and those sit on opposite sides of the CRLF +// normalization the callee applies unilaterally — so on a CRLF file the two +// strings always differed and every unset looked like a change. `bd config +// unset` then printed "Unset (in config.yaml)" for a key that was never +// there, the exact false claim this command's fix set out to remove, and +// rewrote the whole file to LF on the way past. Windows is a supported +// platform and every other change-report fixture in this package is LF, so +// nothing else in-tree can catch a regression here. +// +// Both public callers had the shape, so both are pinned. +func TestUnsetOfAnAbsentKeyLeavesACRLFFileUntouched(t *testing.T) { + const body = "issue_prefix: vp\r\nexport.auto: true\r\n" + + assertUntouched := func(t *testing.T, path string) { + t.Helper() + after, err := os.ReadFile(path) //nolint:gosec // test-owned temp path + if err != nil { + t.Fatal(err) + } + if string(after) != body { + t.Errorf("a no-op unset rewrote the file:\ngot: %q\nwant: %q", string(after), body) + } + } + + t.Run("UnsetYamlConfig", func(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatal(err) + } + path := filepath.Join(beadsDir, "config.yaml") + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_DIR", beadsDir) + + changed, err := UnsetYamlConfig("dolt.mode") + if err != nil { + t.Fatalf("UnsetYamlConfig: %v", err) + } + if changed { + t.Error("UnsetYamlConfig() changed = true for a key the file does not contain") + } + assertUntouched(t, path) + }) + + t.Run("UnsetUserYamlConfig", func(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + path, err := UserConfigYamlPath() + if err != nil { + t.Fatalf("UserConfigYamlPath: %v", err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + + changed, err := UnsetUserYamlConfig("dolt.mode") + if err != nil { + t.Fatalf("UnsetUserYamlConfig: %v", err) + } + if changed { + t.Error("UnsetUserYamlConfig() changed = true for a key the file does not contain") + } + assertUntouched(t, path) + }) +} + +// The other direction of the same compare. Normalizing before the call is what +// keeps the absent-key case above a no-op, but it also means each caller now +// decides between "write" and "was not set" on content it has already rewritten +// in memory. A caller that returned early whenever that normalization changed +// anything would pass every test above and turn each unset on a CRLF +// config.yaml into a silent " was not set" while the key stayed effective. +// The absent-key cell only proves the no-op direction, and the commentOutYamlKey +// CRLF pin sits below the decision, so neither can see that. +// +// The written file comes back LF throughout. That is base behavior, pinned here +// through both callers as known-and-accepted, like the callee-level pin above. +func TestUnsetOfAPresentKeyInACRLFFileReportsTheWrite(t *testing.T) { + const ( + body = "issue_prefix: vp\r\ndolt.mode: server\r\nexport.auto: true\r\n" + want = "issue_prefix: vp\n# dolt.mode: server\nexport.auto: true\n" + ) + + assertWritten := func(t *testing.T, path string) { + t.Helper() + after, err := os.ReadFile(path) //nolint:gosec // test-owned temp path + if err != nil { + t.Fatal(err) + } + if string(after) != want { + t.Errorf("the unset did not write the key out:\ngot: %q\nwant: %q", string(after), want) + } + } + + t.Run("UnsetYamlConfig", func(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatal(err) + } + path := filepath.Join(beadsDir, "config.yaml") + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("BEADS_DIR", beadsDir) + + changed, err := UnsetYamlConfig("dolt.mode") + if err != nil { + t.Fatalf("UnsetYamlConfig: %v", err) + } + if !changed { + t.Error("UnsetYamlConfig() changed = false for a key the file contains") + } + assertWritten(t, path) + }) + + t.Run("UnsetUserYamlConfig", func(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + path, err := UserConfigYamlPath() + if err != nil { + t.Fatalf("UserConfigYamlPath: %v", err) + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + + changed, err := UnsetUserYamlConfig("dolt.mode") + if err != nil { + t.Fatalf("UnsetUserYamlConfig: %v", err) + } + if !changed { + t.Error("UnsetUserYamlConfig() changed = false for a key the file contains") + } + assertWritten(t, path) + }) +}