Skip to content

Commit 43c1913

Browse files
committed
Moved manifest metadata tracking from fingerprint to dep info
1 parent 652623b commit 43c1913

File tree

7 files changed

+255
-149
lines changed

7 files changed

+255
-149
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Type definitions for the result of a compilation.
22
3+
use std::borrow::Cow;
34
use std::collections::{BTreeSet, HashMap};
45
use std::ffi::{OsStr, OsString};
56
use std::path::PathBuf;
@@ -351,54 +352,21 @@ impl<'gctx> Compilation<'gctx> {
351352
}
352353
}
353354

354-
let metadata = pkg.manifest().metadata();
355-
356355
let cargo_exe = self.gctx.cargo_exe()?;
357356
cmd.env(crate::CARGO_ENV, cargo_exe);
358357

359358
// When adding new environment variables depending on
360359
// crate properties which might require rebuild upon change
361360
// consider adding the corresponding properties to the hash
362361
// in BuildContext::target_metadata()
363-
let rust_version = pkg.rust_version().as_ref().map(ToString::to_string);
364-
cmd.env("CARGO_MANIFEST_DIR", pkg.root())
365-
.env("CARGO_MANIFEST_PATH", pkg.manifest_path())
366-
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
367-
.env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string())
368-
.env("CARGO_PKG_VERSION_PATCH", &pkg.version().patch.to_string())
369-
.env("CARGO_PKG_VERSION_PRE", pkg.version().pre.as_str())
370-
.env("CARGO_PKG_VERSION", &pkg.version().to_string())
371-
.env("CARGO_PKG_NAME", &*pkg.name())
372-
.env(
373-
"CARGO_PKG_DESCRIPTION",
374-
metadata.description.as_ref().unwrap_or(&String::new()),
375-
)
376-
.env(
377-
"CARGO_PKG_HOMEPAGE",
378-
metadata.homepage.as_ref().unwrap_or(&String::new()),
379-
)
380-
.env(
381-
"CARGO_PKG_REPOSITORY",
382-
metadata.repository.as_ref().unwrap_or(&String::new()),
383-
)
384-
.env(
385-
"CARGO_PKG_LICENSE",
386-
metadata.license.as_ref().unwrap_or(&String::new()),
387-
)
388-
.env(
389-
"CARGO_PKG_LICENSE_FILE",
390-
metadata.license_file.as_ref().unwrap_or(&String::new()),
391-
)
392-
.env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
393-
.env(
394-
"CARGO_PKG_RUST_VERSION",
395-
&rust_version.as_deref().unwrap_or_default(),
396-
)
397-
.env(
398-
"CARGO_PKG_README",
399-
metadata.readme.as_ref().unwrap_or(&String::new()),
400-
)
401-
.cwd(pkg.root());
362+
for (key, value) in pkg.emitted_env_vars() {
363+
match value {
364+
Cow::Borrowed(value) => cmd.env(key, value),
365+
Cow::Owned(ref value) => cmd.env(key, value.as_str()),
366+
};
367+
}
368+
369+
cmd.cwd(pkg.root());
402370

403371
apply_env_config(self.gctx, &mut cmd)?;
404372

src/cargo/core/compiler/fingerprint/dep_info.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use cargo_util::paths;
1919
use cargo_util::ProcessBuilder;
2020
use cargo_util::Sha256;
2121

22+
use crate::core::package::EmittablePackageMetadata;
2223
use crate::CargoResult;
2324
use crate::CARGO_ENV;
2425

@@ -334,7 +335,10 @@ pub fn translate_dep_info(
334335
//
335336
// For cargo#13280, We trace env vars that are defined in the `[env]` config table.
336337
on_disk_info.env.retain(|(key, _)| {
337-
env_config.contains_key(key) || !rustc_cmd.get_envs().contains_key(key) || key == CARGO_ENV
338+
EmittablePackageMetadata::is_emittable_env_var(key)
339+
|| env_config.contains_key(key)
340+
|| !rustc_cmd.get_envs().contains_key(key)
341+
|| key == CARGO_ENV
338342
});
339343

340344
let serialize_path = |file| {

src/cargo/core/compiler/fingerprint/dirty_reason.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub enum DirtyReason {
2626
old: Vec<String>,
2727
new: Vec<String>,
2828
},
29-
MetadataChanged,
3029
ConfigSettingsChanged,
3130
CompileKindChanged,
3231
LocalLengthsChanged,
@@ -168,7 +167,6 @@ impl DirtyReason {
168167
s.dirty_because(unit, "the profile configuration changed")
169168
}
170169
DirtyReason::RustflagsChanged { .. } => s.dirty_because(unit, "the rustflags changed"),
171-
DirtyReason::MetadataChanged => s.dirty_because(unit, "the metadata changed"),
172170
DirtyReason::ConfigSettingsChanged => {
173171
s.dirty_because(unit, "the config settings changed")
174172
}

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
//! [`CompileKind`] (host/target) | ✓ | ✓ | ✓ | ✓
7979
//! `__CARGO_DEFAULT_LIB_METADATA`[^4] | | ✓ | ✓ | ✓
8080
//! `package_id` | | ✓ | ✓ | ✓
81-
//! authors, description, homepage, repo | ✓ | | |
8281
//! Target src path relative to ws | ✓ | | |
8382
//! Target flags (test/bench/for_host/edition) | ✓ | | |
8483
//! -C incremental=… flag | ✓ | | |
@@ -189,6 +188,8 @@
189188
//! files to learn about environment variables that the rustc compile depends on.
190189
//! Cargo then later uses this to trigger a recompile if a referenced env var
191190
//! changes (even if the source didn't change).
191+
//! This also includes env vars generated from Cargo metadata like `CARGO_PKG_DESCRIPTION`.
192+
//! (See [`EmittablePackageMetadata`])
192193
//!
193194
//! #### dep-info files for build system integration.
194195
//!
@@ -371,13 +372,15 @@
371372
mod dep_info;
372373
mod dirty_reason;
373374

375+
use std::borrow::Cow;
374376
use std::collections::hash_map::{Entry, HashMap};
375377
use std::env;
376378
use std::fs;
377379
use std::fs::File;
378380
use std::hash::{self, Hash, Hasher};
379381
use std::io::{self};
380382
use std::path::{Path, PathBuf};
383+
use std::str::FromStr;
381384
use std::sync::{Arc, Mutex};
382385
use std::time::SystemTime;
383386

@@ -391,6 +394,7 @@ use serde::{Deserialize, Serialize};
391394
use tracing::{debug, info};
392395

393396
use crate::core::compiler::unit_graph::UnitDep;
397+
use crate::core::package::EmittablePackageMetadata;
394398
use crate::core::Package;
395399
use crate::util;
396400
use crate::util::errors::CargoResult;
@@ -612,10 +616,6 @@ pub struct Fingerprint {
612616
memoized_hash: Mutex<Option<u64>>,
613617
/// RUSTFLAGS/RUSTDOCFLAGS environment variable value (or config value).
614618
rustflags: Vec<String>,
615-
/// Hash of some metadata from the manifest, such as "authors", or
616-
/// "description", which are exposed as environment variables during
617-
/// compilation.
618-
metadata: u64,
619619
/// Hash of various config settings that change how things are compiled.
620620
config: u64,
621621
/// The rustc target. This is only relevant for `.json` files, otherwise
@@ -831,11 +831,12 @@ impl LocalFingerprint {
831831
&self,
832832
mtime_cache: &mut HashMap<PathBuf, FileTime>,
833833
checksum_cache: &mut HashMap<PathBuf, Checksum>,
834-
pkg_root: &Path,
834+
pkg: &Package,
835835
target_root: &Path,
836836
cargo_exe: &Path,
837837
gctx: &GlobalContext,
838838
) -> CargoResult<Option<StaleItem>> {
839+
let pkg_root = pkg.root();
839840
match self {
840841
// We need to parse `dep_info`, learn about the crate's dependencies.
841842
//
@@ -849,6 +850,18 @@ impl LocalFingerprint {
849850
return Ok(Some(StaleItem::MissingFile(dep_info)));
850851
};
851852
for (key, previous) in info.env.iter() {
853+
if let Ok(t) = EmittablePackageMetadata::from_str(key.as_str()) {
854+
let value = pkg.emitted_env_var(&t);
855+
let value = match value {
856+
Cow::Borrowed(v) => v,
857+
Cow::Owned(ref v) => v.as_str(),
858+
};
859+
860+
if Some(value) == previous.as_deref() {
861+
continue;
862+
}
863+
}
864+
852865
let current = if key == CARGO_ENV {
853866
Some(cargo_exe.to_str().ok_or_else(|| {
854867
format_err!(
@@ -932,7 +945,6 @@ impl Fingerprint {
932945
local: Mutex::new(Vec::new()),
933946
memoized_hash: Mutex::new(None),
934947
rustflags: Vec::new(),
935-
metadata: 0,
936948
config: 0,
937949
compile_kind: 0,
938950
fs_status: FsStatus::Stale,
@@ -995,9 +1007,6 @@ impl Fingerprint {
9951007
new: self.rustflags.clone(),
9961008
};
9971009
}
998-
if self.metadata != old.metadata {
999-
return DirtyReason::MetadataChanged;
1000-
}
10011010
if self.config != old.config {
10021011
return DirtyReason::ConfigSettingsChanged;
10031012
}
@@ -1142,13 +1151,14 @@ impl Fingerprint {
11421151
&mut self,
11431152
mtime_cache: &mut HashMap<PathBuf, FileTime>,
11441153
checksum_cache: &mut HashMap<PathBuf, Checksum>,
1145-
pkg_root: &Path,
1154+
pkg: &Package,
11461155
target_root: &Path,
11471156
cargo_exe: &Path,
11481157
gctx: &GlobalContext,
11491158
) -> CargoResult<()> {
11501159
assert!(!self.fs_status.up_to_date());
11511160

1161+
let pkg_root = pkg.root();
11521162
let mut mtimes = HashMap::new();
11531163

11541164
// Get the `mtime` of all outputs. Optionally update their mtime
@@ -1249,7 +1259,7 @@ impl Fingerprint {
12491259
if let Some(item) = local.find_stale_item(
12501260
mtime_cache,
12511261
checksum_cache,
1252-
pkg_root,
1262+
pkg,
12531263
target_root,
12541264
cargo_exe,
12551265
gctx,
@@ -1279,7 +1289,6 @@ impl hash::Hash for Fingerprint {
12791289
profile,
12801290
ref deps,
12811291
ref local,
1282-
metadata,
12831292
config,
12841293
compile_kind,
12851294
ref rustflags,
@@ -1294,7 +1303,6 @@ impl hash::Hash for Fingerprint {
12941303
path,
12951304
profile,
12961305
&*local,
1297-
metadata,
12981306
config,
12991307
compile_kind,
13001308
rustflags,
@@ -1445,7 +1453,7 @@ fn calculate(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
14451453
fingerprint.check_filesystem(
14461454
&mut build_runner.mtime_cache,
14471455
&mut build_runner.checksum_cache,
1448-
unit.pkg.root(),
1456+
&unit.pkg,
14491457
&target_root,
14501458
cargo_exe,
14511459
build_runner.bcx.gctx,
@@ -1529,9 +1537,6 @@ fn calculate_normal(
15291537
build_runner.lto[unit],
15301538
unit.pkg.manifest().lint_rustflags(),
15311539
));
1532-
// Include metadata since it is exposed as environment variables.
1533-
let m = unit.pkg.manifest().metadata();
1534-
let metadata = util::hash_u64((&m.authors, &m.description, &m.homepage, &m.repository));
15351540
let mut config = StableHasher::new();
15361541
if let Some(linker) = build_runner.compilation.target_linker(unit.kind) {
15371542
linker.hash(&mut config);
@@ -1560,7 +1565,6 @@ fn calculate_normal(
15601565
deps,
15611566
local: Mutex::new(local),
15621567
memoized_hash: Mutex::new(None),
1563-
metadata,
15641568
config: Hasher::finish(&config),
15651569
compile_kind,
15661570
rustflags: extra_flags,

0 commit comments

Comments
 (0)