diff --git a/CHANGELOG.md b/CHANGELOG.md index a80acb144..9626c205d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -165,6 +165,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 checks in embedded, server, and proxied-server command paths; the legacy `:` await value remains accepted for compatibility. +### Fixed + +- **A dotted config key now round-trips: what `bd config set` writes, + `bd config get` and bd's own readers find** + ([#6574](https://github.com/gastownhall/beads/pull/6574), + [#6594](https://github.com/gastownhall/beads/issues/6594), bd-zj95). Setting a + key like `sync.remote` or `dolt.host` into a `config.yaml` with no matching + section appended a key whose *name* contained the dot — a top-level + `sync.remote: "..."` — instead of nesting it under `sync:`. Viper finds a key + spelled that way; the direct reader `GetStringFromDir` splits on the dot and + walks nested mappings, so it does not. The value was set and invisible at the + same time depending on which reader asked, and `bd init --remote` in a fresh + workspace was the ordinary way in: the remote was recorded and then not found. + New dotted keys are written nested now. An existing flat spelling is updated + in place for compatibility with other writers of the shared config file — + rewriting that one line and nothing else, the comment on it included — and + bd's direct reader recognizes both forms with the same precedence as Viper. + `bd doctor` warns when both forms coexist. `bd config unset` removes the + nested form — it only ever matched the flat spelling, so unsetting + `sync.remote` silently left the remote live. Keys the write does not own, + including dotted ones somebody else put there, are left exactly as they are. + + Four things that used to succeed now report an error instead, because each one + produced a value no reader could see, or claimed to remove a value it had + left live. On `bd config set`: a key under a parent that already holds a value + (`sync: enabled`, then `bd config set sync.remote`), and a key in a file whose + top level is not a mapping. On `bd config unset`: a key inside a flow-style + mapping (`sync: {remote: "..."}`), which bd matches by line and so cannot + edit, and a key whose value is a block scalar (`remote: |`, in either + spelling), where commenting the key out would leave the body behind as a value + of its own. Unsetting a key that is not set remains a successful no-op in + every shape. + ## [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/bootstrap_guard_test.go b/cmd/bd/bootstrap_guard_test.go index 6a3df5560..40df292a3 100644 --- a/cmd/bd/bootstrap_guard_test.go +++ b/cmd/bd/bootstrap_guard_test.go @@ -79,13 +79,7 @@ func TestIsGitCodeRepoURL(t *testing.T) { // Returns a cleanup function that resets config state. func setupSyncRemoteConfig(t *testing.T, beadsDir, remote string) func() { t.Helper() - if err := os.WriteFile( - filepath.Join(beadsDir, "config.yaml"), - []byte("sync.remote: "+remote+"\n"), - 0o644, - ); err != nil { - t.Fatalf("write config.yaml: %v", err) - } + seedSyncRemote(t, beadsDir, remote) config.ResetForTesting() t.Setenv("BEADS_DIR", beadsDir) t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") diff --git a/cmd/bd/bootstrap_test.go b/cmd/bd/bootstrap_test.go index a8d944165..c590f6d24 100644 --- a/cmd/bd/bootstrap_test.go +++ b/cmd/bd/bootstrap_test.go @@ -355,9 +355,7 @@ func TestDetectBootstrapAction_ExplicitSyncRemotePreservesRemotesAPIURL(t *testi t.Fatal(err) } const syncRemote = "http://myserver:7007/mydb" - if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("sync.remote: "+syncRemote+"\n"), 0o644); err != nil { - t.Fatal(err) - } + seedSyncRemote(t, beadsDir, syncRemote) t.Setenv("BEADS_DIR", beadsDir) t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") if err := config.Initialize(); err != nil { @@ -407,9 +405,7 @@ func TestDetectBootstrapAction_ExistingEmbeddedDBWithSyncRemoteIsNoOp(t *testing t.Fatal(err) } const syncRemote = "http://myserver:7007/mydb" - if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("sync.remote: "+syncRemote+"\n"), 0o644); err != nil { - t.Fatal(err) - } + seedSyncRemote(t, beadsDir, syncRemote) t.Setenv("BEADS_DIR", beadsDir) t.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1") if err := config.Initialize(); err != nil { @@ -1327,8 +1323,18 @@ func TestFinalizeSyncedBootstrapWritesConfigFiles(t *testing.T) { // sync.remote must be persisted so subsequent fresh clones (and // bootstrap retries) can rediscover the remote without re-probing // origin refs. - if !strings.Contains(yaml, "sync.remote: ") && !strings.Contains(yaml, "sync-remote: ") { - t.Errorf("config.yaml does not contain sync.remote entry:\n%s", yaml) + // + // Asserted by READING it back rather than by matching a spelling. This used + // to require a literal `sync.remote: ` line, which is a key whose name + // contains a dot — the shape config.GetStringFromDir and viper can never + // find, because both split on the dot and walk nested mappings. The writer + // now nests (bd-zj95), so the old assertion passed for exactly as long as + // the value was unreadable and failed the moment it became readable. What + // bootstrap owes its caller is a remote that can be read back, so that is + // what this checks. + if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != syncRemote { + t.Errorf("config.GetStringFromDir(%q, \"sync.remote\") = %q, want %q:\n%s", + beadsDir, got, syncRemote, yaml) } if !strings.Contains(yaml, syncRemote) { t.Errorf("config.yaml does not contain sync remote URL %q:\n%s", syncRemote, yaml) diff --git a/cmd/bd/doctor/config_values.go b/cmd/bd/doctor/config_values.go index 2038dcc17..a7673de12 100644 --- a/cmd/bd/doctor/config_values.go +++ b/cmd/bd/doctor/config_values.go @@ -13,6 +13,7 @@ import ( "github.com/steveyegge/beads/internal/beads" "github.com/steveyegge/beads/internal/configfile" "github.com/steveyegge/beads/internal/storage/dolt" + "gopkg.in/yaml.v3" ) // validRoutingModes are the allowed values for routing.mode @@ -188,6 +189,9 @@ func checkYAMLConfigValues(repoPath string) []string { issues = append(issues, fmt.Sprintf("config.yaml: failed to parse: %v", err)) return issues } + if data, err := os.ReadFile(configPath); err == nil { //nolint:gosec // resolved workspace config + issues = append(issues, findDualSpelledConfigKeys(data)...) + } // Validate issue-prefix (should be alphanumeric with dashes/underscores, reasonably short) if v.IsSet("issue-prefix") { @@ -296,6 +300,59 @@ func checkYAMLConfigValues(repoPath string) []string { return issues } +func findDualSpelledConfigKeys(data []byte) []string { + var root yaml.Node + if err := yaml.Unmarshal(data, &root); err != nil || len(root.Content) == 0 { + return nil + } + mapping := root.Content[0] + if mapping.Kind != yaml.MappingNode { + return nil + } + + var issues []string + for i := 0; i+1 < len(mapping.Content); i += 2 { + key := mapping.Content[i].Value + parts := strings.Split(key, ".") + if len(parts) < 2 || !yamlPathExists(mapping, parts) { + continue + } + issues = append(issues, fmt.Sprintf( + "config.yaml: %q is present in both flat and nested spelling; remove one spelling so all readers use the same value", + key, + )) + } + return issues +} + +func yamlPathExists(mapping *yaml.Node, parts []string) bool { + current := mapping + for i, part := range parts { + if current.Kind != yaml.MappingNode { + return false + } + idx := yamlMappingChild(current, part) + if idx == -1 { + return false + } + if i == len(parts)-1 { + return true + } + current = current.Content[idx+1] + } + return false +} + +func yamlMappingChild(mapping *yaml.Node, name string) int { + for i := 0; i+1 < len(mapping.Content); i += 2 { + key := mapping.Content[i] + if key.Kind == yaml.ScalarNode && key.Value == name { + return i + } + } + return -1 +} + // isValidBoolString checks if a string represents a valid boolean value func isValidBoolString(s string) bool { lower := strings.ToLower(strings.TrimSpace(s)) diff --git a/cmd/bd/doctor/config_values_test.go b/cmd/bd/doctor/config_values_test.go index 54c96e911..86030fa27 100644 --- a/cmd/bd/doctor/config_values_test.go +++ b/cmd/bd/doctor/config_values_test.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "testing" ) @@ -136,6 +137,26 @@ func TestCheckConfigValues(t *testing.T) { }) } +func TestCheckConfigValuesWarnsOnFlatAndNestedSpellings(t *testing.T) { + tmpDir := t.TempDir() + beadsDir := filepath.Join(tmpDir, ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatalf("create .beads: %v", err) + } + configContent := "dolt.host: 127.0.0.1\ndolt:\n host: 10.0.0.1\n" + if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configContent), 0o644); err != nil { + t.Fatalf("write config.yaml: %v", err) + } + + check := CheckConfigValues(tmpDir) + if check.Status != StatusWarning { + t.Fatalf("expected warning, got %s: %s", check.Status, check.Detail) + } + if !strings.Contains(check.Detail, "dolt.host") || !strings.Contains(check.Detail, "flat and nested") { + t.Fatalf("warning does not identify the dual spelling: %s", check.Detail) + } +} + func TestCheckConfigValues_WorktreeFallbackUsesSharedConfig(t *testing.T) { mainRepoDir, worktreeDir := setupDoctorSharedWorktree(t) mainBeadsDir := filepath.Join(mainRepoDir, ".beads") diff --git a/cmd/bd/dolt_worktree_remote_test.go b/cmd/bd/dolt_worktree_remote_test.go index bbfc4efe8..e8f7eb36c 100644 --- a/cmd/bd/dolt_worktree_remote_test.go +++ b/cmd/bd/dolt_worktree_remote_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "runtime" "strconv" - "strings" "testing" + "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/testutil" ) @@ -53,13 +53,21 @@ func TestDoltRemoteAddPersistsSyncRemoteToSharedWorktreeConfig(t *testing.T) { t.Fatalf("bd dolt remote add from bare-parent worktree failed: %v\n%s", err, out) } + // Asserted by reading it back, not by matching YAML text. The literal + // `sync.remote: ""` this used to require is a key whose NAME contains + // a dot — the shape config.GetStringFromDir can never find, because it + // splits on the dot and walks nested mappings. Viper finds a dotted key + // either way, so this assertion was green while the value was invisible to + // bd's other reader; it passed BECAUSE bd-zj95 was present. What the remote + // add owes its caller is a remote that reads back, so that is what this + // checks. configPath := filepath.Join(bareBeadsDir, "config.yaml") content, err := os.ReadFile(configPath) if err != nil { t.Fatalf("failed to read shared config.yaml: %v", err) } - if !strings.Contains(string(content), `sync.remote: "`+remoteURL+`"`) { - t.Fatalf("expected shared config.yaml to contain sync.remote, got:\n%s", string(content)) + if got := config.GetStringFromDir(bareBeadsDir, "sync.remote"); got != remoteURL { + t.Fatalf("shared config.yaml sync.remote = %q, want %q; contents:\n%s", got, remoteURL, content) } if _, err := os.Stat(filepath.Join(worktreeDir, ".beads")); !os.IsNotExist(err) { diff --git a/cmd/bd/init_embedded_test.go b/cmd/bd/init_embedded_test.go index ee27208a0..942926755 100644 --- a/cmd/bd/init_embedded_test.go +++ b/cmd/bd/init_embedded_test.go @@ -16,6 +16,7 @@ import ( "testing" "time" + "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/configfile" "github.com/steveyegge/beads/internal/storage/embeddeddolt" "github.com/steveyegge/beads/internal/storage/schema" @@ -383,12 +384,18 @@ func TestEmbeddedInit(t *testing.T) { t.Fatalf("init without git origin should not configure a Dolt remote; remote list:\n%s", out) } - configYAML, err := os.ReadFile(filepath.Join(dir, ".beads", "config.yaml")) + // Asserted by reading it back, not by matching a spelling. The + // writer nests (bd-zj95), so "the substring `sync.remote:` is + // absent" is now satisfied by a config.yaml that DOES persist a + // remote — this guard would have sailed past the regression it + // exists to catch. + beadsDir := filepath.Join(dir, ".beads") + configYAML, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml")) if err != nil { t.Fatalf("read config.yaml: %v", err) } - if strings.Contains(string(configYAML), "sync.remote:") || strings.Contains(string(configYAML), "sync-remote:") { - t.Fatalf("init without git origin should not persist sync.remote; config.yaml:\n%s", configYAML) + if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != "" { + t.Fatalf("init without git origin persisted sync.remote = %q; config.yaml:\n%s", got, configYAML) } } }) @@ -924,12 +931,15 @@ func TestEmbeddedInit(t *testing.T) { t.Fatalf("stealth init should not synthesize a Dolt remote; remote list:\n%s", out) } - configYAML, err := os.ReadFile(filepath.Join(dir, ".beads", "config.yaml")) + // Read back rather than grep for a spelling: see the same guard in + // the no-git-origin case above. + beadsDir := filepath.Join(dir, ".beads") + configYAML, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml")) if err != nil { t.Fatalf("read config.yaml: %v", err) } - if strings.Contains(string(configYAML), "sync.remote:") || strings.Contains(string(configYAML), "sync-remote:") { - t.Fatalf("stealth init should not persist sync.remote; config.yaml:\n%s", configYAML) + if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != "" { + t.Fatalf("stealth init persisted sync.remote = %q; config.yaml:\n%s", got, configYAML) } } diff --git a/cmd/bd/init_remote_test.go b/cmd/bd/init_remote_test.go index 0243689dd..71aed22bf 100644 --- a/cmd/bd/init_remote_test.go +++ b/cmd/bd/init_remote_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/configfile" ) @@ -125,9 +126,8 @@ func TestPersistInitSyncRemoteExplicitRemoteWritesTargetDir(t *testing.T) { } callerConfig := filepath.Join(callerBeadsDir, "config.yaml") - if err := os.WriteFile(callerConfig, []byte("sync.remote: git+ssh://git@example.com/wrong/repo.git\n"), 0o600); err != nil { - t.Fatal(err) - } + const callerRemote = "git+ssh://git@example.com/wrong/repo.git" + seedSyncRemote(t, callerBeadsDir, callerRemote) targetConfig := filepath.Join(targetBeadsDir, "config.yaml") if err := os.WriteFile(targetConfig, []byte("# Beads Config\n"), 0o600); err != nil { t.Fatal(err) @@ -162,6 +162,56 @@ func TestPersistInitSyncRemoteExplicitRemoteWritesTargetDir(t *testing.T) { if strings.Contains(string(callerBytes), remote) { t.Fatalf("caller config.yaml was modified instead of target:\n%s", callerBytes) } + // Stronger than the absence above: the caller still resolves to what it had. + // A write that landed somewhere unreadable would satisfy "does not contain + // the new remote" while having quietly destroyed the old one. + if got := config.GetStringFromDir(callerBeadsDir, "sync.remote"); got != callerRemote { + t.Fatalf("caller sync.remote = %q, want it untouched at %q:\n%s", got, callerRemote, callerBytes) + } +} + +// `bd init --remote` runs createConfigYaml and then immediately writes +// sync.remote into what it just created. That template is comments and nothing +// else, which is the one document shape yaml.v3 parses to no nodes at all — so a +// writer that round-trips a node tree has nothing to write back but the key it +// added, and the operator's first commit after init carries a config.yaml with +// its entire documentation deleted. It is git-tracked, so they see it. +// +// This runs the real pair in the real order rather than asserting on a replica +// of the template, so it stays true if either side moves. +func TestInitPersistingSyncRemoteKeepsTheConfigTemplate(t *testing.T) { + beadsDir := filepath.Join(t.TempDir(), ".beads") + if err := os.MkdirAll(beadsDir, 0o750); err != nil { + t.Fatal(err) + } + if err := createConfigYaml(beadsDir, false, ""); err != nil { + t.Fatalf("createConfigYaml: %v", err) + } + template, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml")) + if err != nil { + t.Fatal(err) + } + + const remote = "git+ssh://git@example.com/right/repo.git" + if err := persistInitSyncRemote(beadsDir, remote, remote, false, true, false); err != nil { + t.Fatalf("persistInitSyncRemote: %v", err) + } + + body, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml")) + if err != nil { + t.Fatal(err) + } + for _, line := range strings.Split(string(template), "\n") { + if strings.TrimSpace(line) == "" { + continue + } + if !strings.Contains(string(body), line) { + t.Errorf("init dropped a line of its own config template: %q\ngot:\n%s", line, body) + } + } + if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != remote { + t.Errorf("sync.remote = %q, want %q\n%s", got, remote, body) + } } func TestInitTimeCloneConfigExternalDefaultsAreSelfContained(t *testing.T) { diff --git a/cmd/bd/sync_remote_fixture_test.go b/cmd/bd/sync_remote_fixture_test.go new file mode 100644 index 000000000..d5870dfba --- /dev/null +++ b/cmd/bd/sync_remote_fixture_test.go @@ -0,0 +1,48 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "github.com/steveyegge/beads/internal/config" +) + +// seedSyncRemote records a sync remote in beadsDir the way bd records one, and +// proves it is readable afterwards. +// +// The tests that use this used to hand-write `sync.remote: ` into +// config.yaml — a key whose NAME contains a dot. That spelling is not what bd's +// writer produces and not what config.GetStringFromDir can read: it splits on +// the dot and walks nested mappings. Viper happens to find a literal dotted key +// as well as a nested one, so those fixtures worked through one reader while +// being invisible to the other — the exact asymmetry bd-zj95 was, and the +// reason the fixtures were green while the bug was live. +// +// Writing through the real writer and asserting the read-back means these tests +// can no longer encode a spelling at all. If the round trip breaks again, the +// fixture fails here, naming the property, rather than some downstream +// assertion failing for a reason that looks unrelated. +func seedSyncRemote(t *testing.T, beadsDir, remote string) { + t.Helper() + path := filepath.Join(beadsDir, "config.yaml") + if _, err := os.Stat(path); os.IsNotExist(err) { + // SetYamlConfigInDir edits an existing file; it declines to create one, + // which is a deliberate guard against writing config into a directory + // that is not a workspace. + if writeErr := os.WriteFile(path, nil, 0o644); writeErr != nil { //nolint:gosec // test fixture + t.Fatalf("create config.yaml: %v", writeErr) + } + } else if err != nil { + t.Fatalf("stat config.yaml: %v", err) + } + + if err := config.SetYamlConfigInDir(beadsDir, "sync.remote", remote); err != nil { + t.Fatalf("record sync.remote: %v", err) + } + if got := config.GetStringFromDir(beadsDir, "sync.remote"); got != remote { + body, _ := os.ReadFile(path) //nolint:gosec,errcheck // diagnostic only + t.Fatalf("sync.remote does not read back after being written: got %q, want %q\nconfig.yaml:\n%s", + got, remote, body) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 808c6725c..ae48de830 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -700,6 +700,11 @@ func GetStringFromDir(beadsDir, key string) string { if err := yaml.Unmarshal(data, &root); err != nil { return "" } + // Match Viper's precedence: an exact top-level key wins over dotted-path + // descent when a file contains both spellings. + if val, ok := root[key]; ok { + return configValueString(val) + } parts := strings.SplitN(key, ".", 2) node := root for len(parts) == 2 { @@ -718,6 +723,10 @@ func GetStringFromDir(beadsDir, key string) string { if !ok { return "" } + return configValueString(val) +} + +func configValueString(val interface{}) string { switch s := val.(type) { case string: return s diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 3452449c5..2451584e7 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -6,6 +6,8 @@ import ( "runtime" "strings" "testing" + + "github.com/spf13/viper" ) // envSnapshot saves and clears BD_/BEADS_ environment variables. @@ -1688,6 +1690,34 @@ func TestGetStringFromDir(t *testing.T) { } }) + t.Run("literal flat dotted key", func(t *testing.T) { + dir := t.TempDir() + writeConfig(t, dir, "dolt.host: 127.0.0.1\n") + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Errorf("got %q, want %q", got, "127.0.0.1") + } + }) + + t.Run("literal flat dotted key wins over nested spelling", func(t *testing.T) { + dir := t.TempDir() + writeConfig(t, dir, "dolt.host: 127.0.0.1\ndolt:\n host: 10.0.0.1\n") + got := GetStringFromDir(dir, "dolt.host") + if got != "127.0.0.1" { + t.Errorf("got %q, want the literal-key value %q", got, "127.0.0.1") + } + + savedV := v + v = viper.New() + t.Cleanup(func() { v = savedV }) + v.SetConfigFile(filepath.Join(dir, "config.yaml")) + if err := v.ReadInConfig(); err != nil { + t.Fatalf("ReadInConfig: %v", err) + } + if viperGot := GetString("dolt.host"); viperGot != got { + t.Errorf("readers disagree: GetString = %q, GetStringFromDir = %q", viperGot, got) + } + }) + t.Run("non-existent file returns empty string", func(t *testing.T) { dir := t.TempDir() if got := GetStringFromDir(dir, "dolt.auto-start"); got != "" { diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index f86b0ef2c..a27def39b 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -247,23 +247,8 @@ func checkSecretGitTracked(configPath, key string) error { ) } -// keyAliases maps alternative key names to their canonical yaml form. -// This ensures consistency when users use different formats (dot vs hyphen). -var keyAliases = map[string]string{} - -// normalizeYamlKey converts a key to its canonical yaml format. -// Some keys have aliases (e.g., sync.branch -> sync-branch) to handle -// different input formats consistently. -func normalizeYamlKey(key string) string { - if canonical, ok := keyAliases[key]; ok { - return canonical - } - return key -} - // SetYamlConfig sets a configuration value in the project's config.yaml file. // It handles both adding new keys and updating existing (possibly commented) keys. -// Keys are normalized to their canonical yaml format (e.g., sync.branch -> sync-branch). func SetYamlConfig(key, value string) error { // Validate specific keys (GH#995) if err := validateYamlConfigValue(key, value); err != nil { @@ -524,8 +509,6 @@ func UnsetUserYamlConfig(key string) error { if err != nil { return err } - normalizedKey := normalizeYamlKey(key) - content, err := os.ReadFile(configPath) //nolint:gosec // configPath is a validated absolute user config path if err != nil { if os.IsNotExist(err) { @@ -534,7 +517,10 @@ func UnsetUserYamlConfig(key string) error { return fmt.Errorf("failed to read user config.yaml: %w", err) } - newContent := commentOutYamlKey(string(content), normalizedKey) + newContent, err := commentOutYamlKey(string(content), key) + if err != nil { + return err + } // Preserve the owner-private 0600 posture every other user-global writer // uses (SetUserYamlConfig, setYamlConfigAtPath, the metrics bootstrap); @@ -568,10 +554,6 @@ func SetUserYamlConfig(key, value string) error { } func setYamlConfigAtPath(configPath, key, value string) error { - - // Normalize key to canonical yaml format - normalizedKey := normalizeYamlKey(key) - // Read existing config content, err := os.ReadFile(configPath) //nolint:gosec // configPath is from findProjectConfigYaml if err != nil { @@ -579,7 +561,7 @@ func setYamlConfigAtPath(configPath, key, value string) error { } // Update or add the key - newContent, err := updateYamlKey(string(content), normalizedKey, value) + newContent, err := updateYamlKey(string(content), key, value) if err != nil { return err } @@ -594,13 +576,11 @@ func setYamlConfigAtPath(configPath, key, value string) error { // GetYamlConfig gets a configuration value from config.yaml. // Returns empty string if key is not found or is commented out. -// Keys are normalized to their canonical yaml format (e.g., sync.branch -> sync-branch). func GetYamlConfig(key string) string { if v == nil { return "" } - normalizedKey := normalizeYamlKey(key) - return v.GetString(normalizedKey) + return v.GetString(key) } // UnsetYamlConfig removes a configuration value from the project's config.yaml file. @@ -611,14 +591,15 @@ func UnsetYamlConfig(key string) error { return err } - normalizedKey := normalizeYamlKey(key) - content, err := os.ReadFile(configPath) //nolint:gosec // configPath is from findProjectConfigYaml if err != nil { return fmt.Errorf("failed to read config.yaml: %w", err) } - newContent := commentOutYamlKey(string(content), normalizedKey) + newContent, err := commentOutYamlKey(string(content), key) + if err != nil { + return err + } 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) @@ -706,8 +687,6 @@ func findProjectBeadsDir() string { // updateYamlKey updates a key in yaml content, handling commented-out keys. // If the key exists (commented or not), it updates it in place. // If the key doesn't exist, it appends it at the end. -// -//nolint:unparam // error return kept for future validation func updateYamlKey(content, key, value string) (string, error) { if strings.Contains(key, ".") { if updated, ok, err := updateNestedYamlKey(content, key, value); err != nil { @@ -768,20 +747,59 @@ func updateNestedYamlKey(content, key, value string) (string, bool, error) { return "", false, err } if len(root.Content) == 0 { - return "", false, nil + // An empty or comment-only document parses to no nodes at all: yaml.v3 + // keeps no trace of its text, not even the comments. So there is nothing + // to nest into AND nothing to marshal back — fabricating a mapping here + // would emit the new key and silently delete everything else in the + // file, and `bd init`'s default template is exactly this shape, comments + // and nothing else. Append the rendered key to the text instead, which + // leaves the document byte for byte intact. Falling through to the flat + // writer is not an option either: that is what produced a key literally + // named "dolt.host", which GetStringFromDir — splitting on the dot and + // looking for a nested mapping — can never read back. + appended, err := appendNestedYamlKey(content, parts, value) + if err != nil { + return "", false, err + } + return appended, true, nil } mapping := root.Content[0] if mapping.Kind != yaml.MappingNode { - return "", false, nil + return "", false, fmt.Errorf("cannot set %q: the top level of this config file is not a mapping", key) } - if findMappingChild(mapping, key) != -1 { - return "", false, nil + // Preserve the spelling already chosen by the file's writer. config.yaml is + // shared with integrations that intentionally use literal dotted top-level + // keys, so migrating that node would make it invisible to those readers. + // + // Rewrite the one line the key is on rather than marshaling the document + // back: this branch exists to leave a file other writers share alone, and a + // whole-document marshal reformats every unrelated section of it — dropping + // blank separators, re-indenting nested blocks from two spaces to four, + // collapsing comment alignment. config.yaml is git-tracked, so a one-key set + // would show up as a whole-file diff nobody asked for. + if idx := findMappingChild(mapping, key); idx != -1 { + keyNode, valueNode := mapping.Content[idx], mapping.Content[idx+1] + if updated, ok := replaceFlatKeyLine(content, keyNode, valueNode, value); ok { + return updated, true, nil + } + // The entry does not fit on its own line, so there is no single line to + // swap. Marshal it back, reformatting and all: a correct value in a + // reformatted file beats a value written somewhere no reader looks. + valueNode.Kind = yaml.ScalarNode + valueNode.Tag = "" + valueNode.Style = scalarStyleFor(value) + valueNode.Value = value + out, err := yaml.Marshal(&root) + if err != nil { + return "", false, err + } + return string(out), true, nil } - leaf, ok := findOrCreateNestedScalar(mapping, parts) - if !ok { - return "", false, nil + leaf, err := findOrCreateNestedScalar(mapping, parts) + if err != nil { + return "", false, err } leaf.Kind = yaml.ScalarNode @@ -796,12 +814,73 @@ func updateNestedYamlKey(content, key, value string) (string, bool, error) { return string(out), true, nil } -func findOrCreateNestedScalar(mapping *yaml.Node, parts []string) (*yaml.Node, bool) { +// replaceFlatKeyLine swaps the value on the single line a top-level key and its +// scalar value share, keeping the key exactly as the file spells it — quoting, +// indentation and all — and leaving every other line byte for byte alone. The +// rewritten line keeps its own trailing comment too, so the only thing that +// changes anywhere in the file is the value that was asked for. +// +// Reports false when the entry is not that shape. The line is only safe to +// replace when it is the whole entry: a value that continues onto later lines (a +// block scalar, a nested mapping, a quoted string wrapped across lines) would +// have its body left behind. Parsing the one line on its own settles that, since +// a top-level entry that ends on its line is a complete document. +func replaceFlatKeyLine(content string, keyNode, valueNode *yaml.Node, value string) (string, bool) { + lines := strings.Split(content, "\n") + i := keyNode.Line - 1 + if i < 0 || i >= len(lines) { + return "", false + } + + var probe map[string]string + if err := yaml.Unmarshal([]byte(lines[i]), &probe); err != nil { + return "", false + } + if len(probe) != 1 || probe[keyNode.Value] != valueNode.Value { + return "", false + } + head, _, found := strings.Cut(lines[i], ":") + if !found || strings.Trim(strings.TrimSpace(head), `"'`) != keyNode.Value { + return "", false + } + + tail := trailingCommentOn(lines[i], keyNode, valueNode) + lines[i] = head + ": " + formatYamlValue(value) + tail + return strings.Join(lines, "\n"), true +} + +// trailingCommentOn returns the entry's end-of-line comment together with the +// whitespace separating it from the value, exactly as line spells both, or "" +// when the line carries no comment. +// +// The line is rebuilt from the key and the new value, so an operator's note on +// the one line this branch rewrites would otherwise be the single thing a +// "rewrite only this line" edit silently dropped. Matching yaml.v3's already +// parsed comment text back from the right is what keeps a "#" inside a quoted +// value (`host: 'a # b' # note`) from being mistaken for the comment. yaml.v3 +// hangs the comment on the value node, except when the value is empty and there +// is no value node line to hang it on, so both are consulted. +func trailingCommentOn(line string, keyNode, valueNode *yaml.Node) string { + comment := valueNode.LineComment + if comment == "" { + comment = keyNode.LineComment + } + if comment == "" { + return "" + } + start := strings.LastIndex(line, comment) + if start < 0 { + return " " + comment + } + for start > 0 && (line[start-1] == ' ' || line[start-1] == '\t') { + start-- + } + return line[start:] +} + +func findOrCreateNestedScalar(mapping *yaml.Node, parts []string) (*yaml.Node, error) { current := mapping for i, part := range parts { - if current.Kind != yaml.MappingNode { - return nil, false - } idx := findMappingChild(current, part) isLeaf := i == len(parts)-1 if idx == -1 { @@ -814,21 +893,60 @@ func findOrCreateNestedScalar(mapping *yaml.Node, parts []string) (*yaml.Node, b } current.Content = append(current.Content, keyNode, valNode) if isLeaf { - return valNode, true + return valNode, nil } current = valNode continue } child := current.Content[idx+1] if isLeaf { - return child, true + return child, nil + } + if child.Tag == "!!null" { + // A section with nothing left under it — `sync:` and no more, which + // is exactly what an unset leaves behind once it has commented the + // last leaf out. It holds no value to lose, so treat it as the empty + // mapping it looks like. Refusing here sent the caller to the flat + // writer, so set -> unset -> set put the unreadable `sync.remote:` + // spelling back into the file this whole fix exists to keep out. + child.Kind = yaml.MappingNode + child.Tag = "!!map" + child.Value = "" + child.Style = 0 } if child.Kind != yaml.MappingNode { - return nil, false + return nil, fmt.Errorf("cannot set %q: %q already holds a value, so there is no section to nest under it", + strings.Join(parts, "."), strings.Join(parts[:i+1], ".")) } current = child } - return nil, false + // Unreachable: every path through the loop returns on the last part. + return nil, fmt.Errorf("cannot set %q: no key to write", strings.Join(parts, ".")) +} + +// appendNestedYamlKey renders just the key being written and appends it to the +// document text. Used when the document has no nodes to walk, where the text is +// the only copy of the file's contents that exists. +func appendNestedYamlKey(content string, parts []string, value string) (string, error) { + node := &yaml.Node{Kind: yaml.ScalarNode, Style: scalarStyleFor(value), Value: value} + for i := len(parts) - 1; i >= 0; i-- { + node = &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Content: []*yaml.Node{ + {Kind: yaml.ScalarNode, Tag: "!!str", Value: parts[i]}, + node, + }} + } + rendered, err := yaml.Marshal(node) + if err != nil { + return "", err + } + + // Same spacing the flat writer uses: a blank line between whatever was + // already in the file and the key being added. + existing := strings.TrimRight(content, "\n") + if existing == "" { + return string(rendered), nil + } + return existing + "\n\n" + string(rendered), nil } func findMappingChild(mapping *yaml.Node, name string) int { @@ -861,27 +979,234 @@ func scalarStyleFor(value string) yaml.Style { return 0 } -func commentOutYamlKey(content, key string) string { - keyPattern := regexp.MustCompile(`^(\s*)` + regexp.QuoteMeta(key) + `\s*:`) +func commentOutYamlKey(content, key string) (string, error) { + if err := unsupportedUnsetShape(content, key); err != nil { + return "", err + } + + // The flat spelling first — a key literally named "sync.remote", which + // older files carry and which this function has always handled. + flatPattern := regexp.MustCompile(`^(\s*)` + regexp.QuoteMeta(key) + `\s*:`) + // And the nested one, which is what the writer produces. Missing this half + // made an unset silently do nothing once the writer started nesting: the + // value stayed live, so bd kept a setting the operator had asked it to + // forget. Unset is the other direction of the same round-trip property as + // set and get, and all three have to agree on the shape. + walk := newNestedKeyWalk(key) var result []string + // blockIndent is the indentation of the key that opened a literal or folded + // block scalar, or -1 outside one. Everything indented past that key is the + // value the user typed, not structure: `notes: |` with an indented + // `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() - if keyPattern.MatchString(line) { - matches := keyPattern.FindStringSubmatch(line) - indent := "" - if len(matches) > 1 { - indent = matches[1] + + if blockIndent >= 0 { + if strings.TrimSpace(line) == "" || lineIndent(line) > blockIndent { + result = append(result, line) + continue + } + blockIndent = -1 + } + blockIndent = blockScalarIndent(line) + + if matches := flatPattern.FindStringSubmatch(line); matches != nil { + result = append(result, matches[1]+"# "+strings.TrimLeft(line, " \t")) + continue + } + + if name, indent, ok := yamlKeyOnLine(line); ok && walk.step(name, indent) { + result = append(result, strings.Repeat(" ", indent)+"# "+strings.TrimLeft(line, " \t")) + continue + } + result = append(result, line) + } + + return strings.Join(result, "\n"), nil +} + +// 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. +type nestedKeyWalk struct { + segments []string + // depth is how many segments have been matched, and indents holds the + // indentation each was matched at. + depth int + indents []int + // opaque is the indentation of the last key line that could not continue + // the match, or -1. Everything nested under such a key belongs to some + // other path, so the walk steps over that whole subtree instead of + // descending into it and matching a leaf name that repeats there. + opaque int +} + +func newNestedKeyWalk(key string) *nestedKeyWalk { + return &nestedKeyWalk{segments: strings.Split(key, "."), opaque: -1} +} + +// step advances the walk by one mapping line and reports whether that line is +// the key being looked for. +func (w *nestedKeyWalk) step(name string, indent int) bool { + if len(w.segments) < 2 { + // A single-segment key has no nesting to walk; the flat pattern owns it. + return false + } + if w.opaque >= 0 && indent > w.opaque { + return false + } + w.opaque = -1 + // Leaving a block: drop every segment matched at an indent at or deeper + // than this line's. + for w.depth > 0 && indent <= w.indents[w.depth-1] { + w.depth-- + w.indents = w.indents[:w.depth] + } + // Both halves of the anchor matter, and missing either one made an unset + // edit keys it does not own. Segment 0 is only itself at the TOP level: + // without that, a `sync:` section nested under an unrelated key seeded the + // walk, so unsetting `dolt.host` commented out `other.dolt.host` too. And + // every later segment has to be a DIRECT child of the one before it: + // without that, an interposed section was stepped over rather than ending + // the match, so unsetting `sync.remote` on + // + // sync: + // sub: + // remote: keep + // remote: target + // + // commented out `sync.sub.remote` and left the real `sync.remote` live, + // reporting success — the silent divergence this whole fix exists to end. + if w.depth < len(w.segments) && name == w.segments[w.depth] && (w.depth > 0 || indent == 0) { + if w.depth == len(w.segments)-1 { + w.depth, w.indents = 0, nil + return true + } + w.indents = append(w.indents, indent) + w.depth++ + return false + } + w.opaque = indent + return false +} + +// 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 +// reported success while the value stayed live, and the block scalar had its key +// line commented out while the body stayed behind as a value of its own. The set +// 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. +// +// 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 { + 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 + // to diagnose, and the line matcher has always been best-effort on it. + return nil + } + + // The flat spelling first, and by the same order the line matcher uses: it + // comments a literal `sync.remote:` line out without ever walking the nested + // path, so the body left behind orphans at the TOP level, where it stops the + // whole document from parsing rather than just its own section. + if top := root.Content[0]; top.Kind == yaml.MappingNode { + if idx := findMappingChild(top, key); idx != -1 { + if err := blockScalarUnsetRefusal(top.Content[idx+1], key); err != nil { + return err } - // Comment out the line, preserving indentation - result = append(result, indent+"# "+strings.TrimLeft(line, " \t")) - } else { - result = append(result, line) } } - return strings.Join(result, "\n") + node := root.Content[0] + parents := make([]*yaml.Node, 0, len(segments)) + for _, segment := range segments { + if node.Kind != yaml.MappingNode { + return nil + } + idx := findMappingChild(node, segment) + if idx == -1 { + return nil + } + parents = append(parents, node) + node = node.Content[idx+1] + } + + for i, parent := range parents { + if parent.Style&yaml.FlowStyle == 0 { + continue + } + where := "the top level of this config file" + if i > 0 { + where = fmt.Sprintf("%q", strings.Join(segments[:i], ".")) + } + return fmt.Errorf("cannot unset %q: %s is written in flow style ({...}), which bd cannot edit; remove the key by hand", key, where) + } + return blockScalarUnsetRefusal(node, key) +} + +// blockScalarUnsetRefusal refuses a key whose value is written as a block +// scalar, in whichever spelling the caller resolved it: commenting the key line +// out leaves the indented body behind, to be re-read as a value of whatever +// encloses it. +func blockScalarUnsetRefusal(valueNode *yaml.Node, key string) error { + if valueNode.Style&(yaml.LiteralStyle|yaml.FoldedStyle) == 0 { + return nil + } + return fmt.Errorf("cannot unset %q: its value is a block scalar (| or >), and commenting the key out would leave the body behind as a value of its own; remove the key by hand", key) +} + +// blockScalarIndent reports the indentation of a key whose value is a literal +// or folded block scalar (`notes: |`), or -1 when the line opens no block. +func blockScalarIndent(line string) int { + _, indent, ok := yamlKeyOnLine(line) + if !ok { + return -1 + } + _, rest, _ := strings.Cut(strings.TrimLeft(line, " \t"), ":") + rest = strings.TrimSpace(rest) + // "|" and ">" are only ever block indicators in value position; a plain + // scalar cannot start with either. + if rest == "" || (rest[0] != '|' && rest[0] != '>') { + return -1 + } + return indent +} + +func lineIndent(line string) int { + return len(line) - len(strings.TrimLeft(line, " \t")) +} + +// yamlKeyOnLine reports the key a mapping line declares and its indentation. +// Comments, list items and blank lines declare nothing. +func yamlKeyOnLine(line string) (name string, indent int, ok bool) { + trimmed := strings.TrimLeft(line, " \t") + if trimmed == "" || strings.HasPrefix(trimmed, "#") || strings.HasPrefix(trimmed, "- ") { + return "", 0, false + } + key, _, found := strings.Cut(trimmed, ":") + if !found { + return "", 0, false + } + key = strings.TrimSpace(key) + if key == "" || strings.ContainsAny(key, " \t") { + return "", 0, false + } + return key, lineIndent(line), true } // formatYamlValue formats a value appropriately for YAML. diff --git a/internal/config/yaml_config_test.go b/internal/config/yaml_config_test.go index d6fd9522c..8603e1604 100644 --- a/internal/config/yaml_config_test.go +++ b/internal/config/yaml_config_test.go @@ -273,26 +273,6 @@ func TestFormatYamlValue(t *testing.T) { } } -func TestNormalizeYamlKey(t *testing.T) { - tests := []struct { - input string - expected string - }{ - {"no-db", "no-db"}, // no alias, unchanged - {"json", "json"}, // no alias, unchanged - {"routing.mode", "routing.mode"}, // no alias for this one - } - - for _, tt := range tests { - t.Run(tt.input, func(t *testing.T) { - got := normalizeYamlKey(tt.input) - if got != tt.expected { - t.Errorf("normalizeYamlKey(%q) = %q, want %q", tt.input, got, tt.expected) - } - }) - } -} - func TestSetYamlConfig(t *testing.T) { oldBeadsDir := os.Getenv("BEADS_DIR") if err := os.Unsetenv("BEADS_DIR"); err != nil { @@ -1055,7 +1035,10 @@ func TestCommentOutYamlKey(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := commentOutYamlKey(tt.content, tt.key) + got, err := commentOutYamlKey(tt.content, tt.key) + if err != nil { + t.Fatalf("commentOutYamlKey() error = %v", err) + } if got != tt.expected { t.Errorf("commentOutYamlKey() =\n%q\nwant:\n%q", got, tt.expected) } diff --git a/internal/config/yaml_nested_roundtrip_test.go b/internal/config/yaml_nested_roundtrip_test.go new file mode 100644 index 000000000..ab0d0ef58 --- /dev/null +++ b/internal/config/yaml_nested_roundtrip_test.go @@ -0,0 +1,717 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// A dotted key must round-trip: whatever SetYamlConfigInDir writes, +// GetStringFromDir must read back. That is the whole contract between the two, +// and it is not a property either function can hold alone. +// +// It was broken in two directions, and both were reachable from ordinary files: +// +// - An empty or comment-only config.yaml has no mapping node, so the nested +// writer declined and the caller appended a literal `dolt.host: ...` line — +// a key whose NAME contains a dot. GetStringFromDir splits on the dot and +// looks for a nested mapping, so it never finds it. +// - A file that already carried such a flat key had it updated in place, but +// the direct reader did not understand that spelling. +// +// The observable consequence was a caller writing a value and immediately being +// unable to read it back. Both consumers found it the hard way (bd-zj95). +func TestDottedKeysRoundTripThroughEveryConfigShape(t *testing.T) { + cases := []struct { + name string + seed string + // present is true when the file should exist before the write. + present bool + }{ + {name: "absent file", present: false}, + {name: "empty file", seed: "", present: true}, + {name: "comment only", seed: "# a workspace config\n", present: true}, + {name: "flat dotted keys", seed: "dolt.host: 10.0.0.1\ndolt.port: 3307\n", present: true}, + {name: "flat commented key", seed: "# dolt.host: 10.0.0.1\n", present: true}, + {name: "existing dolt section", seed: "dolt:\n host: 10.0.0.1\n", present: true}, + {name: "unrelated nested section", seed: "sync:\n branch: beads-sync\n", present: true}, + {name: "unrelated flat key", seed: "node_id: somewhere\n", present: true}, + {name: "mixed flat and nested", seed: "dolt.host: 10.0.0.1\ndolt:\n port: 3307\n", present: true}, + } + + writes := map[string]string{ + "dolt.host": "127.0.0.1", + "dolt.port": "45678", + "dolt.auto-start": "true", + "dolt.mode": "server", + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if tc.present { + if err := os.WriteFile(path, []byte(tc.seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + } else { + // SetYamlConfigInDir refuses a workspace with no config.yaml at + // all; that refusal is deliberate and not what this test is + // about, so give it the empty file it asks for. + if err := os.WriteFile(path, nil, 0o600); err != nil { + t.Fatalf("create: %v", err) + } + } + + for key, value := range writes { + if err := SetYamlConfigInDir(dir, key, value); err != nil { + t.Fatalf("SetYamlConfigInDir(%q): %v", key, err) + } + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + for key, want := range writes { + if got := GetStringFromDir(dir, key); got != want { + t.Errorf("GetStringFromDir(%q) = %q, want %q\nfile:\n%s", key, got, want, body) + } + } + // Do not introduce a new flat spelling. An existing flat spelling is + // preserved because another config writer may own that representation, + // and GetStringFromDir now reads it consistently with Viper. + for _, line := range strings.Split(string(body), "\n") { + if line != strings.TrimSpace(line) || strings.HasPrefix(line, "#") { + continue // indented: inside a mapping. commented: not a key. + } + name, _, isKeyValue := strings.Cut(line, ":") + name = strings.TrimSpace(name) + if isKeyValue && writes[name] != "" && !hasLiveFlatKey(tc.seed, name) { + t.Errorf("the write introduced a live flat %q:\n%s", name, body) + } + } + // Comments the file arrived with are the operator's, and a write that + // drops them is a committed diff nobody asked for. `bd init` writes a + // template that is nothing BUT comments, so this is the common case, + // not an exotic one. + for _, line := range strings.Split(tc.seed, "\n") { + line = strings.TrimSpace(line) + if !strings.HasPrefix(line, "#") { + continue + } + if !strings.Contains(string(body), line) { + t.Errorf("the write dropped the comment %q:\n%s", line, body) + } + } + }) + } +} + +func hasLiveFlatKey(content, key string) bool { + prefix := key + ":" + for _, line := range strings.Split(content, "\n") { + if line == strings.TrimSpace(line) && strings.HasPrefix(line, prefix) { + return true + } + } + return false +} + +// Keys the caller does not own are left exactly as written. Rewriting the whole +// file would be bd deciding how someone else's config should look. +func TestDottedWriteLeavesOtherKeysAlone(t *testing.T) { + dir := t.TempDir() + seed := "# keep this comment\nsync.branch: keep-me\ndolt.host: 10.0.0.1\nnode_id: mini\n" + if err := os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + if err := SetYamlConfigInDir(dir, "dolt.host", "127.0.0.1"); err != nil { + t.Fatalf("set: %v", err) + } + + body, err := os.ReadFile(filepath.Join(dir, "config.yaml")) + if err != nil { + t.Fatalf("read: %v", err) + } + text := string(body) + if !strings.Contains(text, "sync.branch: keep-me") { + t.Errorf("a flat key the write does not own was rewritten:\n%s", text) + } + if !strings.Contains(text, "node_id: mini") { + t.Errorf("an unrelated key was lost:\n%s", text) + } + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Errorf("dolt.host reads back as %q\n%s", got, text) + } +} + +func TestDottedWritePreservesExistingFlatSpelling(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "# managed by another writer\ndolt.host: 10.0.0.1\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + if err := SetYamlConfigInDir(dir, "dolt.host", "127.0.0.1"); err != nil { + t.Fatalf("set: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + text := string(body) + if !strings.Contains(text, "dolt.host: 127.0.0.1") { + t.Fatalf("existing flat spelling was not updated in place:\n%s", text) + } + if strings.Contains(text, "dolt:\n") { + t.Fatalf("writer replaced the existing flat spelling with a nested mapping:\n%s", text) + } + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Fatalf("GetStringFromDir(dolt.host) = %q, want %q", got, "127.0.0.1") + } +} + +// Updating an existing flat key must change that key's line and nothing else. +// The branch exists to leave a file other writers share alone, so rewriting the +// rest of it to bd's taste defeats the point: config.yaml is git-tracked, and a +// one-key set that re-indents unrelated sections and drops blank separators is a +// whole-file diff nobody asked for (bd-zj95 review, Finding 2). +// +// The key under test carries the trailing note, not just a bystander: an +// assertion that only watches other lines cannot see the rewritten line lose its +// own annotation, which is the one way "rewrite only this line" still destroys +// content (bd-zj95 review iteration 2, Finding 3). +func TestDottedSetOnAnExistingFlatKeyRewritesOnlyThatLine(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "# top comment\n\nnode_id: mini # trailing note\n\n# section\ndolt.host: 10.0.0.1 # staging box, do not change\n\nsync:\n branch: main\n extras:\n - a\n - b\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + if err := SetYamlConfigInDir(dir, "dolt.host", "127.0.0.1"); err != nil { + t.Fatalf("set: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + want := strings.Replace(seed, "dolt.host: 10.0.0.1", "dolt.host: 127.0.0.1", 1) + if string(body) != want { + t.Errorf("a one-key set rewrote more than the key's line:\ngot:\n%s\nwant:\n%s", body, want) + } + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Errorf("dolt.host reads back as %q\n%s", got, body) + } +} + +// The comment the line rewrite has to carry over is found by matching yaml.v3's +// parsed comment text back from the right, so a "#" the value itself contains is +// never read as the start of one. An empty value is the shape where yaml.v3 has +// no value node line to hang the comment on and puts it on the key instead. +func TestDottedSetKeepsTheTrailingCommentOnTheKeyItRewrites(t *testing.T) { + cases := []struct { + name string + seed string + want string + }{ + { + name: "note after a plain value", + seed: "dolt.host: 10.0.0.1 # staging box\nnode_id: mini\n", + want: "dolt.host: 127.0.0.1 # staging box\nnode_id: mini\n", + }, + { + name: "note after a value containing a hash", + seed: "dolt.host: 'a # b' # the real note\nnode_id: mini\n", + want: "dolt.host: 127.0.0.1 # the real note\nnode_id: mini\n", + }, + { + name: "note on a key with no value", + seed: "dolt.host:\t# not set yet\nnode_id: mini\n", + want: "dolt.host: 127.0.0.1\t# not set yet\nnode_id: mini\n", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(path, []byte(tc.seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + if err := SetYamlConfigInDir(dir, "dolt.host", "127.0.0.1"); err != nil { + t.Fatalf("set: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if string(body) != tc.want { + t.Errorf("the rewritten line did not keep its trailing comment:\ngot:\n%s\nwant:\n%s", body, tc.want) + } + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Errorf("dolt.host reads back as %q\n%s", got, body) + } + if got := GetStringFromDir(dir, "node_id"); got != "mini" { + t.Errorf("node_id reads back as %q\n%s", got, body) + } + }) + } +} + +// Rewriting the key's line means rewriting the line the key is ACTUALLY on, +// whatever it looks like. A quoted flat key is still that key — bd never writes +// one, but a shared config.yaml can arrive with one, and a writer that matches +// the unquoted spelling by text finds nothing and appends a second flat key +// beside the first, leaving the value it just "set" unreadable. +func TestDottedSetFindsAQuotedFlatKey(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "\"dolt.host\": 10.0.0.1\nnode_id: mini\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + if err := SetYamlConfigInDir(dir, "dolt.host", "127.0.0.1"); err != nil { + t.Fatalf("set: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Errorf("dolt.host reads back as %q\n%s", got, body) + } + if want := strings.Replace(seed, "10.0.0.1", "127.0.0.1", 1); string(body) != want { + t.Errorf("the write did not update the quoted key in place:\ngot:\n%s\nwant:\n%s", body, want) + } +} + +// A flat key whose value does not fit on the key's own line has no single line +// to swap. Correctness wins over formatting there: the value must still read +// back, even though the document gets marshaled and reformatted to do it. +func TestDottedSetOnAMultiLineFlatKeyStillWrites(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "dolt.host: |\n 10.0.0.1\nnode_id: mini\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + if err := SetYamlConfigInDir(dir, "dolt.host", "127.0.0.1"); err != nil { + t.Fatalf("set: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if got := GetStringFromDir(dir, "dolt.host"); got != "127.0.0.1" { + t.Errorf("dolt.host reads back as %q\n%s", got, body) + } + if got := GetStringFromDir(dir, "node_id"); got != "mini" { + t.Errorf("node_id reads back as %q\n%s", got, body) + } +} + +// A single-segment key has no nesting to do and must keep working exactly as it +// did: this is the shape most of bd's config keys have. +func TestUndottedKeysAreUnaffected(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "config.yaml"), []byte("# seed\n"), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + if err := SetYamlConfigInDir(dir, "node_id", "mini"); err != nil { + t.Fatalf("set: %v", err) + } + if got := GetStringFromDir(dir, "node_id"); got != "mini" { + body, _ := os.ReadFile(filepath.Join(dir, "config.yaml")) //nolint:errcheck // diagnostic + t.Fatalf("node_id reads back as %q\n%s", got, body) + } +} + +// Writing a dotted key nested and then being unable to unset it is the same +// round-trip break in the other direction: UnsetYamlConfig comments out the +// line matching the key, and its pattern only ever matched a FLAT +// `sync.remote:` line. Once the writer nests, an unset silently does nothing +// and the value stays live — which for sync.remote means bd keeps a remote the +// operator asked it to forget. +// The over-match cases matter as much as the removal cases, and the first +// version of this table could not express them: every case put the leaf exactly +// one level under a top-level parent, so it was blind to a walk that matched a +// leaf name at the wrong depth or under the wrong parent. Each survivor below is +// a key the unset was never asked about, and each one was destroyed — or left +// live while a bystander died — by the descent this test now pins (bd-zj95 +// review, Finding 1). +func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) { + cases := []struct { + name string + seed string + key string + // survivors are keys the unset must not touch, and the values they must + // still read back afterwards. + survivors map[string]string + }{ + {name: "nested", seed: "sync:\n remote: \"file:///origin.git\"\n", key: "sync.remote"}, + { + name: "nested among siblings", + seed: "sync:\n branch: beads-sync\n remote: \"file:///origin.git\"\n", + key: "sync.remote", + survivors: map[string]string{"sync.branch": "beads-sync"}, + }, + {name: "legacy flat", seed: "sync.remote: \"file:///origin.git\"\n", key: "sync.remote"}, + { + name: "nested with other sections", + seed: "dolt:\n port: 3307\nsync:\n remote: \"file:///origin.git\"\n", + key: "sync.remote", + survivors: map[string]string{"dolt.port": "3307"}, + }, + { + // The target is present AND a deeper key repeats its name. The walk + // used to take the deeper one and report success, so the operator + // lost sync.sub.remote and kept the sync.remote they asked to forget. + name: "deeper key repeats the leaf name", + seed: "sync:\n sub:\n remote: keep\n remote: target\n", + key: "sync.remote", + survivors: map[string]string{"sync.sub.remote": "keep"}, + }, + { + // Same shape, two spaces of indent and a scalar sibling after the + // interposed section: the target does not exist at all here, so the + // only correct outcome is to change nothing. + name: "leaf repeats under a section the key does not own", + seed: "sync:\n nested:\n remote: should-survive\n other: x\n", + key: "sync.remote", + survivors: map[string]string{"sync.nested.remote": "should-survive", "sync.other": "x"}, + }, + { + // The key's own section name repeated under a foreign top-level key. + // Segment 0 used to match at any indent, so this was read as the + // sync section and a key at a completely different path was cut. + name: "section name repeats under a foreign parent", + seed: "other:\n sync:\n remote: keep\n", + key: "sync.remote", + survivors: map[string]string{"other.sync.remote": "keep"}, + }, + { + // Both spellings present: the real dolt.host at top level and a + // namesake under `other`. The unset used to comment out both. + name: "target and namesake in the same file", + seed: "other:\n dolt:\n host: should-survive\ndolt:\n host: kill-me\n", + key: "dolt.host", + survivors: map[string]string{"other.dolt.host": "should-survive"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(path, []byte(tc.seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + // Whether the key was set to begin with decides what "commented out + // as documentation" can mean below: an unset of a key that was never + // there is a no-op, not a removal. + wasSet := GetStringFromDir(dir, tc.key) != "" + + t.Setenv("BEADS_DIR", dir) + if err := UnsetYamlConfig(tc.key); err != nil { + t.Fatalf("UnsetYamlConfig: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if got := GetStringFromDir(dir, tc.key); got != "" { + t.Errorf("%s still reads back as %q after unset:\n%s", tc.key, got, body) + } + leaf := tc.key[strings.LastIndex(tc.key, ".")+1:] + // The key is preserved as documentation, which is this function's + // stated contract, so it must still be visible — commented. + if wasSet && !strings.Contains(string(body), leaf+":") { + t.Errorf("unset removed the key instead of commenting it out:\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 { + t.Errorf("unset touched a key it was not asked about: %s = %q, want %q\n%s", + key, got, want, body) + } + } + }) + } +} + +// bd cannot comment out a key it cannot find by line, and it used to pretend +// otherwise in both directions: a flow-style mapping came back unchanged with a +// success exit, leaving live the value the operator asked bd to forget — the +// PR's own headline symptom — and a block scalar had its key line commented +// while the body stayed behind, re-parsing as a plain multi-line value of the +// PARENT. Say so instead, and leave the file alone (bd-zj95 review, Finding 3). +func TestUnsetRefusesShapesItCannotEdit(t *testing.T) { + cases := []struct { + name string + seed string + key string + // want is a substring the error must carry beyond the key name. + want string + }{ + {name: "flow style section", seed: "sync: {remote: \"file:///origin.git\"}\n", key: "sync.remote", want: "flow style"}, + {name: "flow style nested deeper", seed: "dolt:\n limits: {host: mini}\n", key: "dolt.limits.host", want: "flow style"}, + {name: "literal block scalar", seed: "sync:\n remote: |\n line1\n line2\n", key: "sync.remote", want: "block scalar"}, + {name: "folded block scalar", seed: "sync:\n remote: >\n line1\n", key: "sync.remote", want: "block scalar"}, + // The flat spelling reaches the same body-left-behind outcome by the + // other half of the line matcher, and the orphan lands at the top level, + // 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"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(path, []byte(tc.seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + t.Setenv("BEADS_DIR", dir) + 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) + } + if !strings.Contains(err.Error(), tc.key) || !strings.Contains(err.Error(), tc.want) { + t.Errorf("the error names neither the key nor the shape: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if string(body) != tc.seed { + t.Errorf("a refused unset still changed the file:\n%s", body) + } + }) + } +} + +// A shape bd cannot edit is only worth refusing when the key is actually there. +// Unsetting a key that was never set has always been a successful no-op, and a +// flow-style mapping elsewhere in the file is not the operator's problem. +func TestUnsetOfAnAbsentKeyStillSucceeds(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "sync: {branch: beads-sync}\ndolt:\n port: 3307\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + t.Setenv("BEADS_DIR", dir) + if err := UnsetYamlConfig("sync.remote"); err != nil { + t.Fatalf("UnsetYamlConfig on an unset key: %v", err) + } + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if got := GetStringFromDir(dir, "sync.branch"); got != "beads-sync" { + t.Errorf("the no-op unset changed sync.branch to %q:\n%s", got, body) + } +} + +// set, unset, set is one command sequence, not three independent ones, and the +// round trip has to survive all of it. An unset comments the leaf out and leaves +// the section behind holding nothing — `sync:` and no more. The writer used to +// refuse that section, because a null is not a mapping, and fell through to the +// flat writer, which put a literal `sync.remote:` key back into the file: the +// unreadable shape, re-created one command after being fixed. bd said "Set +// sync.remote = ..." while GetStringFromDir saw nothing. +func TestSettingADottedKeyAgainAfterUnsetStaysNested(t *testing.T) { + cases := []struct { + name string + seed string + }{ + // The section is left holding nothing at all once its only leaf is + // commented out. This is the shape that broke. + {name: "section empties out", seed: "node_id: mini\n"}, + // A sibling keeps the section a mapping, so this shape always worked. + // Pin it anyway: the fix must not trade one for the other. + {name: "sibling keeps the section", seed: "sync:\n branch: beads-sync\n"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(path, []byte(tc.seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + t.Setenv("BEADS_DIR", dir) + + if err := SetYamlConfigInDir(dir, "sync.remote", "file:///a.git"); err != nil { + t.Fatalf("first set: %v", err) + } + if err := UnsetYamlConfig("sync.remote"); err != nil { + t.Fatalf("unset: %v", err) + } + if got := GetStringFromDir(dir, "sync.remote"); got != "" { + body, _ := os.ReadFile(path) //nolint:errcheck // diagnostic + t.Fatalf("unset left sync.remote at %q:\n%s", got, body) + } + if err := SetYamlConfigInDir(dir, "sync.remote", "file:///b.git"); err != nil { + t.Fatalf("second set: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if got := GetStringFromDir(dir, "sync.remote"); got != "file:///b.git" { + t.Errorf("sync.remote reads back as %q after set/unset/set:\n%s", got, body) + } + for _, line := range strings.Split(string(body), "\n") { + if strings.HasPrefix(strings.TrimSpace(line), "sync.remote:") { + t.Errorf("the unreadable flat spelling came back:\n%s", body) + } + } + if tc.name == "sibling keeps the section" { + if got := GetStringFromDir(dir, "sync.branch"); got != "beads-sync" { + t.Errorf("the sibling was lost: sync.branch = %q\n%s", got, body) + } + } + }) + } +} + +// A config.yaml that is nothing but comments is not an edge case: it is what +// `bd init` writes, so it is the shape every fresh workspace's FIRST dotted +// write lands on — and config.yaml is git-tracked, so losing it is a committed +// diff the operator never asked for. +// +// yaml.v3 parses such a document to no nodes at all and keeps none of its text, +// so a writer that marshals a node tree has nothing to write back but the key it +// just added. Asserting the value reads back is not enough to catch that; the +// file is only correct if everything that was in it is still in it. +func TestFirstDottedWriteKeepsACommentOnlyFile(t *testing.T) { + seed := `# Beads Configuration File +# This file configures default behavior for all bd commands in this repository + +# Issue prefix for this repository (used by bd init) +# issue-prefix: "" + +# Use no-db mode: JSONL-only, no Dolt database +# no-db: false + +# Default actor for audit trails (overridden by BEADS_ACTOR or --actor) +# actor: "" +` + + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + // Two writes: the first lands on the comment-only file, the second on + // whatever the first produced. Both have to keep the comments. + if err := SetYamlConfigInDir(dir, "sync.remote", "file:///origin.git"); err != nil { + t.Fatalf("set sync.remote: %v", err) + } + if err := SetYamlConfigInDir(dir, "dolt.port", "3307"); err != nil { + t.Fatalf("set dolt.port: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + text := string(body) + for _, line := range strings.Split(strings.TrimRight(seed, "\n"), "\n") { + if line == "" { + continue + } + if !strings.Contains(text, line) { + t.Errorf("the write dropped a line that was already in the file: %q\ngot:\n%s", line, text) + } + } + if got := GetStringFromDir(dir, "sync.remote"); got != "file:///origin.git" { + t.Errorf("sync.remote reads back as %q\n%s", got, text) + } + if got := GetStringFromDir(dir, "dolt.port"); got != "3307" { + t.Errorf("dolt.port reads back as %q\n%s", got, text) + } +} + +// When a key's own parent already holds a value there is no section to nest +// under it, and bd has nothing correct to write. It used to write the flat +// `sync.remote:` spelling and report success, so the operator was told the +// value was set and every reader that splits on the dot saw nothing. Say so +// instead, and leave the file alone. +func TestSettingUnderAScalarParentIsAnError(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "sync: enabled\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + err := SetYamlConfigInDir(dir, "sync.remote", "file:///origin.git") + if err == nil { + body, _ := os.ReadFile(path) //nolint:errcheck // diagnostic + t.Fatalf("setting sync.remote under a scalar sync reported success\n%s", body) + } + if !strings.Contains(err.Error(), "sync") { + t.Errorf("the error does not name the key in the way: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + if string(body) != seed { + t.Errorf("a refused write still changed the file:\n%s", body) + } +} + +// Unset matches by line, so it has to know which lines are YAML and which are +// somebody's prose. A literal block scalar's body is data: text indented under +// `notes: |` only looks like a mapping. Commenting inside it edits the value the +// operator wrote. +func TestUnsetLeavesBlockScalarTextAlone(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "config.yaml") + seed := "notes: |\n sync:\n remote: keep-this-text\nsync:\n remote: \"file:///origin.git\"\n" + if err := os.WriteFile(path, []byte(seed), 0o600); err != nil { + t.Fatalf("seed: %v", err) + } + + t.Setenv("BEADS_DIR", dir) + if err := UnsetYamlConfig("sync.remote"); err != nil { + t.Fatalf("UnsetYamlConfig: %v", err) + } + + body, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read: %v", err) + } + text := string(body) + if !strings.Contains(text, " remote: keep-this-text") { + t.Errorf("unset rewrote text inside the notes block:\n%s", text) + } + if got := GetStringFromDir(dir, "sync.remote"); got != "" { + t.Errorf("sync.remote still reads back as %q after unset:\n%s", got, text) + } + if got := GetStringFromDir(dir, "notes"); got != "sync:\n remote: keep-this-text\n" { + t.Errorf("the notes value changed: %q\n%s", got, text) + } +}