Registry Mirror
Skip to content

Conversation

@weihanglo

@weihanglo weihanglo commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Stabilization report: -Zembed-metadata for rlib

What is stabilized

This stabilize the cargo side of -Zembed-metadata for rlib artifacts
which changes the default behavior without providing any new user facing config:

  • Cargo will pass -Cembed-metadata=no to rustc unconditionally
    when building rlib crate type.
    The rlib crate type will contain only a small stub of metadata.
    The full metadata will be stored in a separate .rmeta file.
  • In order to link the new slimmed rlib,
    Cargo will need to pass extra --extern flag to the corresponding .rmeta file
    to get the rustc metadata.
    This affects bin, staticlib, cdylib, proc-macro crate types (except rlib and dylib).

Other user-observable changes

  • The rustc command line length grows because the number of --extern flags doubles.
    Real world projects like cargo, the lagest cmdline length grows from 34k bytes to 43k bytes in a short target dir path (~26%).
  • This reduce the size of the target directory by 5% to 35% on real world projects,
    according to Kobzol's blog post and the Inside Rust post
  • rlib and becomes way less useful when distributed standalone,
    unless the companion .rmeta is copied and explicitly passed with an extra --extern foo=<path>.rmeta for external build tool.

What is not stabilized / included

  • -Zembed-metadata=yes|no flag for dylib
  • Uplifting .rmeta next to the uplifted rlib. rlib is considered a private/intermediate artifact. See fix!: stop uplifting rlibs  #17715.

Feedback

Landing plan

When -Cembed-metadata is stabilized (1.101), cargo can do a simple switch for rlib.

Unresolved questions

Is this a breaking change?

Proposed: May not be.

An uplifted rlib/dylib was still there in the same place,
and its object code is still there as well.
External linkers or loading dylib can still loading them if needed.

The things got removed are rustc metadata inside it.
The metadata was never a thing user can rely on or distribute easily.
Reasons being:

  • Only readable by exactly the same rustc version
  • Not useable alone without transitive deps's rlibs,
    which are already internal/private.
    They anyway need cargo --message-format=json to find their locations.
  • cargo --message-format=json already prints uplifted rlib's internal path.
  • We have changed the default value of split-debuginfo which also changed the final artifact, and that time it affected executables not just rlib (Default macOS targets to unpacked debuginfo #9298)

However, this is really a compatibility note for those already copying/distributing rlib around, even they have already use --message-format=json. We should have a outstanding note in release notes' compatibility note section.

A stable opt-out flag

Proposed: No.

It is unclear why people need this except not breaking their existing CI pipeline.
The code search shows that most nightly reports were fixes by passing .rmeta.
If stable users need it for reasonable use cases, we can always add an stable option later later.

Command-line length

Command-line argument length does grow,
but in 1.100.0 Cargo uses the new build-dir layout,
which is going to force people to use more response files.
At the time this is stabilized,
users and rustc wrapper tools (e.g., sccache) should already be prepared to be compatible with longer command-line argument length.

Some data:

Project Largest call, embed=yes Largest call, embed=no --extern
sccache 38,500 bytes 46,409 (+20.4%) 70 → 139
cargo 34,586 bytes 43,865 (+26.8%) 82 → 163

Implementation history

PR Merged Title
#15378 2025-05-06 add support for -Zembed-metadata
#16513 2026-01-15 invalidate the whole build cache when the flag changes
#17168 2026-07-08 reduce rustc -L args in the new build-dir layout
#17149 2026-07-12 rename -Zno-embed-metadata to -Zembed-metadata=no
#17266 2026-07-27 allow setting -Zembed-metadata from config
#17267 2026-08-17 on by default on nightly
#17621 2026-10-22 stop uplifting rlib on nightly
#17715 2026-11-30 stop uplifting rlib stabilization

Follow-ups after stabilization

  • Compatibility note in the release notes
  • Consider Cargo stopping dylib uplift as well
  • After stop uplifting dylib, fully stabilize the -Cembed-metdata=no behavior for all crate types

Acknowledgement

See rust-lang/rust#163436


🤖 LLM disclosure: collecting code search results on GitHub; generating "Implementation history" table; commandline argument length benchmark

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-cfg-expr Area: Platform cfg expressions A-documenting-cargo-itself Area: Cargo's documentation labels Sep 29, 2026
@epage

epage commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Some questions I have that don't seem to be addressed in the stabilization report

  • How do our stability guarantees fit into this?
  • If rlibs (and dylibs?) have no guarantees, should we clarify that first by no longer uplifting them?

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 19, 2026
…=petrochenkov

Stabilize `-Zembed-metadata`

Tracking issue: rust-lang#139165

This PR proposes to stabilize the `-Zembed-metadata` compiler flag, so that Cargo can start using `-Cembed-metadata=no` by default on the stable channel.

The flag can be used to avoid duplicating the metadata of Rust crates within `.rlib` and `.rmeta` files. This can reduce the size of the `target` directory anywhere from 5% to 35% on real-world projects, depending on the chosen compilation profile ([1][kobzol-blog], [2][cargo-blog]). Additionally, `-Cembed-metadata=no` also removes the metadata out of Rust `dylib`s, and thus reduces their size.

The plan is to stabilize this compiler flag so that Cargo can start using it on the stable channel. The stabilization plan thus has two parts:
- Stabilize the `-Cembed-metadata` flag in `rustc`
- Change Cargo to use `-Cembed-metadata=no` by default

The Cargo team has prepared their side of the [stabilization report](rust-lang/cargo#17533). You can examine it to see the whole picture.

## What is being stabilized

The `-Zembed-metadata` compiler flag.

Note that `rustc` will still default to `-Cembed-metadata=yes`, as that is its original behavior since [Rust 1.16.0](https://blog.rust-lang.org/2017/03/16/Rust-1.16/#other-improvements). But it will now be possible to pass `-Cembed-metadata=no` on stable.

## Motivation

When compiling Rust crates as libraries, Cargo uses pipelined compilation, which tells `rustc` (using `--emit=metadata`) to output an `.rmeta` file as soon as possible, so that Cargo can start follow-up `rustc` instances sooner and speed up the compilation process. This file contains the Rust-specific metadata of the compiled crate.

However, after the compilation of the Rust crate finishes, the compiler will also write an `.rlib` file to disk, which contains both the compiled object code, but also the same metadata that is already contained in the `.rmeta` file.

This means that after the compilation finishes, the metadata of each (non-leaf) compiled crate will be duplicated on disk. The metadata can be quite large, and it is not uncommon that it takes hundreds of megabytes for real-world projects. This duplication thus contributes to increasing the size of the `target` directory, which large size is one of the top 3 most pressing [challenges](https://blog.rust-lang.org/2026/03/02/2025-State-Of-Rust-Survey-results/#challenges-and-wishes-about-rust) that Rust users struggle with.

The `-Cembed-metadata=no` flag removes this duplication (see [below](#What-does-the-flag-do) on how it works).

Some size benchmark results can be found in ([1][kobzol-blog], [2][cargo-blog]). It can reach up to 35% size reduction on real-world projects in `release` mode, with the effect being smaller for `dev` builds (because incremental artifacts and debuginfo start to dominate the `target` directory size).

A slight bonus is that we only write the metadata to disk once, thus resulting in less I/O. I haven't noticed any measurable effect (either positive or negative) on compile times though, at least on Linux. The main motivation is disk size, not compilation speed.

## What does the flag do

When you pass `-Cembed-metadata=no`, `rustc` will not write the full metadata of the compiled crate into the emitted `.rlib` file. Instead, it will only write a ["metadata stub"](https://github.com/rust-lang/rust/blob/79ee8dc08073a0abf93d2b494ac5c1be11d4342e/compiler/rustc_metadata/src/rmeta/encoder.rs#L2552), which only contains the most basic information (name, target tuple, SVH).

Note that this flag should usually be combined with `--emit=metadata` to write the metadata to a `.rmeta` file, otherwise no metadata would be generated at all.

When loading crate dependencies passed via `--extern`, `rustc` will read the metadata of the passed `.rlib`. If it only contains a stub, it expects that you will also pass another `--extern` flag with a path to a `.rmeta` file with the full metadata. If you don't, you will be met with the following error:

```
error: only metadata stub found for `rlib` dependency `foo`. Please provide path to the corresponding .rmeta file with full metadata
```

Note that `rustc` will still default to the original behavior of embedding metadata, so build systems that wrap `rustc` will have to explicitly opt into using `-Cembed-metadata=no`, and at that point they will also have to start passing `--extern=<...>.rmeta` flags to `rustc`. That is what Cargo currently does if you use `nightly` or if you use the unstable `-Zembed-metadata=no` *Cargo* flag.

In theory, we could relax the requirement to pass both paths to the `.rlib` and the `.rmeta` file for each direct dependency in the future, and let `rustc --extern=foo/bar.rlib` automatically attempt to load `--extern=foo/bar.rmeta` when the `.rmeta` file path is not passed explicitly.

`.rlib` and `.rmeta` files produced using `-Cembed-metadata=no` and `-Cembed-metadata=yes` can be freely combined, and a downstream `rustc` invocation that consumes artifacts produced by `=no` does not need to itself receive the `=no` flag.

## Ecosystem impact

The `-Zembed-metadata` flag has been used for the standard library that we ship since January 2026 (rust-lang#145343) and for the compiler itself and codegen backends (cg_clif and cg_gcc) since August 2026 (rust-lang#151061).

The flag has also been used by Cargo by default on the `nightly` channel since the second half of August 2026. A blog post with a call for testing can be found [here](https://blog.rust-lang.org/inside-rust/2026/08/18/reducing-target-dir-size-on-nightly/).

I'm not aware of any regressions or issues reported in relation to this, not is the Cargo team (AFAIK). There are some unresolved questions about backwards compability if Cargo starts passing `-Cembed-metadata=no` by default, but those are related strictly to Cargo, and not the compiler flag itself (because the compiler flag will remain opt-in).

## History

The issue of duplicated metadata was originally noted back in 2015 (rust-lang#23366, rust-lang#29511), back then because of the metadata being duplicated in Rust `dylib`s. Since Cargo started using pipelined compilation by default, it became more acute, because the metadata of all compiled `.rlib` crate dependencies started being duplicated in the `target` directory.

@bjorn3 proposed to add a compiler flag to split out the metadata into a separate file in rust-lang#57076. The implementation was attempted in rust-lang#93945 and later in rust-lang#120855, but there was non-trivial complexity due to Cargo integration.

In 2025, I opened an [MCP](rust-lang/compiler-team#851) with the plan to implement this flag in two stages (first in rustc, and only then in Cargo), to reduce the implementation complexity, which was accepted. The implementation in `rustc` then happened in rust-lang#137535.

Implementation history:
- rust-lang#137535 (April 2025): Introduction of `-Zembed-metadata`, reviewed by @petrochenkov
- rust-lang/cargo#15378 (May 2025): Initial integration in Cargo, reviewed by @epage
- rust-lang#145343 (January 2026): Dogfood the flag for stdlib, reviewed by @bjorn3
- rust-lang#151061 (August 2026): Dogfood the flag for rustc and codegen backends, authored by @bjorn3
- rust-lang/cargo#17267 (August 2026): Enabled the `-Cembed-metadata=no` flag by default by Cargo on the `nightly` channel

## Unresolved questions/concerns

### Command-line length limit
This is more related to the Cargo side of things, but I also wanted to mention it here, because there is a possible improvement we could do on the compiler side in the future.

As noted [above](#What-does-the-flag-do), usage of `-Cembed-metadata=no` requires the build system/user to pass two `--extern` flags for each direct dependency of a given `rustc` invocation. This can increase the size of the executed command-line arguments, which can in edge cases start running into operating system limits.

Recently, we saw something similar in action with the Cargo's new build dir layout stabilization, where Cargo suddenly started passing a lot more `-L` flags to `rustc`, which caused issues around CLI length limits and `PATH` env. var. length limits. Cargo can work around the CLI length limit by using response files (`rustc @args`), but not all downstream tools (such as `sccache`) support those fully (yet). Based on the experience with the new build dir layout, some work was done to improve this support in the ecosystem though, and to optimize the number of flags that Cargo passes to `rustc`.

If this became a problem in practice, we could teach `rustc` to start implicitly assuming `--extern=foo.rmeta` when `--extern=foo.rlib` was passed. This would reduce the number of required `--extern` flags passed to the same count as with `-Cembed-metadata=yes` (or without using that flag at all).

I think that we can always do this retroactively if needed without breaking backwards compatibility, so it does not have to block this stabilization.

### Landing this change

Given that this compiler flag is expected to have tight integration with Cargo, and Cargo already uses it on the nightly channel by default, we have to be a bit careful about how we actually land the `-Zembed-metadata` => `-Cembed-metadata` change.

Problem A: if we simply switched it from `-Z` to `-C` in `rust-lang/rust`, then nightly would break from the following day, because Cargo would be still attempting to pass the `-Z` flag, while `rustc` would already be expecting the `-C` variant of the flag.

Problem B: furthermore, bootstrap currently unconditionally passes `-Zembed-metadata=no`, both to in-tree Cargo and to beta Cargo, so that also requires some attention.

There are several options of how we could resolve this:
1. Conclude the nightly Cargo experiment, so that it will not pass `-Zembed-metadata=no` to `rustc` by default anymore. Then sync Cargo to `rust-lang/rust`, and only then merge this PR. This solves A.
2. Move Cargo from a git submodule to a (Josh) subtree, so that we can atomically change both Cargo and `rustc` to use `-C` instead of `-Z` (inside this PR), to avoid breaking nightly users. This solves A.
    - Even though I would like to do this migration anyway, I don't think that we need to tie it to this stabilization, as it can take some time before we resolve all concerns about the migration.
3. Do what 2. proposes, but without a subtree. That means that we have to:
    - Change to `-C` in Cargo
    - Change to `-C` in `rust-lang/rust`
    - Sync the Cargo change back to `rust-lang/rust` in the same day that we land the `rustc` change

    While this solves A, it might run into some staging issues in bootstrap, and it looks very complicated to pull off.
4. Teach `rustc` to accept both the `-Z` and the `-C` variant of the flag for some time. This solves both A and B, and also gives people who use `-Zembed-metadata` manually on `nightly` some breathing room to upgrade to the `-C` variant of the flag (but GitHub code search didn't find many usages of this flag, and this is normally an expected breakage for users of unstable compiler flags).

I decided to go with 4, because the integration with Cargo is tight, and modifying bootstrap back and forth to support `-Z`/no flag /`-C` would be busy work. This solves both A and B and requires little implementation effort.

## Acknowledgments

- @bjorn3 for coming up with the idea, providing a reference implementation and mentoring me to finish up the implementation
- @petrochenkov for reviewing the compiler side of things
- @epage and @weihanglo for reviewing the Cargo side of things

[kobzol-blog]: https://kobzol.github.io/rust/rustc/2025/06/02/reduce-cargo-target-dir-size-with-z-no-embed-metadata.html#cargo-integration
[cargo-blog]: https://blog.rust-lang.org/inside-rust/2026/08/18/reducing-target-dir-size-on-nightly/#how-much-does-it-help

CC @bjorn3 @petrochenkov @weihanglo @epage

r? petrochenkov

@rustbot label +needs-fcp +T-compiler
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 19, 2026
Rollup merge of #163436 - Kobzol:stabilize-embed-metadata, r=petrochenkov

Stabilize `-Zembed-metadata`

Tracking issue: #139165

This PR proposes to stabilize the `-Zembed-metadata` compiler flag, so that Cargo can start using `-Cembed-metadata=no` by default on the stable channel.

The flag can be used to avoid duplicating the metadata of Rust crates within `.rlib` and `.rmeta` files. This can reduce the size of the `target` directory anywhere from 5% to 35% on real-world projects, depending on the chosen compilation profile ([1][kobzol-blog], [2][cargo-blog]). Additionally, `-Cembed-metadata=no` also removes the metadata out of Rust `dylib`s, and thus reduces their size.

The plan is to stabilize this compiler flag so that Cargo can start using it on the stable channel. The stabilization plan thus has two parts:
- Stabilize the `-Cembed-metadata` flag in `rustc`
- Change Cargo to use `-Cembed-metadata=no` by default

The Cargo team has prepared their side of the [stabilization report](rust-lang/cargo#17533). You can examine it to see the whole picture.

## What is being stabilized

The `-Zembed-metadata` compiler flag.

Note that `rustc` will still default to `-Cembed-metadata=yes`, as that is its original behavior since [Rust 1.16.0](https://blog.rust-lang.org/2017/03/16/Rust-1.16/#other-improvements). But it will now be possible to pass `-Cembed-metadata=no` on stable.

## Motivation

When compiling Rust crates as libraries, Cargo uses pipelined compilation, which tells `rustc` (using `--emit=metadata`) to output an `.rmeta` file as soon as possible, so that Cargo can start follow-up `rustc` instances sooner and speed up the compilation process. This file contains the Rust-specific metadata of the compiled crate.

However, after the compilation of the Rust crate finishes, the compiler will also write an `.rlib` file to disk, which contains both the compiled object code, but also the same metadata that is already contained in the `.rmeta` file.

This means that after the compilation finishes, the metadata of each (non-leaf) compiled crate will be duplicated on disk. The metadata can be quite large, and it is not uncommon that it takes hundreds of megabytes for real-world projects. This duplication thus contributes to increasing the size of the `target` directory, which large size is one of the top 3 most pressing [challenges](https://blog.rust-lang.org/2026/03/02/2025-State-Of-Rust-Survey-results/#challenges-and-wishes-about-rust) that Rust users struggle with.

The `-Cembed-metadata=no` flag removes this duplication (see [below](#What-does-the-flag-do) on how it works).

Some size benchmark results can be found in ([1][kobzol-blog], [2][cargo-blog]). It can reach up to 35% size reduction on real-world projects in `release` mode, with the effect being smaller for `dev` builds (because incremental artifacts and debuginfo start to dominate the `target` directory size).

A slight bonus is that we only write the metadata to disk once, thus resulting in less I/O. I haven't noticed any measurable effect (either positive or negative) on compile times though, at least on Linux. The main motivation is disk size, not compilation speed.

## What does the flag do

When you pass `-Cembed-metadata=no`, `rustc` will not write the full metadata of the compiled crate into the emitted `.rlib` file. Instead, it will only write a ["metadata stub"](https://github.com/rust-lang/rust/blob/79ee8dc08073a0abf93d2b494ac5c1be11d4342e/compiler/rustc_metadata/src/rmeta/encoder.rs#L2552), which only contains the most basic information (name, target tuple, SVH).

Note that this flag should usually be combined with `--emit=metadata` to write the metadata to a `.rmeta` file, otherwise no metadata would be generated at all.

When loading crate dependencies passed via `--extern`, `rustc` will read the metadata of the passed `.rlib`. If it only contains a stub, it expects that you will also pass another `--extern` flag with a path to a `.rmeta` file with the full metadata. If you don't, you will be met with the following error:

```
error: only metadata stub found for `rlib` dependency `foo`. Please provide path to the corresponding .rmeta file with full metadata
```

Note that `rustc` will still default to the original behavior of embedding metadata, so build systems that wrap `rustc` will have to explicitly opt into using `-Cembed-metadata=no`, and at that point they will also have to start passing `--extern=<...>.rmeta` flags to `rustc`. That is what Cargo currently does if you use `nightly` or if you use the unstable `-Zembed-metadata=no` *Cargo* flag.

In theory, we could relax the requirement to pass both paths to the `.rlib` and the `.rmeta` file for each direct dependency in the future, and let `rustc --extern=foo/bar.rlib` automatically attempt to load `--extern=foo/bar.rmeta` when the `.rmeta` file path is not passed explicitly.

`.rlib` and `.rmeta` files produced using `-Cembed-metadata=no` and `-Cembed-metadata=yes` can be freely combined, and a downstream `rustc` invocation that consumes artifacts produced by `=no` does not need to itself receive the `=no` flag.

## Ecosystem impact

The `-Zembed-metadata` flag has been used for the standard library that we ship since January 2026 (#145343) and for the compiler itself and codegen backends (cg_clif and cg_gcc) since August 2026 (#151061).

The flag has also been used by Cargo by default on the `nightly` channel since the second half of August 2026. A blog post with a call for testing can be found [here](https://blog.rust-lang.org/inside-rust/2026/08/18/reducing-target-dir-size-on-nightly/).

I'm not aware of any regressions or issues reported in relation to this, not is the Cargo team (AFAIK). There are some unresolved questions about backwards compability if Cargo starts passing `-Cembed-metadata=no` by default, but those are related strictly to Cargo, and not the compiler flag itself (because the compiler flag will remain opt-in).

## History

The issue of duplicated metadata was originally noted back in 2015 (#23366, #29511), back then because of the metadata being duplicated in Rust `dylib`s. Since Cargo started using pipelined compilation by default, it became more acute, because the metadata of all compiled `.rlib` crate dependencies started being duplicated in the `target` directory.

@bjorn3 proposed to add a compiler flag to split out the metadata into a separate file in #57076. The implementation was attempted in #93945 and later in #120855, but there was non-trivial complexity due to Cargo integration.

In 2025, I opened an [MCP](rust-lang/compiler-team#851) with the plan to implement this flag in two stages (first in rustc, and only then in Cargo), to reduce the implementation complexity, which was accepted. The implementation in `rustc` then happened in #137535.

Implementation history:
- #137535 (April 2025): Introduction of `-Zembed-metadata`, reviewed by @petrochenkov
- rust-lang/cargo#15378 (May 2025): Initial integration in Cargo, reviewed by @epage
- #145343 (January 2026): Dogfood the flag for stdlib, reviewed by @bjorn3
- #151061 (August 2026): Dogfood the flag for rustc and codegen backends, authored by @bjorn3
- rust-lang/cargo#17267 (August 2026): Enabled the `-Cembed-metadata=no` flag by default by Cargo on the `nightly` channel

## Unresolved questions/concerns

### Command-line length limit
This is more related to the Cargo side of things, but I also wanted to mention it here, because there is a possible improvement we could do on the compiler side in the future.

As noted [above](#What-does-the-flag-do), usage of `-Cembed-metadata=no` requires the build system/user to pass two `--extern` flags for each direct dependency of a given `rustc` invocation. This can increase the size of the executed command-line arguments, which can in edge cases start running into operating system limits.

Recently, we saw something similar in action with the Cargo's new build dir layout stabilization, where Cargo suddenly started passing a lot more `-L` flags to `rustc`, which caused issues around CLI length limits and `PATH` env. var. length limits. Cargo can work around the CLI length limit by using response files (`rustc @args`), but not all downstream tools (such as `sccache`) support those fully (yet). Based on the experience with the new build dir layout, some work was done to improve this support in the ecosystem though, and to optimize the number of flags that Cargo passes to `rustc`.

If this became a problem in practice, we could teach `rustc` to start implicitly assuming `--extern=foo.rmeta` when `--extern=foo.rlib` was passed. This would reduce the number of required `--extern` flags passed to the same count as with `-Cembed-metadata=yes` (or without using that flag at all).

I think that we can always do this retroactively if needed without breaking backwards compatibility, so it does not have to block this stabilization.

### Landing this change

Given that this compiler flag is expected to have tight integration with Cargo, and Cargo already uses it on the nightly channel by default, we have to be a bit careful about how we actually land the `-Zembed-metadata` => `-Cembed-metadata` change.

Problem A: if we simply switched it from `-Z` to `-C` in `rust-lang/rust`, then nightly would break from the following day, because Cargo would be still attempting to pass the `-Z` flag, while `rustc` would already be expecting the `-C` variant of the flag.

Problem B: furthermore, bootstrap currently unconditionally passes `-Zembed-metadata=no`, both to in-tree Cargo and to beta Cargo, so that also requires some attention.

There are several options of how we could resolve this:
1. Conclude the nightly Cargo experiment, so that it will not pass `-Zembed-metadata=no` to `rustc` by default anymore. Then sync Cargo to `rust-lang/rust`, and only then merge this PR. This solves A.
2. Move Cargo from a git submodule to a (Josh) subtree, so that we can atomically change both Cargo and `rustc` to use `-C` instead of `-Z` (inside this PR), to avoid breaking nightly users. This solves A.
    - Even though I would like to do this migration anyway, I don't think that we need to tie it to this stabilization, as it can take some time before we resolve all concerns about the migration.
3. Do what 2. proposes, but without a subtree. That means that we have to:
    - Change to `-C` in Cargo
    - Change to `-C` in `rust-lang/rust`
    - Sync the Cargo change back to `rust-lang/rust` in the same day that we land the `rustc` change

    While this solves A, it might run into some staging issues in bootstrap, and it looks very complicated to pull off.
4. Teach `rustc` to accept both the `-Z` and the `-C` variant of the flag for some time. This solves both A and B, and also gives people who use `-Zembed-metadata` manually on `nightly` some breathing room to upgrade to the `-C` variant of the flag (but GitHub code search didn't find many usages of this flag, and this is normally an expected breakage for users of unstable compiler flags).

I decided to go with 4, because the integration with Cargo is tight, and modifying bootstrap back and forth to support `-Z`/no flag /`-C` would be busy work. This solves both A and B and requires little implementation effort.

## Acknowledgments

- @bjorn3 for coming up with the idea, providing a reference implementation and mentoring me to finish up the implementation
- @petrochenkov for reviewing the compiler side of things
- @epage and @weihanglo for reviewing the Cargo side of things

[kobzol-blog]: https://kobzol.github.io/rust/rustc/2025/06/02/reduce-cargo-target-dir-size-with-z-no-embed-metadata.html#cargo-integration
[cargo-blog]: https://blog.rust-lang.org/inside-rust/2026/08/18/reducing-target-dir-size-on-nightly/#how-much-does-it-help

CC @bjorn3 @petrochenkov @weihanglo @epage

r? petrochenkov

@rustbot label +needs-fcp +T-compiler
@weihanglo

Copy link
Copy Markdown
Member Author

Some questions I have that don't seem to be addressed in the stabilization report

* How do our stability guarantees fit into this?

* If rlibs (and dylibs?) have no guarantees, should we clarify that first by no longer uplifting them?

From #t-cargo > Usage of -Zno-embed-metadata @ 💬:

During today's Cargo team meeting, we were discussing whether we should stop lifting rlib as final artifacts first, so that splitting metadata then is no longer considered. Our conclusion is yes we should do it first before stabilization -Zembed-metadata on Cargo side, and to minimize the scope, we'd like to focus on rlib use cases first.

The plan we discussed was roughly like this:

  1. We stop lifting rlib for nightly only now as a fast track, and be ready to revert aggressively if there are real use cases, and discuss then and perhaps un-revert.
  2. After next nightly cutoff we do an FCP for stabilizing the "stop-uplifting" behavior.
  3. We would perhaps do another blog post about why the benefits of this (so that we can do more aggressive rlib/metadata experiment, like also some check/build reuse cases)
  4. Once we stop uplifting rlibs, we are free to split rlib and metadata.

What do people think?

@rustbot

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Oct 22, 2026
nazzeDe pushed a commit to nazzeDe/cargo that referenced this pull request Nov 30, 2026
### What does this PR try to stabilize?

The step 2 of
<rust-lang#17278 (comment)>

This stabilizes the behavior currently on nightly: stop uplifting rlib.

In user-facing words, `rlib` is no longer a final artifacts appearing in
target directory (like `target/debug/libfoo.rlib`). It is now a
intermediate artifacts. The way to access it is via
[`--message-format=json`](https://doc.rust-lang.org/nightly/cargo/reference/external-tools.html#artifact-messages)
artifact message to get its file path. This also applies to the unstable
[`--artifact-dir`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dir)
flag.

#### Rationale

An uplifted rlib is not usable on its own. Both its metadata and object
code need the rlibs of all its transitive dependencies, which Cargo has
never uplifted. Anyone consuming rlibs correctly today already reads
JSON messages to find dependency rlibs.

On the other hand, there are other effort wanting to evolve rlib/rmeta
contents. Communicating it clearer that rlib is an intermediate artifact
would help those works without each change being a potential breaking
change.
(See
<rust-lang#17278 (comment)>
for those works/goals/efforts)

#### Out of scope

* dylibs uplifting change — they have real final-artifact use cases like
`dlopen`, though their embedded rmeta has the same issue rlib has; we
would like to discuss it separately
* The stabilization of `-Cembed-metadata=no` (rust-lang#17533).
This would unblock that

#### Data and feedback

* Rlib uplifting has been disabled on nightly since nightly-2026-10-24
(rust-lang#17621). So far no reports on rust-lang/cargo or
rust-lang/rust, and no cross-references to either PR.
* We have earlier nightly signal that we have defaulted to
`-Zembed-metadata=no` since 2026-08-19 (rust-lan g/cargo#17267).
Uplifted rlibs on nightly have not been usable via `--extern` alone
since then.
* Calls for input seems postivie so far. [#t-cargo/build-integration],
[#t-compiler], [Mastodon], and [#t-cargo]. The concerns raised have been
answered and fixed with JSON messages earlier.

#### Risks

* Stable users differ from nightly users — it is always the case 🤷🏾‍♂️
* No stable opt-out[^1] — We have no plan for a stable escape hatch. If
significant breakage shows up during beta, we revert and discuss, and
un-revert as fast as we could easy revert.

---

🤖 LLM involvement: Helped fix tests

[^1]: Technically we still have a temporary opt-out
`__CARGO_TEMPORARY_STOP_RLIB_UPLIFT_OPT_OUT` just in case but there is
no plan it stays forever.

[#t-cargo/build-integration]:
https://rust-lang.zulipchat.com/#narrow/channel/334885-t-cargo.2Fbuild-integration/topic/Any.20users.20of.20rlibs.20as.20a.20final.20build.20artifact.3F/near/613293424
[#t-compiler]:
https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Anyone.20know.20of.20people.20using.20rlibs.20generated.20by.20.60cargo.20build.60/near/613294173
[Mastodon]: https://hachyderm.io/@epage/116999642073706513
[#t-cargo]:
https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Usage.20of.20-Zno-embed-metadata/near/633481654
@rustbot rustbot added A-rebuild-detection Area: rebuild detection and fingerprinting A-unstable Area: nightly unstable support labels Dec 1, 2026
@weihanglo weihanglo changed the title feat: stabilize -Cmetadata=no as default behavior feat: stabilize -Cmetadata=no as rlib default behavior Dec 1, 2026
@weihanglo weihanglo added the relnotes Release-note worthy label Dec 1, 2026
@weihanglo
weihanglo marked this pull request as ready for review December 1, 2026 13:52
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 1, 2026
@rustbot

rustbot commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @Muscraft, @epage, @weihanglo
  • @Muscraft, @epage, @weihanglo expanded to Muscraft, epage, weihanglo
  • Random selection from Muscraft, epage

@weihanglo weihanglo changed the title feat: stabilize -Cmetadata=no as rlib default behavior feat: stabilize -Cembed-metadata=no as rlib default behavior Dec 1, 2026
@weihanglo weihanglo added the T-cargo Team: Cargo label Dec 1, 2026
@rust-lang rust-lang deleted a comment from rust-rfcbot Dec 1, 2026
@weihanglo

weihanglo commented Dec 1, 2026 •

Copy link
Copy Markdown
Member Author

@rfcbot fcp merge cargo

This is targeting 1.103, and the PR is written as if 1.101 has got released.
One major change is that we only pass -Cembed-metadata=no for rlib
since we have stopped uplifting rlib.
dylib handling is deferred.

See the PR description for more.

@rust-rfcbot

rust-rfcbot commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

@weihanglo has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Dec 1, 2026
@weihanglo weihanglo moved this to FCP merge in Cargo status tracker Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-cfg-expr Area: Platform cfg expressions A-documenting-cargo-itself Area: Cargo's documentation A-rebuild-detection Area: rebuild detection and fingerprinting A-unstable Area: nightly unstable support disposition-merge FCP with intent to merge proposed-final-comment-period An FCP proposal has started, but not yet signed off. relnotes Release-note worthy S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Team: Cargo

Projects

Status: FCP merge

Development

Successfully merging this pull request may close these issues.

4 participants