support overriding a crate's opt level per compilation mode

Open
#4,224 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
74/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Quiet
Tech stack
rust
Domain
build-system

Research direction

Start in rust.bzl to trace the rust_* rule attributes, then follow construct_arguments where the toolchain opt level is substituted. Add the compilation-mode-keyed opt_level behavior described in the issue and verify that listed crates override the mode's level while unlisted modes and crates retain current behavior.

Written by the indexing model from the issue text.

Description

Cargo's canonical fast-dev-build recipe optimizes chosen packages while the rest of the workspace keeps opt-level 0 ([profile.dev.package."<name>"] opt-level = 1). rules_rust has no equivalent: rust_toolchain.opt_level applies to every crate in a compilation mode, and the only per-crate escape hatch is raw -C flags in rustc_flags, which bypasses the compilation-mode model — one rustc_flags value applies in every mode, so a fastbuild override leaks into -c opt builds too.

Proposal: an opt_level attr on the rust_* rules, keyed by compilation mode, substituting the toolchain's opt level for that crate:

rust_library(
    name = "hot_crate",
    ...
    opt_level = {"fastbuild": "1"},
)

Modes not listed, and crates without the attr, behave exactly as today.

Data point from our monorepo: a profile of a CPU-bound e2e suite put ~55% of CPU self-time in a handful of rarely-edited foundation crates; pinning those to opt-level 1 under fastbuild cut the heaviest test from 158s to ~85s while leaving every other crate's compile time untouched. We carry this as a ~25-line patch (attr in rust.bzl, one substitution in construct_arguments) and can send a PR if the shape is acceptable.

Dominant language
Starlark
Stars
857
Forks
701
Avg merge
4d 7h
Merged PRs (30d)
46

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_rust

All issues in bazelbuild/rules_rust

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.