Skip to content

No validation that --db path is a valid CodeIndex database before executing query #2037

Description

@Widthdom

Summary

WithDb() checks File.Exists for non-URI paths and catches exceptions, but doesn't validate DB schema version, integrity, or compatibility. A file.db that exists but is corrupted, empty, or from an incompatible cdidx version can reach DbContext.TryMigrateForRead() which logs errors but continues, potentially returning incorrect/partial results silently. CLI doesn't exit with a clear error if the DB is not a valid CodeIndex database.

Where

  • src/CodeIndex/Cli/QueryCommandRunner.cs:3117 (File.Exists check)
  • src/CodeIndex/Cli/QueryCommandRunner.cs:3127 (DbContext constructor no validation)
  • src/CodeIndex/Cli/QueryCommandRunner.cs:3128 (TryMigrateForRead swallows errors)

Suggested approach

  1. Add a DbContext.TryValidateIsCodeIndexDb() method that checks for required tables (codeindex_meta, symbols, references)
  2. Call this after DbContext construction but before DbReader creation in WithDb()
  3. If validation fails, print a specific error: "Error: does not appear to be a valid CodeIndex database"
  4. Include a hint: "Rebuild with cdidx index <projectPath> --db <path> to create a fresh database"
  5. Return CommandExitCodes.DatabaseError when validation fails
  6. Write tests for corrupted/truncated/wrong-schema DBs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions