bazelbuild / bazelbuild/rules_rust
[Bug] Unused transitive dependencies got built
- Dominant language
- Starlark
- Stars
- 843
- Forks
- 651
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 15
Description
Mini reproducible repository: https://github.com/fa93hws/rules_rust_transitive_deps
## Step to reproduce:
1. Checkout repo
2. `rustup target add wasm32-wasi`
3. `cd swc_plugin`
4. `cargo build-wasi` and it passes
5. `bazel build :wasm`, and it failed with
```
...
Running Cargo build script onig_sys failed
...
--stderr:
error occurred: Failed to find tool. Is `DUMMY_GCC_TOOL` installed?
```
## Step to fix the error
https://github.com/fa93hws/rules_rust_transitive_deps/pull/1 fixes the issue, by removing `miette = { version = "7.2.0", features = ["fancy", "syntect-highlighter"] }` from the closure.
## Where I think goes wrong
I'm pretty new to `rust` and `rules_rust` so I maybe wrong. I'm not sure about the GCC tooling error but I think it's irrelevant here. The real problem is that `onig_sys` shouldn't be included in the build graph.
If I run `cargo build-wasi` under `swc_plugin` dir, the output looks like this, and there's no `onig_sys` at all.

But it's somehow built by `rules_rust`.
For context, `onig_sys` is a tranistive deps of `miette` with `features = ["fancy", "syntect-highlighter"]`. When there's only `swc_plugin` in the workspace, `miette` only has
```
dependencies = [
"cfg-if",
"miette-derive",
"owo-colors",
"textwrap",
"thiserror",
"unicode-width",
]
```
in the lock file.
But when `miette = { version = "7.2.0", features = ["fancy", "syntect-highlighter"] }` is added to another project in the workspace (`another_rust_project`), `miette`'s dependencies becomes:
```
dependencies = [
...
"supports-unicode",
"syntect", <- it depends on onig_sys
"terminal_size 0.3.0",
...
]
```
So I feel that the issue is that, `rules_rust` will take a look at the lock file, and build all transitive dependencies no matter whether they are actually needed or not? As a comparison, `cargo build` builds the needed dependencies only.
## Other info:
Platform: MacOS 15.1 (24B83)
Rust version:
```
rustc --version
rustc 1.82.0 (f6e511eec 2024-10-15)
```
Contributor guide
Assessment
This issue has not been assessed yet.