rust-lang / rust-lang/cargo

Hermetic build mode

Open
#9,506 5 comments 18 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-reproducibility C-feature-request E-hard S-needs-rfc
Dominant language
Rust
Stars
15.5k
Forks
3k
Avg merge
23h 30m
Merged PRs (30d)
51

Description

Meta: this should be an RFC, but I am unlikely to write one soon. Creating mostly to coordinate discussion.

Problem: today, it's possible to get byte-for-byte identical results compilation results (especially with wasm), but it is impossible to guarantee, because ambient environment (~/.cargo/config, CARGO_HOME, etc) can influence the build. That is, builds are reproducible, but are not hermetic. As a specific example, if I publish a wasm project with Cargo.lock and rust-toolchain.toml, folks running cargo build will generally get the same results, unless someone has RUSTFLAGS set.

Solution: add a mechanism to Cargo to out-out of ambient environment. Strawman proposal -- when --hermetic flag is passed, cargo guarantees that the builds are only affected by the contents of the current working directory.

I don't know an exhaustive list of things that hypothetical --hermetic should forbid, but here are some candidates:

  • access to .cargo/config outside of the current directory
  • access to env vars like RUSTFLAGS, unless explicitly opted into (a-la --hermetic --allow-env RUSTFLAGS)
  • access to PATH? This intersects with rustup. compiler version obviously affects the results, and we need compiler, so PATH is importaet. Perhaps printing rustc -vv during hermetic build is the way to go?
  • --hermetic should imply --locked (valid lockfile)
  • --hermetic should not imply --frozen. That is, Cargo should be able to fetch sources from the internet (and uses CARGO_HOME for caches), as long as it guarantees that the result is reproducible.

A related feature is https://github.com/rust-lang/cargo/pull/7894, #7887. It allows to control .cargo/config specifically. I, however, feel that we want to make a more specific promise about properties (hermeticity), and tread config handling as an implementation detail.

cc @jsgf

Contributor guide

Open the contributing guide

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.

Research direction

Start by reading the issue's proposed Cargo --hermetic behavior and the related pull request #7894 and issue #7887. The scope is not yet defined: an RFC would need to establish which ambient configuration and environment inputs are excluded or explicitly allowed, and what reproducibility guarantee counts as done.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.