diff --git a/cmd/bd/auto_import_upgrade_unit_test.go b/cmd/bd/auto_import_upgrade_unit_test.go index dbbd87cb9..1756b485b 100644 --- a/cmd/bd/auto_import_upgrade_unit_test.go +++ b/cmd/bd/auto_import_upgrade_unit_test.go @@ -162,6 +162,7 @@ func TestMaybeAutoImportJSONL_FallbackImporter_SkipsWhenStatisticsReportNonEmpty } func TestShouldRunAutoImportJSONL(t *testing.T) { + initConfigForTest(t) store := &fakeFallbackStore{} writeCmd := &cobra.Command{Use: "update"} @@ -193,6 +194,79 @@ func TestShouldRunAutoImportJSONL(t *testing.T) { } } +// TestShouldRunAutoImportJSONL_RespectsImportAutoFalse is the regression test +// for GH#4304: bd update (and any other write command) auto-imported +// issues.jsonl into an empty database even when import.auto was explicitly +// set to false (via config.yaml or BD_IMPORT_AUTO=false). shouldRunAutoImportJSONL +// previously never consulted the import.auto config key at all — only the +// separate git-hook sync path (importJSONLForSync) checked it. This test fails +// on the buggy code (want=true) and passes once the config check is restored. +func TestShouldRunAutoImportJSONL_RespectsImportAutoFalse(t *testing.T) { + initConfigForTest(t) + store := &fakeFallbackStore{} + writeCmd := &cobra.Command{Use: "update"} + importCmd := &cobra.Command{Use: "import"} + + config.Set("import.auto", false) + if got := shouldRunAutoImportJSONL(writeCmd, store, false, false, false); got { + t.Fatalf("regression GH#4304: shouldRunAutoImportJSONL() = %v with import.auto=false, want false", got) + } + // The explicit "bd import" command must remain unaffected either way — + // confirm it's still gated off (for a different reason) so the config + // check above isn't the only thing keeping it from double-running. + if got := shouldRunAutoImportJSONL(importCmd, store, false, false, false); got { + t.Fatalf("shouldRunAutoImportJSONL() = %v for the import command with import.auto=false, want false", got) + } + + config.Set("import.auto", true) + if got := shouldRunAutoImportJSONL(writeCmd, store, false, false, false); !got { + t.Fatalf("shouldRunAutoImportJSONL() = %v with import.auto=true, want true (negative control)", got) + } +} + +// TestIsDisablingImportAutoViaConfigCommand is the regression test for the +// P2 finding on gastownhall/beads#4595 (Codex cross-vendor review, +// 2026-07-07): shouldRunAutoImportJSONL runs in PersistentPreRun before +// "bd config set import.auto false" writes the new value, so the command +// meant to disable auto-import triggered it on its own invocation whenever a +// stale .beads/issues.jsonl sat next to an empty database (GH#4304). This +// exemption must fire for "config set import.auto false"/"0" and for +// "config set-many ... import.auto=false", but not for unrelated config +// keys, truthy values, or commands outside the config subtree. +func TestIsDisablingImportAutoViaConfigCommand(t *testing.T) { + configCmd := &cobra.Command{Use: "config"} + setCmd := &cobra.Command{Use: "set"} + setManyCmd := &cobra.Command{Use: "set-many"} + configCmd.AddCommand(setCmd, setManyCmd) + writeCmd := &cobra.Command{Use: "update"} + + tests := []struct { + name string + cmd *cobra.Command + args []string + want bool + }{ + {name: "config set import.auto false", cmd: setCmd, args: []string{"import.auto", "false"}, want: true}, + {name: "config set import.auto 0", cmd: setCmd, args: []string{"import.auto", "0"}, want: true}, + {name: "config set import.auto true", cmd: setCmd, args: []string{"import.auto", "true"}, want: false}, + {name: "config set other key", cmd: setCmd, args: []string{"export.auto", "false"}, want: false}, + {name: "config set-many import.auto=false", cmd: setManyCmd, args: []string{"jira.url=x", "import.auto=false"}, want: true}, + {name: "config set-many import.auto=true", cmd: setManyCmd, args: []string{"import.auto=true"}, want: false}, + {name: "unrelated command", cmd: writeCmd, args: []string{"import.auto", "false"}, want: false}, + {name: "set without config parent", cmd: &cobra.Command{Use: "set"}, args: []string{"import.auto", "false"}, want: false}, + {name: "nil command", cmd: nil, args: []string{"import.auto", "false"}, want: false}, + {name: "too few args", cmd: setCmd, args: []string{"import.auto"}, want: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isDisablingImportAutoViaConfigCommand(tt.cmd, tt.args); got != tt.want { + t.Fatalf("isDisablingImportAutoViaConfigCommand(%v, %v) = %v, want %v", tt.cmd, tt.args, got, tt.want) + } + }) + } +} + // TestMaybeAutoImportJSONL_FallbackImporter_SkipsWhenStatisticsNil covers // the defensive nil-statistics guard: if the store reports no error but also // no counts, auto-import should skip rather than panic or assume emptiness. diff --git a/cmd/bd/config_setmany_test.go b/cmd/bd/config_setmany_test.go index 5e7430be4..30fd0388d 100644 --- a/cmd/bd/config_setmany_test.go +++ b/cmd/bd/config_setmany_test.go @@ -53,7 +53,7 @@ func TestConfigSetManyArgParsing(t *testing.T) { // TestConfigSetManyYamlKeyDetection tests that yaml-only keys are correctly identified // for routing in the set-many command. func TestConfigSetManyYamlKeyDetection(t *testing.T) { - yamlKeys := []string{"no-db", "json", "routing.mode", "routing.default", "no-push", "import.path"} + yamlKeys := []string{"no-db", "json", "routing.mode", "routing.default", "no-push", "import.auto", "import.path"} for _, key := range yamlKeys { if !config.IsYamlOnlyKey(key) { t.Errorf("expected %q to be yaml-only", key) diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 1a7fe14ab..841f7451b 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -11,6 +11,7 @@ import ( "runtime/pprof" "runtime/trace" "slices" + "strconv" "strings" "sync" "sync/atomic" @@ -1229,7 +1230,8 @@ var rootCmd = &cobra.Command{ // Skip auto-import when the user is explicitly running "bd import" — // the import command handles JSONL files itself and auto-importing // first would interfere (double-import / upsert confusion). - if shouldRunAutoImportJSONL(cmd, store, useReadOnly, globalFlag, doltCfg.ServerMode) { + if shouldRunAutoImportJSONL(cmd, store, useReadOnly, globalFlag, doltCfg.ServerMode) && + !isDisablingImportAutoViaConfigCommand(cmd, args) { maybeAutoImportJSONL(rootCtx, store, beadsDir) } @@ -1388,9 +1390,49 @@ func shouldRunAutoImportJSONL(cmd *cobra.Command, s storage.DoltStorage, useRead if cmd == nil || s == nil || useReadOnly || globalFlag || serverMode { return false } + // import.auto=false (or BD_IMPORT_AUTO=false) must disable ALL auto-import + // behavior, not just the git-hook sync path (importJSONLForSync). Without + // this check, a fresh/empty database would silently auto-import stale + // issues.jsonl on every write command regardless of the config setting + // (GH#4304). + if !config.GetBool("import.auto") { + return false + } return cmd.Name() != "import" } +// isDisablingImportAutoViaConfigCommand reports whether the command about to +// run is "bd config set import.auto false" (or an equivalent +// "bd config set-many ... import.auto=false" pair). shouldRunAutoImportJSONL +// runs in PersistentPreRun before configSetCmd/configSetManyCmd write the new +// value to config.yaml, so without this exemption the master switch would +// trigger the very auto-import it is meant to disable on its own invocation +// when a stale .beads/issues.jsonl sits next to an empty database (GH#4304). +func isDisablingImportAutoViaConfigCommand(cmd *cobra.Command, args []string) bool { + if cmd == nil || cmd.Parent() == nil || cmd.Parent().Name() != "config" { + return false + } + switch cmd.Name() { + case "set": + return len(args) >= 2 && args[0] == "import.auto" && isFalsyConfigValue(args[1]) + case "set-many": + for _, arg := range args { + key, value, ok := strings.Cut(arg, "=") + if ok && key == "import.auto" && isFalsyConfigValue(value) { + return true + } + } + } + return false +} + +// isFalsyConfigValue reports whether a config value string parses as a +// boolean false (e.g. "false", "0", "f"). +func isFalsyConfigValue(value string) bool { + parsed, err := strconv.ParseBool(value) + return err == nil && !parsed +} + func commandAllowsEmptyAutoExport(cmd *cobra.Command) bool { if cmd == nil { return false diff --git a/docs/CONFIG.md b/docs/CONFIG.md index ad9bd62dc..b88e732ad 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -360,7 +360,7 @@ Configuration keys use dot-notation namespaces to organize settings: - `export.skip_encoding_errors` - Skip issues that fail JSON encoding (default: false) - `export.write_manifest` - Write .manifest.json with export metadata (default: false) - `auto_export.error_policy` - Override error policy for auto-exports (default: `best-effort`) -- `import.auto` - Legacy hook fallback that imports JSONL after git merge/checkout only when no Dolt remote is configured (default: `true`) +- `import.auto` - Master switch for automatic JSONL imports (default: `true`). Gates both the legacy hook fallback that imports JSONL after git merge/checkout when no Dolt remote is configured, and the empty-database recovery import that write commands run when `.beads/issues.jsonl` exists but the database is empty. Set to `false` to disable all auto-imports; explicit `bd import` always works. - `sync.branch` - Name of the dedicated sync branch for beads data (see docs/PROTECTED_BRANCHES.md) - `sync.require_confirmation_on_mass_delete` - Require interactive confirmation before pushing when >50% of issues vanish during a merge AND more than 5 issues existed before (default: `false`) diff --git a/internal/config/yaml_config.go b/internal/config/yaml_config.go index cdc03fe61..1cc89b3de 100644 --- a/internal/config/yaml_config.go +++ b/internal/config/yaml_config.go @@ -71,6 +71,7 @@ var YamlOnlyKeys = map[string]bool{ "backup.git-repo": true, // Import settings + "import.auto": true, "import.path": true, // Dolt server settings diff --git a/internal/config/yaml_config_test.go b/internal/config/yaml_config_test.go index 113658d7e..b1d02841b 100644 --- a/internal/config/yaml_config_test.go +++ b/internal/config/yaml_config_test.go @@ -43,6 +43,7 @@ func TestIsYamlOnlyKey(t *testing.T) { {"backup.future-key", true}, // prefix match // Import settings + {"import.auto", true}, {"import.path", true}, {"import.orphan_handling", false},