diff --git a/cmd/bd/identity_preview_gate_test.go b/cmd/bd/identity_preview_gate_test.go index 76a086e79..0bd331558 100644 --- a/cmd/bd/identity_preview_gate_test.go +++ b/cmd/bd/identity_preview_gate_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "net" "os" "path/filepath" "strings" @@ -14,6 +15,7 @@ import ( "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/storage/embeddeddolt" ) // TestIsBootstrapPreviewErr pins the classifier that decides whether the @@ -22,17 +24,42 @@ import ( // shape embeddeddolt produces for "no database on disk yet" and nothing else, // including an error that merely happens to also be wrapped. func TestIsBootstrapPreviewErr(t *testing.T) { - wrappedNotExist := fmt.Errorf("embeddeddolt: no embedded database at %s: %w", "/tmp/x/embeddeddolt", os.ErrNotExist) + // The bootstrap shape, built the way embeddeddolt's read-only open builds + // it around its failed stat of the data dir. + dataDir := filepath.Join(t.TempDir(), "embeddeddolt") + _, statErr := os.Stat(dataDir) + noEmbeddedDB := fmt.Errorf("%w at %s: %w", embeddeddolt.ErrNoEmbeddedDatabase, dataDir, statErr) + unreadableDataDir := fmt.Errorf("%w at %s: %w", embeddeddolt.ErrNoEmbeddedDatabase, dataDir, os.ErrPermission) wrappedOther := fmt.Errorf("dial tcp 127.0.0.1:3306: %w", errors.New("connect: connection refused")) + // Real ENOENTs from sources that are not "no local database yet" (bd-wlygc). + // A hand-built errors.New can never satisfy errors.Is(err, os.ErrNotExist), + // so only a real syscall error tells a narrow classifier from one that + // matches every ENOENT. The dial is what dolt server mode wraps for a + // configured socket nothing listens on; it is relative to a fresh cwd so + // the path stays under the unix socket path limit wherever t.TempDir lives. + t.Chdir(t.TempDir()) + _, dialErr := net.Dial("unix", "absent.sock") + absentSocket := fmt.Errorf("Dolt server unreachable at %s: %w", "absent.sock", dialErr) + _, manifestErr := os.Stat(filepath.Join(t.TempDir(), "beads", ".dolt", "noms", "manifest")) + guttedDataDir := fmt.Errorf("embeddeddolt: open db: %w", manifestErr) + for _, err := range []error{absentSocket, guttedDataDir} { + if !errors.Is(err, os.ErrNotExist) { + t.Fatalf("precondition: %v is not an ENOENT, so it cannot tell a narrow classifier from a broad one", err) + } + } + tests := []struct { name string err error want bool }{ {"nil error", nil, false}, - {"direct os.ErrNotExist", os.ErrNotExist, true}, - {"wrapped os.ErrNotExist (embedded store shape)", wrappedNotExist, true}, + {"embedded store's no-database error", noEmbeddedDB, true}, + {"bare os.ErrNotExist", os.ErrNotExist, false}, + {"embedded store's data dir unreadable", unreadableDataDir, false}, + {"real ENOENT dialing an absent unix socket", absentSocket, false}, + {"real ENOENT from a file missing inside the data dir", guttedDataDir, false}, {"generic non-ENOENT error", errors.New("connection refused"), false}, {"wrapped non-ENOENT error", wrappedOther, false}, } diff --git a/cmd/bd/main.go b/cmd/bd/main.go index c083fed90..3cce047b5 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -2749,8 +2749,14 @@ func flushBatchCommitOnShutdown() { // run, not a workspace problem. It must stay narrow: any other previewErr // (unreachable server, bad config, ...) has to refuse rather than silently // wave the real, mutating open through (be-3bt2e). +// +// So it matches the embedded store's own no-database error, and only when the +// cause is a missing data dir — not any ENOENT. A dial to a configured unix +// socket whose server is down satisfies errors.Is(err, os.ErrNotExist) too, +// as does a data dir missing an internal file, and neither is a first run +// (bd-wlygc). func isBootstrapPreviewErr(err error) bool { - return errors.Is(err, os.ErrNotExist) + return errors.Is(err, embeddeddolt.ErrNoEmbeddedDatabase) && errors.Is(err, os.ErrNotExist) } // refuseUnverifiablePreviewOpen renders the identity gate's refusal when the diff --git a/internal/storage/embeddeddolt/readonly.go b/internal/storage/embeddeddolt/readonly.go index d01deb2b3..cfa1402ce 100644 --- a/internal/storage/embeddeddolt/readonly.go +++ b/internal/storage/embeddeddolt/readonly.go @@ -15,3 +15,14 @@ import "errors" // refuses it must not turn an otherwise successful command into a non-zero // exit after the fact. var ErrReadOnly = errors.New("embeddeddolt: store is read-only") + +// ErrNoEmbeddedDatabase is wrapped, together with the failed stat's own error, +// by OpenReadOnly and OpenForPreviewCommand when beadsDir has no embedded data +// directory. +// +// Exported, and declared here without a cgo build tag, for the same reason as +// ErrReadOnly: the CLI's pre-open workspace identity check has to tell "no +// local database yet" — a first run, which it skips — apart from every other +// failed preview open, which it refuses. errors.Is(err, os.ErrNotExist) alone +// cannot: a dial to a unix socket nothing listens on satisfies it too. +var ErrNoEmbeddedDatabase = errors.New("embeddeddolt: no embedded database") diff --git a/internal/storage/embeddeddolt/store.go b/internal/storage/embeddeddolt/store.go index 842717273..282e426e1 100644 --- a/internal/storage/embeddeddolt/store.go +++ b/internal/storage/embeddeddolt/store.go @@ -356,7 +356,7 @@ func openReadOnly(ctx context.Context, beadsDir, database, branch string, checkB } dataDir := filepath.Join(absBeadsDir, "embeddeddolt") if _, err := os.Stat(dataDir); err != nil { - return nil, fmt.Errorf("embeddeddolt: no embedded database at %s: %w", dataDir, err) + return nil, fmt.Errorf("%w at %s: %w", ErrNoEmbeddedDatabase, dataDir, err) } s := &EmbeddedDoltStore{