Skip to content

Incorrect detection of proc-macro crate type #140929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vigoo opened this issue May 11, 2025 · 4 comments
Closed

Incorrect detection of proc-macro crate type #140929

vigoo opened this issue May 11, 2025 · 4 comments
Labels
C-bug Category: This is a bug. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue.

Comments

@vigoo
Copy link

vigoo commented May 11, 2025

I have a proc-macro crate implementing a deriver logic with #[proc_macro_derive] that compiles correctly.

If I add a harness = false line to the [lib] section though like this:

[package]
name = "golem-wasm-rpc-derive"
version = "0.0.0"
edition = "2021"
license = "Apache-2.0"
homepage = "https://golem.cloud"
repository = "https://github.com/golemcloud/golem"
description = "Golem WASM RPC derivation macros"

[lib]
proc-macro = true
harness = false       # <--- this is the line added

[dependencies]
heck = "0.5.0"
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }

[dev-dependencies]
test-r = { workspace = true }

then the compilation fails with:

   Compiling golem-wasm-rpc-derive v0.0.0 (.../wasm-rpc-derive)
error: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type
  --> wasm-rpc-derive/src/lib.rs:25:1
   |
25 | #[proc_macro_derive(IntoValue, attributes(wit_transparent, unit_case, wit_field))]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7
@vigoo vigoo added the C-bug Category: This is a bug. label May 11, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 11, 2025
@ehuss
Copy link
Contributor

ehuss commented May 11, 2025

Can you show exactly what command you ran? If it is cargo test, then indeed those two options are not going to be compatible with one another.

@vigoo
Copy link
Author

vigoo commented May 11, 2025

@ehuss thanks for the reply, your comment made me realize what is going on more precisely.

So this is in a cargo workspace. After adding the harness = false I can still build the crate with

cargo build -p golem-wasm-rpc-derive

and also

cargo build --workspace

works.

What caused the issue is that our CI build is running

cargo build --workspace --all-targets

and that fails with the provided error.

I still feel like this is a bug (or at least a very strange behavior), as I would not expect disabling the test harness to lead to an error saying the proc-macro crate is not a proc-macro crate.

@ehuss
Copy link
Contributor

ehuss commented May 12, 2025

Ah. The issue is that --all-targets builds the lib unittest for the proc-macro. With harness=true, this is fine since unittest support is built-in to the compiler so to speak, and it has special support for building that way. Although it has limited use since you still can't use any proc-macros from the unittests. With harness=false, this is essentially building as a binary crate type, and those are incompatible with defining proc-macros, and I doubt we would want to relax that restriction.

I'm not sure what your goal is with setting harness=false, but I would recommend using an integration test for that purpose.

I filed rust-lang/cargo#15519 for providing a better error message.

@jieyouxu jieyouxu added T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 12, 2025
@vigoo
Copy link
Author

vigoo commented May 12, 2025

Thanks!
Adding the harness = false in this crate of the workspace was just to avoid the need to explicitly exclude this project when running cargo test on the whole workspace, passing test harness parameters not supported by the built-in harness (in every other crate a custom test harness is used). It's definitely not a blocker, just wanted to report the misleading error message.

@vigoo vigoo closed this as completed May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-cargo Relevant to the cargo team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants