Files
mpc/cmd/mpcd/sqlite_driver_test.go
zeekayandHanzo Dev cc315fae73 fix: one "sqlite" driver registrar — mpcd panicked before main
`go test ./...` here could not start a single test binary:

    panic: sql: Register called twice for driver sqlite
    database/sql.Register(...)         database/sql/sql.go:63
    modernc.org/sqlite.init.0()        modernc.org/[email protected].0/sqlite.go:57

database/sql keys drivers by NAME and panics on a duplicate, so this is an
init-time process kill, not a test failure — mpcd the daemon died the same way.

mpcd links three SQLite-backed stores in one process. Two of them —
hanzoai/base's core and hanzoai/tasks' shard store — reach SQLite through
github.com/hanzoai/sqlite, which owns the "sqlite" name (delegating to
modernc.org/sqlite under !cgo and hanzoai/csqlite+SQLCipher under cgo, exactly
one registration either way). The third, luxfi/mpc's pkg/infra SQLiteMeta,
blank-imported modernc.org/sqlite directly. Two module paths, one driver name,
both init()s run.

Enumerated with `go list -deps` over the whole graph, the registrars of the name
"sqlite" were: modernc.org/sqlite (sqlite.go:57, driverName = "sqlite") and
github.com/hanzoai/sqlite (driver_cgo.go:43 under cgo, the modernc blank import
under !cgo). hanzoai/csqlite registers "sqlite3" and pgx/v5/stdlib registers
"pgx" — different names, no collision. The sole importer of modernc.org/sqlite
was github.com/luxfi/mpc/pkg/infra.

Fixed upstream where the offending import lives, not shimmed around here:
luxfi/mpc v1.17.16 routes SQLiteMeta through hanzoai/sqlite. Same shape as the
hanzoai/o11y fix — one package owns the driver, everyone else depends on it. No
driver was swapped and no build tag was added; the pure-Go/cgo split is
hanzoai/sqlite's own and is unchanged.

Deps move with it: luxfi/mpc v1.14.13 -> v1.17.16 and hanzoai/base v1.3.0 ->
v1.5.8 (v1.3.0's tag had also moved out from under go.sum, so a clean checkout
could not resolve it at all).

cmd/mpcd/sqlite_driver_test.go pins the invariant at the composition point that
actually broke. Its teeth are the import graph — the file compiles into package
main, so a second registrar panics during init and takes the whole test binary
down before any test runs. Verified red (panic, on luxfi/mpc v1.17.15 with every
other dep held fixed) then green, under CGO_ENABLED=0 and CGO_ENABLED=1.

go build ./... and go test ./... pass under both CGO settings.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 12:27:24 -07:00

46 lines
1.7 KiB
Go

package main
import (
"database/sql"
"slices"
"testing"
)
// TestSQLiteDriverRegisteredOnce pins the invariant that made this whole package
// unrunnable: mpcd links three SQLite-backed stores in one process — luxfi/mpc's
// pkg/infra SQLiteMeta, hanzoai/base's core, and hanzoai/tasks' shard store — and
// database/sql panics if two of them register the driver NAME "sqlite".
//
// panic: sql: Register called twice for driver sqlite
// modernc.org/sqlite.init.0() modernc.org/[email protected]/sqlite.go:57
//
// pkg/infra used to blank-import modernc.org/sqlite while the others reach SQLite
// through github.com/hanzoai/sqlite, so the second init() killed the process
// before main — and `go test ./...` here could not even start. Every store now
// goes through hanzoai/sqlite, the one package that owns that name (it delegates
// to modernc under !cgo and hanzoai/csqlite+SQLCipher under cgo).
//
// The teeth are in the import graph, not the assertions: this file is compiled
// into the mpcd package, so a regression to a second registrar panics during
// init and takes the whole test binary down before any test runs. The assertions
// below just make the intent legible.
func TestSQLiteDriverRegisteredOnce(t *testing.T) {
drivers := sql.Drivers()
if !slices.Contains(drivers, "sqlite") {
t.Fatalf("no %q driver registered; hanzoai/sqlite must register it: %v", "sqlite", drivers)
}
// sql.Drivers is deduplicated by construction (sql.Register panics on a
// duplicate name), so reaching this line already proves single registration.
var n int
for _, d := range drivers {
if d == "sqlite" {
n++
}
}
if n != 1 {
t.Fatalf("driver %q registered %d times, want 1", "sqlite", n)
}
}