-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
Problem
When proc macros print to stdout, the output is reflected in the cargo output. For instance, the following proc macro will cause Hello
to be printed in the compiler output, even when message-format=json
is passed, even though it's not a valid JSON object. This will cause various tools (such as cargo-metadata
, rust-analyzer
, jq
...) to fail to parse the message, and give up.
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro_attribute]
pub fn hello(_attr: TokenStream, item: TokenStream) -> TokenStream {
println!("Hello");
item
}
I'm not sure if this is considered a cargo or a rustc bug ^^'.
Steps
- Install
jq
git clone https://github.com/roblabla/cargo-proc-macro-print-bug
cargo build --message-format=json | jq .
This will cause jq
to error with "parse error: Invalid numeric literal at line 3, column 0", which is caused by the string "Hello", printed by the proc macro, to appear in stdout
.
Possible Solution(s)
I imagine the proc macro output could be:
- Ignored/silenced
- Wrapped in a
CompilerMessage
JSON object - Wrapped in a new kind of JSON object, maybe a
GenericMessage
?
Notes
cargo 1.42.0 (8633429 2020-01-31)
rustc 1.42.0 (b8cedc004 2020-03-09)