Proc-macro dependencies are compiled with dev-dependencies features during cargo test

⚓ Rust    📅 2025-09-10    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 63 days ago. The information described in this article may have changed.

Repository: 10takla/feature-problem/tree/tmp

Problem

When building with cargo build -p grammar, everything works as expected:

$ cargo build -p grammar
     Removed 131 files, 27.5MiB total
   Compiling parser v0.0.0 (/workspaces/codespaces-blank/parser)
   Compiling grammar v0.0.0 (/workspaces/codespaces-blank/grammar)
[parser/src/lib.rs:2:8] cfg!(feature = "logs") = false
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.42s

But when running tests (cargo test), during the build of the grammar! macro, messages are printed to the console:

$ cargo test -p grammar
     Removed 76 files, 18.2MiB total
   Compiling parser v0.0.0 (/workspaces/codespaces-blank/parser)
   Compiling grammar v0.0.0 (/workspaces/codespaces-blank/grammar)
[parser/src/lib.rs:2:8] cfg!(feature = "logs") = true
parsing...
[parser/src/lib.rs:2:8] cfg!(feature = "logs") = true
parsing...
[parser/src/lib.rs:2:8] cfg!(feature = "logs") = true
parsing...
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.77s
     Running unittests src/lib.rs (target/debug/deps/grammar-93eb89580b25eadd)

grammar/Cargo.toml

[package]
edition = "2018"
name = "grammar"
resolver = "2"

[lib]
proc-macro = true

[dependencies]
parser.workspace = true

[dev-dependencies]
parser = { workspace = true, features = ["logs"] }

Issue

When running cargo test, all targets that declare the grammar! macro are built with the "parser/logs" feature enabled — even for main and test.

Is there a way to design this so that the macro is built without the feature, but the tests run with the feature enabled at runtime?

5 posts - 3 participants

Read full topic

🏷️ Rust_feed