diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 0deda834bbf..64c8dec2bbf 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -42,6 +42,8 @@ impl EitherManifest { #[derive(Clone, Debug)] pub struct Manifest { // alternate forms of manifests: + contents: Rc, + document: Rc>, resolved_toml: Rc, summary: Summary, @@ -389,6 +391,8 @@ compact_debug! { impl Manifest { pub fn new( + contents: Rc, + document: Rc>, resolved_toml: Rc, summary: Summary, @@ -416,6 +420,8 @@ impl Manifest { embedded: bool, ) -> Manifest { Manifest { + contents, + document, resolved_toml, summary, @@ -445,6 +451,25 @@ impl Manifest { } } + /// The raw contents of the original TOML + pub fn contents(&self) -> &str { + self.contents.as_str() + } + /// Collection of spans for the original TOML + pub fn document(&self) -> &toml_edit::ImDocument { + &self.document + } + /// The [`TomlManifest`] with all fields expanded + pub fn resolved_toml(&self) -> &TomlManifest { + &self.resolved_toml + } + pub fn summary(&self) -> &Summary { + &self.summary + } + pub fn summary_mut(&mut self) -> &mut Summary { + &mut self.summary + } + pub fn dependencies(&self) -> &[Dependency] { self.summary.dependencies() } @@ -469,12 +494,6 @@ impl Manifest { pub fn package_id(&self) -> PackageId { self.summary.package_id() } - pub fn summary(&self) -> &Summary { - &self.summary - } - pub fn summary_mut(&mut self) -> &mut Summary { - &mut self.summary - } pub fn targets(&self) -> &[Target] { &self.targets } @@ -500,9 +519,6 @@ impl Manifest { pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] { &self.replace } - pub fn resolved_toml(&self) -> &TomlManifest { - &self.resolved_toml - } pub fn patch(&self) -> &HashMap> { &self.patch } diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 1bc8f4309f0..e2ff5a45f89 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -453,10 +453,19 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult { let orig_resolve = ops::load_pkg_lockfile(ws)?; // Convert Package -> TomlManifest -> Manifest -> Package + let contents = orig_pkg.manifest().contents(); + let document = orig_pkg.manifest().document(); let toml_manifest = prepare_for_publish(orig_pkg.manifest().resolved_toml(), ws, orig_pkg.root())?; let source_id = orig_pkg.package_id().source_id(); - let manifest = to_real_manifest(toml_manifest, source_id, orig_pkg.manifest_path(), gctx)?; + let manifest = to_real_manifest( + contents.to_owned(), + document.clone(), + toml_manifest, + source_id, + orig_pkg.manifest_path(), + gctx, + )?; let new_pkg = Package::new(manifest, orig_pkg.manifest_path()); // Regenerate Cargo.lock using the old one as a guide. diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 9873e9f2a53..0aba18e2555 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -57,7 +57,8 @@ pub fn read_manifest( (|| { if toml.package().is_some() { - to_real_manifest(toml, source_id, path, gctx).map(EitherManifest::Real) + to_real_manifest(contents, document, toml, source_id, path, gctx) + .map(EitherManifest::Real) } else { to_virtual_manifest(toml, source_id, path, gctx).map(EitherManifest::Virtual) } @@ -443,6 +444,8 @@ pub fn prepare_for_publish( #[tracing::instrument(skip_all)] pub fn to_real_manifest( + contents: String, + document: toml_edit::ImDocument, me: manifest::TomlManifest, source_id: SourceId, manifest_file: &Path, @@ -1208,6 +1211,8 @@ pub fn to_real_manifest( _unused_keys: Default::default(), }; let mut manifest = Manifest::new( + Rc::new(contents), + Rc::new(document), Rc::new(resolved_toml), summary, default_kind,