JuliusBrussee/caveman · error
open sqlite
Error message
open sqlite %q: %w
What it means
This error wraps the failure of sql.Open("sqlite", dsn) while opening the CCR store's SQLite database. It is thrown by openWithBudgetHooks after PrepareSQLitePathCanonical has prepared the file, so the driver itself rejected the DSN or failed to initialize. Note the store opens with mode=rw when the file already exists, so a missing file also surfaces here.
Solutions
- Ensure the SQLite driver is imported for its side effects (e.g. _ "modernc.org/sqlite") in the binary opening the store
- Verify the canonical path exists and is writable before calling Open; recreate it via PrepareSQLitePath if it was deleted
- Check that no other process is deleting/replacing the database file between preparation and open
- Enable driver-level debug by unwrapping the wrapped %w error to see the underlying driver message
Example fix
// before
import (
"database/sql"
)
// after
import (
"database/sql"
_ "modernc.org/sqlite"
) Defensive patterns
Strategy: try-catch
Validate before calling
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
return fmt.Errorf("ccr db %s must be prepared before Open", dbPath)
} Try / catch
store, err := ccr.Open(dbPath)
if err != nil {
var pe *fs.PathError
if errors.As(err, &pe) { /* inspect underlying driver failure */ }
return fmt.Errorf("open ccr store: %w", err)
} Prevention
- Always blank-import the SQLite driver package
- Prepare the path with PrepareSQLitePath before Open
- Ensure the db file is not deleted between prepare and open
- Run Open as a user with write access to the db directory
When it happens
Trigger: Malformed DSN options appended by the store, the SQLite driver not registered (blank import of modernc.org/sqlite or mattn/go-sqlite3 missing), or the prepared database file vanishing between preparation and open while mode=rw forbids recreation.
Common situations: Deployments where the data directory is cleaned by another process between prepare and open; builds forgetting the driver side-effect import; corrupt or unreadable file permissions passed as canonicalPath.
Understand the failure class
Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.
Related errors
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/050949c7d7c0afbf.
Report an issue: GitHub.
Appendix: source
Thrown at engine/ccr/store_sqlite.go:191
if err != nil {
return nil, err
}
if afterPrepare != nil {
afterPrepare()
}
before, err := inspectSQLiteGeneration(canonicalPath)
if err != nil {
return nil, err
}
dsn := SQLiteDSN(canonicalPath)
if canonicalPath != ":memory:" {
// PrepareSQLitePathCanonical already created the file. Never recreate
// it if it vanishes between preparation and the driver's open.
dsn += "&mode=rw"
}
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, fmt.Errorf("open sqlite %q: %w", canonicalPath, err)
}
// One writer connection. With a single connection every statement serializes
// in-process (no self-contention) while busy_timeout absorbs cross-process
// contention; it is also mandatory for an in-memory DSN, where a second
// pooled connection would be a second, empty database.
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
walJournal := false
if canonicalPath != ":memory:" {
if err := persistSQLiteWAL(db); err != nil {
closeSQLiteAfterOpenFailure(db, canonicalPath)
return nil, fmt.Errorf("configure sqlite journal: %w", err)
}
// A filesystem that cannot back -shm (NFS, SMB, some roaming profiles)
// leaves the DSN's journal_mode request unapplied. Requiring the WAL
// sidecars there would report a perfectly good database as replaced.
mode, err := sqliteJournalMode(db)
if err != nil {View on GitHub (pinned to 3ee70a1026)