From 7cc784a0a46c33ae008b77fbfe190e6fdb527cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 14 Aug 2026 13:31:55 +0200 Subject: [PATCH] Enable `-Zembed-metadata=no` by default on the nightly (and dev) channel --- src/compiler/build_context/target_info.rs | 31 ++++++++++++++++--- .../build_runner/compilation_files.rs | 3 +- src/compiler/fingerprint/mod.rs | 3 +- src/compiler/mod.rs | 13 ++++++-- src/context/mod.rs | 4 --- src/ops/cargo_clean.rs | 4 +-- tests/testsuite/build.rs | 2 +- 7 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/compiler/build_context/target_info.rs b/src/compiler/build_context/target_info.rs index de4b197a07f..150950311a9 100644 --- a/src/compiler/build_context/target_info.rs +++ b/src/compiler/build_context/target_info.rs @@ -60,6 +60,9 @@ pub struct TargetInfo { pub rustflags: Rc<[String]>, /// Extra flags to pass to `rustdoc`, see [`extra_args`]. pub rustdocflags: Rc<[String]>, + /// Should metadata be embedded in .rlib files? + /// Corresponds to `-Zembed-metadata`. + pub should_embed_metadata: bool, } /// Kind of each file generated by a Unit, part of `FileType`. @@ -344,6 +347,21 @@ impl TargetInfo { } } + let should_embed_metadata = match gctx.cli_unstable().embed_metadata { + Some(v) => v, + None => { + let cargo_nightly = matches!( + crate::version().release_channel.as_deref(), + Some("nightly" | "dev") + ); + let rustc_nightly = matches!(rustc.version.pre.as_str(), "dev" | "nightly"); + + // Enable -Zembed-metadata=no by default if both cargo and rustc are nightly + let is_nightly = cargo_nightly && rustc_nightly; + !is_nightly + } + }; + return Ok(TargetInfo { crate_type_process, crate_types: RefCell::new(map), @@ -361,6 +379,7 @@ impl TargetInfo { cfg, supports_std, support_split_debuginfo, + should_embed_metadata, }); } } @@ -573,10 +592,9 @@ impl TargetInfo { mode: CompileMode, target_kind: &TargetKind, target_triple: &str, - gctx: &GlobalContext, ) -> CargoResult<(Vec, Vec)> { match mode { - CompileMode::Build => self.calc_rustc_outputs(target_kind, target_triple, gctx), + CompileMode::Build => self.calc_rustc_outputs(target_kind, target_triple), CompileMode::Test => { match self.file_types(&CrateType::Bin, FileFlavor::Normal, target_triple)? { Some(fts) => Ok((fts, Vec::new())), @@ -597,7 +615,6 @@ impl TargetInfo { &self, target_kind: &TargetKind, target_triple: &str, - gctx: &GlobalContext, ) -> CargoResult<(Vec, Vec)> { let mut unsupported = Vec::new(); let mut result = Vec::new(); @@ -619,7 +636,7 @@ impl TargetInfo { } } if !result.is_empty() { - if !gctx.should_embed_metadata() + if !self.should_embed_metadata() && crate_types .iter() .any(|ct| ct.benefits_from_no_embed_metadata()) @@ -650,6 +667,12 @@ impl TargetInfo { pub fn maybe_support_std(&self) -> bool { matches!(self.supports_std, Some(true) | None) } + + /// Should crate metadata be embedded into .rlib files? + /// If not, they will only be stored in .rmeta files. + pub fn should_embed_metadata(&self) -> bool { + self.should_embed_metadata + } } /// Takes rustc output (using specialized command line args), and calculates the file prefix and diff --git a/src/compiler/build_runner/compilation_files.rs b/src/compiler/build_runner/compilation_files.rs index 8a6be2cdf9f..bbe597400bd 100644 --- a/src/compiler/build_runner/compilation_files.rs +++ b/src/compiler/build_runner/compilation_files.rs @@ -420,7 +420,6 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> { CompileMode::Build, &TargetKind::Bin, bcx.target_data.short_name(&kind), - bcx.gctx, ) .expect("target must support `bin`"); @@ -664,7 +663,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> { let info = bcx.target_data.info(unit.kind); let triple = bcx.target_data.short_name(&unit.kind); let (file_types, unsupported) = - info.rustc_outputs(unit.mode, unit.target.kind(), triple, bcx.gctx)?; + info.rustc_outputs(unit.mode, unit.target.kind(), triple)?; if file_types.is_empty() { if !unsupported.is_empty() { let unsupported_strs: Vec<_> = unsupported.iter().map(|ct| ct.as_str()).collect(); diff --git a/src/compiler/fingerprint/mod.rs b/src/compiler/fingerprint/mod.rs index 172165dca40..67c4992b35f 100644 --- a/src/compiler/fingerprint/mod.rs +++ b/src/compiler/fingerprint/mod.rs @@ -1675,7 +1675,8 @@ fn calculate_normal( // rustc to link to deps using `--extern`. If it changes, we should rebuild everything. build_runner .bcx - .gctx + .target_data + .info(unit.kind) .should_embed_metadata() .not() .hash(&mut config); diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index d7fa56e1c46..5ef40dbb64f 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -1320,7 +1320,12 @@ fn build_base_args( if unit.mode.is_check() { cmd.arg("--emit=dep-info,metadata"); - } else if !build_runner.bcx.gctx.should_embed_metadata() { + } else if !build_runner + .bcx + .target_data + .info(unit.kind) + .should_embed_metadata() + { // Nightly rustc supports the -Zembed-metadata=no flag, which tells it to avoid including // full metadata in rlib/dylib artifacts, to save space on disk. In this case, metadata // will only be stored in .rmeta files. @@ -1814,7 +1819,11 @@ pub fn extern_args( let mut result = Vec::new(); let deps = build_runner.unit_deps(unit); - let no_embed_metadata = !build_runner.bcx.gctx.should_embed_metadata(); + let no_embed_metadata = !build_runner + .bcx + .target_data + .info(unit.kind) + .should_embed_metadata(); let public_dependency_enabled = is_public_dependency_enabled(build_runner, unit); // Closure to add one dependency to `result`. diff --git a/src/context/mod.rs b/src/context/mod.rs index b743ea47168..9fb69c013f9 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1269,10 +1269,6 @@ impl GlobalContext { self.extra_verbose } - pub fn should_embed_metadata(&self) -> bool { - self.cli_unstable().embed_metadata.unwrap_or(true) - } - pub fn network_allowed(&self) -> bool { !self.offline_flag().is_some() } diff --git a/src/ops/cargo_clean.rs b/src/ops/cargo_clean.rs index fa3830775db..99623710089 100644 --- a/src/ops/cargo_clean.rs +++ b/src/ops/cargo_clean.rs @@ -300,7 +300,7 @@ fn clean_specs( let triple = target_data.short_name(compile_kind); let (file_types, _unsupported) = target_data .info(*compile_kind) - .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?; + .rustc_outputs(mode, target.kind(), triple)?; let artifact_dir = layout .artifact_dir() .expect("artifact-dir was not locked during clean"); @@ -377,7 +377,7 @@ fn clean_specs( let triple = target_data.short_name(compile_kind); let (file_types, _unsupported) = target_data .info(*compile_kind) - .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?; + .rustc_outputs(mode, target.kind(), triple)?; let artifact_dir = layout .artifact_dir() .expect("artifact-dir was not locked during clean"); diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 4f6ad78d869..75850397804 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -6549,7 +6549,7 @@ fn embed_metadata_no_invalidate() { "#]]) .run(); - p.cargo("build") + p.cargo("build -Z embed-metadata=yes") .masquerade_as_nightly_cargo(&["-Z embed-metadata"]) .with_stderr_data(str![[r#" [COMPILING] bar v0.5.0 ([ROOT]/foo/bar)