|
| 1 | +# proc-macro-srv-cli |
| 2 | + |
| 3 | +A standalone binary for the `proc-macro-srv` crate that provides procedural macro expansion for rust-analyzer. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +rust-analyzer uses a RPC (via stdio) client-server architecture for procedural macro expansion. This is necessary because: |
| 8 | + |
| 9 | +1. Proc macros are dynamic libraries that can segfault, bringing down the entire process, so running them out of process allows rust-analyzer to recover from fatal errors. |
| 10 | +2. Proc macro dylibs are compiled against a specific rustc version and require matching internal APIs to load and execute, as such having this binary shipped as a rustup component allows us to always match the rustc version irrespective of the rust-analyzer version used. |
| 11 | + |
| 12 | +## The `sysroot-abi` Feature |
| 13 | + |
| 14 | +**The `sysroot-abi` feature is required for the binary to actually function.** Without it, the binary will return an error: |
| 15 | + |
| 16 | +``` |
| 17 | +proc-macro-srv-cli needs to be compiled with the `sysroot-abi` feature to function |
| 18 | +``` |
| 19 | + |
| 20 | +This feature is necessary because the proc-macro server needs access to unstable rustc internals (`proc_macro_internals`, `proc_macro_diagnostic`, `proc_macro_span`) which are only available on nightly or with `RUSTC_BOOTSTRAP=1`. |
| 21 | +rust-analyzer is a stable toolchain project though, so the feature flag is used to have it remain compilable on stable by default. |
| 22 | + |
| 23 | +### Building |
| 24 | + |
| 25 | +```bash |
| 26 | +# Using nightly toolchain |
| 27 | +cargo build -p proc-macro-srv-cli --features sysroot-abi |
| 28 | + |
| 29 | +# Or with RUSTC_BOOTSTRAP on stable |
| 30 | +RUSTC_BOOTSTRAP=1 cargo build -p proc-macro-srv-cli --features sysroot-abi |
| 31 | +``` |
| 32 | + |
| 33 | +### Installing the proc-macro server |
| 34 | + |
| 35 | +For local testing purposes, you can install the proc-macro server using the xtask command: |
| 36 | + |
| 37 | +```bash |
| 38 | +# Recommended: use the xtask command |
| 39 | +cargo xtask install --proc-macro-server |
| 40 | +``` |
| 41 | + |
| 42 | +## Testing |
| 43 | + |
| 44 | +```bash |
| 45 | +cargo test --features sysroot-abi -p proc-macro-srv -p proc-macro-srv-cli -p proc-macro-api |
| 46 | +``` |
| 47 | + |
| 48 | +The tests use a test proc macro dylib built by the `proc-macro-test` crate, which compiles a small proc macro implementation during build time. |
| 49 | + |
| 50 | +**Note**: Tests only compile on nightly toolchains (or with `RUSTC_BOOTSTRAP=1`). |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +The binary requires the `RUST_ANALYZER_INTERNALS_DO_NOT_USE` environment variable to be set. This is intentional—the binary is an implementation detail of rust-analyzer and its API is still unstable: |
| 55 | + |
| 56 | +```bash |
| 57 | +RUST_ANALYZER_INTERNALS_DO_NOT_USE=1 rust-analyzer-proc-macro-srv --version |
| 58 | +``` |
| 59 | + |
| 60 | +## Related Crates |
| 61 | + |
| 62 | +- `proc-macro-srv`: The core server library that handles loading dylibs and expanding macros, but not the RPC protocol. |
| 63 | +- `proc-macro-api`: The client library used by rust-analyzer to communicate with this server as well as the protocol definitions. |
| 64 | +- `proc-macro-test`: Test harness with sample proc macros for testing |
| 65 | +- `proc-macro-srv-cli`: The actual server binary that handles the RPC protocol. |
0 commit comments