bazelbuild / bazelbuild/rules_rust
Add Cargo expand functionality
- Dominant language
- Starlark
- Stars
- 843
- Forks
- 651
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 15
Description
[Cargo expand](https://github.com/dtolnay/cargo-expand) is an extremely useful command for debugging Rust source files with complicated proc macros. `cargo expand` is just a wrapper around `rustc` which passes in the flag `-Zunpretty=expanded`.
**Proposal**
Add a `rust_expand` and `rust_expand_aspect` to the core rules which functions similarly to the clippy rules. The `rust_expand` rule can be used to generated an expanded `.rs` file and the `rust_expand_aspect` can be used to produce such files on demand.
Example:
```
# .bazelrc
build:expanded --aspects=@rules_rust//rust:defs.bzl%rust_expand_aspect
build:expanded --output_groups=+expanded
```
```shell
bazel build --config=expanded //:ok_binary
INFO: Analyzed target //:ok_binary (1 packages loaded, 2 targets configured).
INFO: Found 1 target...
Aspect @rules_rust//rust/private:expand.bzl%rust_expand_aspect of //:ok_binary up-to-date:
bazel-bin/ok_binary.expand.rs
INFO: Elapsed time: 0.149s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
```
`ok_binary`'s `src/main.rs`
```rust
fn main() {
println!("Hello world");
}
```
And the expanded output:
```rust
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
fn main() {
{
::std::io::_print(::core::fmt::Arguments::new_v1(&["Hello world\n"],
&[]));
};
}
```
Contributor guide
Assessment
This issue has not been assessed yet.