The database feature does not compile without a driver feature (E0004)

Open
#817 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start with crates/database/src/db/neutral.rs around line 247, where the issue says driver-gated enum arms cause non-exhaustive match errors, and inspect the related AnyConnection, AnyRow, AnyTransaction, and AnyQueryResult definitions. Check the crate’s feature configuration and reproduce with cargo check using only the database feature. Done means driver-free builds either compile with a clear runtime error or fail with one actionable compile error naming the required driver features.

Written by the indexing model from the issue text.

Description

Problem

A package that uses models or migrations must enable the database feature of laravel-rust. The application selects the database driver, so the package does not enable a driver feature. But database does not compile without one of database-sqlite, database-postgres, or database-mysql.

Steps to reproduce:

  1. Create a library crate with this dependency:

    [dependencies]
    laravel-rust = { version = "0.1.6", default-features = false, features = ["database"] }
    
  2. Run cargo check.

error[E0004]: non-exhaustive patterns: type `&AnyConnection` is non-empty
   --> laravel-rust-database-0.1.6/src/db/neutral.rs:247:19
    |
247 |         dispatch!(self, AnyConnection, connection => connection.begin().await.map(Into::into))
    |                   ^^^^

cargo check stops with 57 errors in laravel-rust-database. Most of the errors are E0004 on AnyConnection, AnyRow, AnyTransaction, and AnyQueryResult. All variants of these enums are behind driver features (neutral.rs L247). The problem occurs in 0.1.4 and in 0.1.6.

Who sees the problem

An application does not see the problem, because the application enables a driver and Cargo unifies the features. A package sees the problem each time that Cargo builds the package alone:

  • cargo publish verifies the package without an application.
  • docs.rs builds the package alone.
  • cargo check and cargo test run in the workspace of the package.
  • A check that builds each feature alone fails on each feature set that has no driver.

Workaround

The package adds features that only pass a driver through, for example database-sqlite = ["laravel-rust/database-sqlite"]. An application never uses these features. The package then publishes with --all-features and sets all-features = true for docs.rs. Its feature checks must also skip each feature set that has no driver. Each package that uses the database must repeat this setup.

Laravel

In Laravel for PHP, the models and migrations of a package do not know the driver. The DB_CONNECTION value of the application selects the driver.

Proposal

Make database compile without a driver feature. For example, each Any* enum can get one more arm when no driver feature is enabled. The crate then compiles, and a query fails at runtime with a clear error such as "no database driver is enabled".

If you do not want that behavior, add a compile_error! that names the three driver features. The 57 errors then become one error that tells the user what to do.

Dominant language
Rust
Stars
847
Forks
174
Avg merge
5h 40m
Merged PRs (30d)
167

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust-framework

All issues in laravel/rust-framework

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.