diff --git a/src/cargo/util/toml/embedded.rs b/src/cargo/util/toml/embedded.rs index 2090791cd..d36480ea0 100644 --- a/src/cargo/util/toml/embedded.rs +++ b/src/cargo/util/toml/embedded.rs @@ -6,8 +6,6 @@ use crate::util::restricted_names; use crate::CargoResult; use crate::GlobalContext; -const DEFAULT_EDITION: crate::core::features::Edition = - crate::core::features::Edition::LATEST_STABLE; const AUTO_FIELDS: &[&str] = &[ "autolib", "autobins", @@ -64,24 +62,20 @@ pub(super) fn expand_manifest( } cargo_util::paths::write_if_changed(&hacked_path, hacked_source)?; - let manifest = expand_manifest_(&frontmatter, &hacked_path, gctx) + let manifest = expand_manifest_(&frontmatter, &hacked_path) .with_context(|| format!("failed to parse manifest at `{}`", path.display()))?; let manifest = toml::to_string_pretty(&manifest)?; Ok(manifest) } else { let frontmatter = ""; - let manifest = expand_manifest_(frontmatter, path, gctx) + let manifest = expand_manifest_(frontmatter, path) .with_context(|| format!("failed to parse manifest at `{}`", path.display()))?; let manifest = toml::to_string_pretty(&manifest)?; Ok(manifest) } } -fn expand_manifest_( - manifest: &str, - path: &std::path::Path, - gctx: &GlobalContext, -) -> CargoResult { +fn expand_manifest_(manifest: &str, path: &std::path::Path) -> CargoResult { let mut manifest: toml::Table = toml::from_str(&manifest)?; for key in ["workspace", "lib", "bin", "example", "test", "bench"] { @@ -117,13 +111,6 @@ fn expand_manifest_( package .entry("name".to_owned()) .or_insert(toml::Value::String(name)); - package.entry("edition".to_owned()).or_insert_with(|| { - let _ = gctx.shell().warn(format_args!( - "`package.edition` is unspecified, defaulting to `{}`", - DEFAULT_EDITION - )); - toml::Value::String(DEFAULT_EDITION.to_string()) - }); let mut bin = toml::Table::new(); bin.insert("name".to_owned(), toml::Value::String(bin_name)); @@ -558,7 +545,6 @@ name = "test-" path = "/home/me/test.rs" [package] -edition = "2024" name = "test-" [workspace] @@ -587,7 +573,6 @@ path = [..] time = "0.1.25" [package] -edition = "2024" name = "test-" [workspace] diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 688d8c184..8491e2ae7 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -321,7 +321,7 @@ fn normalize_toml( if let Some(original_package) = original_toml.package() { let normalized_package = - normalize_package_toml(original_package, manifest_file, is_embedded, &inherit)?; + normalize_package_toml(original_package, manifest_file, is_embedded, gctx, &inherit)?; let package_name = &normalized_package.name.clone(); let edition = normalized_package .normalized_edition() @@ -550,6 +550,7 @@ fn normalize_package_toml<'a>( original_package: &manifest::TomlPackage, manifest_file: &Path, is_embedded: bool, + gctx: &GlobalContext, inherit: &dyn Fn() -> CargoResult<&'a InheritableFields>, ) -> CargoResult> { let package_root = manifest_file.parent().unwrap(); @@ -559,7 +560,22 @@ fn normalize_package_toml<'a>( .clone() .map(|value| field_inherit_with(value, "edition", || inherit()?.edition())) .transpose()? - .map(manifest::InheritableField::Value); + .map(manifest::InheritableField::Value) + .or_else(|| { + if is_embedded { + const DEFAULT_EDITION: crate::core::features::Edition = + crate::core::features::Edition::LATEST_STABLE; + let _ = gctx.shell().warn(format_args!( + "`package.edition` is unspecified, defaulting to `{}`", + DEFAULT_EDITION + )); + Some(manifest::InheritableField::Value( + DEFAULT_EDITION.to_string(), + )) + } else { + None + } + }); let rust_version = original_package .rust_version .clone()