bazelbuild / bazelbuild/rules_rust

[Bug] Unused transitive dependencies got built

Open
#3,001 3 comments 0 reactions 0 assignees View on GitHub
needs-design
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.
![QQ_1731652246337](https://github.com/user-attachments/assets/aef9a1c3-f8d7-4a6b-998c-54eed06fb99c)
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.