diff --git a/CHANGELOG.md b/CHANGELOG.md index e7d01278f..51eb82066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `bd dolt pull`, which is what opting out means). Pulling before the first open after upgrading avoids the state entirely. +- **A `--format` typed on `bd list` now beats a `json: true` config default** + ([#6278](https://github.com/gastownhall/beads/issues/6278)). With `json: true` + in `config.yaml` (or the environment), `bd list --format digraph`, `dot` or a + template printed the JSON listing instead, because `bd list`'s own `--format` + flag shadows the root one the config-precedence check looks at. A typed + `--format` now wins over the config default; an explicit `--json` still wins + over `--format`. + ### Added - **`bd backup` works on a proxied-server workspace bd runs the Dolt server diff --git a/cmd/bd/list_format_json_test.go b/cmd/bd/list_format_json_test.go new file mode 100644 index 000000000..03e186661 --- /dev/null +++ b/cmd/bd/list_format_json_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "testing" + + "github.com/spf13/cobra" +) + +// TestListTypedFormatBeatsAConfigJSONDefault pins GH#6278. The root pre-run +// promotes a config-file (or env) `json: true` into jsonOutput unless the +// ROOT --format changed, but `bd list` registers its own --format, which +// shadows the root one. So `bd list --format digraph` under `json: true` +// printed the JSON listing instead of the graph. A --format the caller typed +// must win over that default; an explicit --json still wins over --format. +func TestListTypedFormatBeatsAConfigJSONDefault(t *testing.T) { + t.Run("typed format clears a config json default", func(t *testing.T) { + // pinJSONOutput(true) stands in for the pre-run having read json: true. + pinJSONOutput(t, true) + in, err := gatherListInput(newListFlagsCommand(t, "--format", "digraph")) + if err != nil { + t.Fatalf("gatherListInput: %v", err) + } + if in.jsonOutput || jsonOutput { + t.Errorf("jsonOutput = %v (in.jsonOutput = %v), want false: the typed --format must beat the config default", jsonOutput, in.jsonOutput) + } + if in.formatStr != "digraph" { + t.Errorf("formatStr = %q, want %q", in.formatStr, "digraph") + } + }) + + t.Run("explicit --json still beats --format", func(t *testing.T) { + pinJSONOutput(t, true) + cmd := newListFlagsCommand(t, "--format", "digraph") + root := &cobra.Command{Use: "bd"} + root.PersistentFlags().Bool("json", false, "") + root.AddCommand(cmd) + if err := root.PersistentFlags().Set("json", "true"); err != nil { + t.Fatalf("set --json: %v", err) + } + in, err := gatherListInput(cmd) + if err != nil { + t.Fatalf("gatherListInput: %v", err) + } + if !in.jsonOutput || !jsonOutput { + t.Errorf("jsonOutput = %v, want true: an explicit --json outranks --format", jsonOutput) + } + }) + + t.Run("no typed format leaves the config default alone", func(t *testing.T) { + pinJSONOutput(t, true) + in, err := gatherListInput(newListFlagsCommand(t)) + if err != nil { + t.Fatalf("gatherListInput: %v", err) + } + if !in.jsonOutput { + t.Error("in.jsonOutput = false, want true: without --format the config json default applies") + } + }) + + t.Run("--format json still turns json on", func(t *testing.T) { + pinJSONOutput(t, false) + in, err := gatherListInput(newListFlagsCommand(t, "--format", "json")) + if err != nil { + t.Fatalf("gatherListInput: %v", err) + } + if !in.jsonOutput || in.formatStr != "" { + t.Errorf("jsonOutput = %v, formatStr = %q, want true and empty", in.jsonOutput, in.formatStr) + } + }) +} diff --git a/cmd/bd/list_input.go b/cmd/bd/list_input.go index 5dd69799c..b637f47ff 100644 --- a/cmd/bd/list_input.go +++ b/cmd/bd/list_input.go @@ -65,6 +65,16 @@ func gatherListInput(cmd *cobra.Command) (listInput, error) { if strings.EqualFold(in.formatStr, "json") { jsonOutput = true in.formatStr = "" + } else if in.formatStr != "" && cmd.Flags().Changed("format") && !commandJSONFlagChanged(cmd) { + // A --format the caller typed outranks a json default from config or + // the environment (GH#6278). The root pre-run promotes `json: true` + // into jsonOutput unless the ROOT --format changed, but this command's + // own --format shadows the root one, so that check never sees it and + // the JSON branch would replace the requested graph or template. An + // explicit --json still wins (the !commandJSONFlagChanged conjunct + // above is what preserves it); note no flag's help text states that + // precedence, so this comment is its only record. + jsonOutput = false } in.jsonOutput = jsonOutput diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 53d341d40..f5cc0b9d4 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -1100,7 +1100,13 @@ var rootCmd = &cobra.Command{ jsonOutput = true } } - // If flag wasn't explicitly set, use viper value + // If flag wasn't explicitly set, use viper value. + // + // SHADOWING HAZARD (GH#6278): this reads the ROOT persistent --format + // only, so a subcommand that registers its own local --format shadows + // it here and still gets `json: true` promoted over the format it was + // asked for. `bd list` compensates in gatherListInput; any new local + // --format registration needs the same treatment or this fires again. if !commandJSONFlagChanged(cmd) && !cmd.Root().PersistentFlags().Changed("format") { jsonOutput = config.GetBool("json") } else {