rust-lang / rust-lang/cargo

Confusing error with lib.harness and proc-macro

Open
#15,519 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-proc-macro S-needs-design
Dominant language
Rust
Stars
15.5k
Forks
3k
Avg merge
23h 30m
Merged PRs (30d)
51

Description

The combination of harness=false and proc-macro=true are incompatible since proc-macros cannot be bin crates. I'd recommend making this a warning or error.

The following illustrates the current behavior generating a potentially confusing error:

[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[lib]
proc-macro = true
harness = false
// src/lib.rs
use proc_macro::TokenStream;

#[proc_macro]
pub fn foo(_input: TokenStream) -> TokenStream {
    "".parse().unwrap()
}

#[cfg(test)]
fn main() {}

Running cargo build --all-targets:

   Compiling foo v0.1.0 (/Users/eric/Temp/foo)
error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
 --> src/lib.rs:3:1
  |
3 | #[proc_macro]
  | ^^^^^^^^^^^^^

error: could not compile `foo` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...

Previously reported at https://github.com/rust-lang/rust/issues/140929.

Meta
cargo 1.89.0-nightly (7918c7eb5 2025-04-27)
release: 1.89.0-nightly
commit-hash: 7918c7eb59614c39f1c4e27e99d557720976bdd7
commit-date: 2025-04-27
host: aarch64-apple-darwin
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.7.1 (sys:0.4.80+curl-8.12.1 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Mac OS 15.4.1 [64-bit]

Contributor guide

Open the contributing guide

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.

Research direction

Start with the Cargo.toml combination of harness = false and proc-macro = true, then reproduce it using cargo build --all-targets with the src/lib.rs example. Trace how Cargo constructs the lib test target; done means the incompatible configuration produces a deliberate, clear diagnostic instead of the confusing #[proc_macro] error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.