Skip to content

Confusing error with lib.harness and proc-macro #15519

@ehuss

Description

@ehuss

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 rust-lang/rust#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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Error and warning messages generated by Cargo itself.A-proc-macroArea: compiling proc-macrosS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions