Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(toml): Expose surce/spans for VirtualManifests #13603

Merged
merged 15 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util-schemas"
version = "0.3.0"
version = "0.3.1"
rust-version = "1.76.0" # MSRV:1
edition.workspace = true
license.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use rust_version::RustVersion;
pub use rust_version::RustVersionError;

/// This type is used to deserialize `Cargo.toml` files.
#[derive(Debug, Deserialize, Serialize)]
#[derive(Default, Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct TomlManifest {
// when adding new fields, be sure to check whether `requires_package` should disallow them
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::util::errors::CargoResult;
use crate::GlobalContext;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::rc::Rc;

use super::BuildConfig;

Expand Down Expand Up @@ -103,6 +104,10 @@ pub fn resolve_std<'gctx>(
/*custom_metadata*/ &None,
));
let virtual_manifest = crate::core::VirtualManifest::new(
Rc::default(),
Rc::new(toml_edit::ImDocument::parse("".to_owned()).expect("empty is valid TOML")),
Rc::default(),
Rc::default(),
/*replace*/ Vec::new(),
patch,
ws_config,
Expand Down
32 changes: 32 additions & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ pub struct Warnings(Vec<DelayedWarning>);

#[derive(Clone, Debug)]
pub struct VirtualManifest {
// alternate forms of manifests:
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,

// this form of manifest:
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
Expand Down Expand Up @@ -629,6 +636,10 @@ impl Manifest {

impl VirtualManifest {
pub fn new(
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
Expand All @@ -637,6 +648,10 @@ impl VirtualManifest {
resolve_behavior: Option<ResolveBehavior>,
) -> VirtualManifest {
VirtualManifest {
contents,
document,
original_toml,
resolved_toml,
replace,
patch,
workspace,
Expand All @@ -647,6 +662,23 @@ impl VirtualManifest {
}
}

/// 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<String> {
&self.document
}
/// The [`TomlManifest`] with all fields expanded
pub fn original_toml(&self) -> &TomlManifest {
&self.original_toml
}
/// The [`TomlManifest`] with all fields expanded
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
}

pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
&self.replace
}
Expand Down
10 changes: 9 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ pub fn read_manifest(
to_real_manifest(contents, document, original_toml, source_id, path, gctx)
.map(EitherManifest::Real)
} else {
to_virtual_manifest(original_toml, source_id, path, gctx).map(EitherManifest::Virtual)
to_virtual_manifest(contents, document, original_toml, source_id, path, gctx)
.map(EitherManifest::Virtual)
}
})()
.map_err(|err| {
Expand Down Expand Up @@ -1276,6 +1277,8 @@ fn load_inheritable_fields(
}

fn to_virtual_manifest(
contents: String,
document: toml_edit::ImDocument<String>,
original_toml: manifest::TomlManifest,
source_id: SourceId,
manifest_file: &Path,
Expand Down Expand Up @@ -1362,7 +1365,12 @@ fn to_virtual_manifest(
bail!("virtual manifests must be configured with [workspace]");
}
};
let resolved_toml = original_toml.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This additional clone seems unfortunate, though I expect it won't really hurt the performance in normal cases, as it's for virtual manifests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do a small normalization later on. Over time, I will be doing more.

let mut manifest = VirtualManifest::new(
Rc::new(contents),
Rc::new(document),
Rc::new(original_toml),
Rc::new(resolved_toml),
replace,
patch,
workspace_config,
Expand Down