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

Packages can inherit fields from their root workspace #9684

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,19 @@ pub fn basic_lib_manifest(name: &str) -> String {
)
}

pub fn basic_workspace_manifest(name: &str, workspace: &str) -> String {
format!(
r#"
[package]
name = "{}"
version = "0.1.0"
authors = []
workspace = "{}"
"#,
name, workspace
)
}

pub fn path2url<P: AsRef<Path>>(p: P) -> Url {
Url::from_file_path(p).ok().unwrap()
}
Expand Down
10 changes: 3 additions & 7 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,9 @@ pub fn resolve_std<'cfg>(
String::from("library/alloc"),
String::from("library/test"),
];
let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
&src_path,
&Some(members),
/*default_members*/ &None,
/*exclude*/ &None,
/*custom_metadata*/ &None,
));
let ws_config = crate::core::WorkspaceConfig::Root(
crate::core::WorkspaceRootConfig::from_members(&src_path, members),
);
let virtual_manifest = crate::core::VirtualManifest::new(
/*replace*/ Vec::new(),
patch,
Expand Down
74 changes: 72 additions & 2 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ use url::Url;

use crate::core::compiler::{CompileKind, CrateType};
use crate::core::resolver::ResolveBehavior;
use crate::core::{Dependency, PackageId, PackageIdSpec, SourceId, Summary};
use crate::core::{
Dependency, InheritableFields, Package, PackageId, PackageIdSpec, SourceId, Summary,
};
use crate::core::{Edition, Feature, Features, WorkspaceConfig};
use crate::util::errors::*;
use crate::util::interning::InternedString;
use crate::util::toml::{TomlManifest, TomlProfiles};
use crate::util::{short_hash, Config, Filesystem};

pub enum EitherManifest {
Real(Manifest),
Real(IntermediateManifest),
Virtual(VirtualManifest),
}

impl EitherManifest {
pub(crate) fn workspace_config(&self) -> &WorkspaceConfig {
match *self {
EitherManifest::Real(ref im) => im.workspace_config(),
EitherManifest::Virtual(ref v) => v.workspace_config(),
}
}
}

/// Contains all the information about a package, as loaded from a `Cargo.toml`.
///
/// This is deserialized using the [`TomlManifest`] type.
Expand Down Expand Up @@ -980,3 +991,62 @@ impl Warnings {
&self.0
}
}

/// This type is used to deserialize `Cargo.toml` files.
#[derive(Debug, Clone)]
pub struct IntermediateManifest {
Copy link
Member

Choose a reason for hiding this comment

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

I think that this structure may want to be defined in the toml parsing area, otherwise it's pretty back-and-forth where it's created here but all the guts of processing happen later with TomlManifest::to_real_manifest.

Also, would it be possible for TomlManifest to itself be an "intermediate" manifest?

workspace: WorkspaceConfig,
source_id: SourceId,
original: Rc<TomlManifest>,
warnings: Warnings,
}

impl IntermediateManifest {
pub fn new(
workspace: WorkspaceConfig,
source_id: SourceId,
original: Rc<TomlManifest>,
) -> IntermediateManifest {
IntermediateManifest {
workspace,
source_id,
original,
warnings: Warnings::new(),
}
}

pub fn to_package(
&self,
manifest_path: &Path,
config: &Config,
inheritable_fields: Option<&InheritableFields>,
) -> CargoResult<(Package, Vec<PathBuf>)> {
let (mut manifest, nested_paths) = TomlManifest::to_real_manifest(
&self.original,
self.source_id,
manifest_path.parent().unwrap(),
config,
inheritable_fields,
)
.with_context(|| format!("failed to parse manifest at `{}`", manifest_path.display()))
.map_err(|err| ManifestError::new(err, manifest_path.into()))?;

for warning in self.warnings.warnings() {
manifest.warnings_mut().add_warning(warning.message.clone());
}

Ok((Package::new(manifest, manifest_path), nested_paths))
}

pub fn original(&self) -> &Rc<TomlManifest> {
&self.original
}

pub fn warnings_mut(&mut self) -> &mut Warnings {
&mut self.warnings
}

pub fn workspace_config(&self) -> &WorkspaceConfig {
&self.workspace
}
}
5 changes: 4 additions & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ pub use self::resolver::{Resolve, ResolveVersion};
pub use self::shell::{Shell, Verbosity};
pub use self::source::{GitReference, Source, SourceId, SourceMap};
pub use self::summary::{FeatureMap, FeatureValue, Summary};
pub use self::workspace::{MaybePackage, Workspace, WorkspaceConfig, WorkspaceRootConfig};
pub use self::workspace::{
find_workspace_root, InheritableFields, MaybePackage, Workspace, WorkspaceConfig,
WorkspaceRootConfig,
};

pub mod compiler;
pub mod dependency;
Expand Down
Loading