From 0f5b562e620ee7ecaef724b2ab40bc0db84c3e5b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 1 Apr 2024 16:56:52 -0500 Subject: [PATCH] refactor(toml): Centralize cargo-features parsing --- src/cargo/util/toml/mod.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 178a285b8..b1b0e4853 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -59,11 +59,16 @@ pub fn read_manifest( .map_err(|e| emit_diagnostic(e.into(), &contents, path, gctx))?; let mut manifest = (|| { + let empty = Vec::new(); + let cargo_features = original_toml.cargo_features.as_ref().unwrap_or(&empty); + let features = Features::new(cargo_features, gctx, &mut warnings, source_id.is_path())?; + if original_toml.package().is_some() { to_real_manifest( contents, document, original_toml, + features, source_id, path, gctx, @@ -76,6 +81,7 @@ pub fn read_manifest( contents, document, original_toml, + features, source_id, path, gctx, @@ -222,6 +228,7 @@ pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult) -> CargoResult, original_toml: manifest::TomlManifest, + features: Features, source_id: SourceId, manifest_file: &Path, gctx: &GlobalContext, @@ -501,11 +510,6 @@ pub fn to_real_manifest( ); }; - // Parse features first so they will be available when parsing other parts of the TOML. - let empty = Vec::new(); - let cargo_features = original_toml.cargo_features.as_ref().unwrap_or(&empty); - let features = Features::new(cargo_features, gctx, warnings, source_id.is_path())?; - let original_package = match (&original_toml.package, &original_toml.project) { (Some(_), Some(project)) => { warnings.push(format!( @@ -1462,6 +1466,7 @@ fn to_virtual_manifest( contents: String, document: toml_edit::ImDocument, original_toml: manifest::TomlManifest, + features: Features, source_id: SourceId, manifest_file: &Path, gctx: &GlobalContext, @@ -1476,13 +1481,9 @@ fn to_virtual_manifest( bail!("this virtual manifest specifies a `{field}` section, which is not allowed"); } - let mut deps = Vec::new(); - let empty = Vec::new(); - let cargo_features = original_toml.cargo_features.as_ref().unwrap_or(&empty); - let features = Features::new(cargo_features, gctx, warnings, source_id.is_path())?; - resolved_toml._unused_keys = Default::default(); + let mut deps = Vec::new(); let (replace, patch) = { let mut manifest_ctx = ManifestContext { deps: &mut deps,