Skip to content

Instantly share code, notes, and snippets.

@TooManyBees
Last active March 18, 2018 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TooManyBees/dc95d064cd3eee506d8047c2745ea9a8 to your computer and use it in GitHub Desktop.
Save TooManyBees/dc95d064cd3eee506d8047c2745ea9a8 to your computer and use it in GitHub Desktop.
Rust workspace/proc macro dependencies

The problem

PS C:\bees\code\super-metroid\web-viewer> cargo build --target wasm32-unknown-unknown
   Compiling proc-macro2 v0.2.3
   Compiling sm v0.1.0 (file:///C:/bees/code/super-metroid/sm)
error[E0463]: can't find crate for `proc_macro`
  --> C:\Users\Bees\.cargo\registry\src\github.com-1ecc6299db9ec823\proc-macro2-0.2.3\src\lib.rs:32:1
   |
32 | extern crate proc_macro;
   | ^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

The workspace's Cargo.tomls follow.

If we go by the comments in rust-lang/rust#40174, then it seems like we can't have the proc-macro crate as a dependency when building a binary for the wasm32-unknown-unknown target.

The ideal build situation:

  • procedural-macro requires library with its codegen feature in order to build and run itself
  • the codegen feature ends up requiring quote (and proc-macro2 and proc-macro as a result) in order to implement the quote::ToTokens trait
  • while the binary crate requires library (without the codegen feature) as well as procedural-macro

What actually happens:

  • Cargo only builds one version of library, the version with codegen
  • the codegen feature ends up "poisoning" the binary crate so that it can't compile on a different target.
[package]
name = "binary"
[lib]
crate-type = ["cdylib"]
[dependencies]
procedural-macro = { path = "../procedural-macro" }
library = { path = "../library" } # not using the codegen feature here
[package]
name = "library"
[features]
codegen = ["quote"]
[dependencies]
quote = { version = "0.4", optional = true } # implements `quote::ToTokens` for this crate's structs
[package]
name = "procedural-macro"
[lib]
proc-macro = true
[dependencies]
library = { path = "../library", features = [ "codegen" ] }
# others like `proc-macro2`, `quote`, `syn`, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment