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 #8664

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 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 @@ -1609,6 +1609,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
13 changes: 5 additions & 8 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::util::errors::CargoResult;
use std::collections::{HashMap, HashSet};
use std::env;
use std::path::PathBuf;
use std::rc::Rc;

/// Parse the `-Zbuild-std` flag.
pub fn parse_unstable_flag(value: Option<&str>) -> Vec<String> {
Expand Down Expand Up @@ -60,17 +61,13 @@ 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,
ws_config,
Rc::new(ws_config),
/*profiles*/ None,
crate::core::Features::default(),
None,
Expand Down
33 changes: 21 additions & 12 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ use crate::core::{Dependency, 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::toml::{DefinedTomlManifest, TomlProfiles};
use crate::util::{short_hash, Config, Filesystem};

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

impl EitherManifest {
pub fn workspace(&self) -> Rc<WorkspaceConfig> {
Copy link
Member

Choose a reason for hiding this comment

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

FWIW I see a number of methods here returning a bare Rc<T>, but if possible I think it would be best to return &Rc<T> since it defers the clone to the point at which it's actually necessary.

Copy link
Member

Choose a reason for hiding this comment

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

This isn't really a perf concern since the reference counting is almost nothing in Cargo, mostly just a stylistic thing.

match self {
Self::Real(manifest) => Rc::clone(&manifest.workspace),
Self::Virtual(virtual_manifest) => Rc::clone(&virtual_manifest.workspace),
}
}
}

/// Contains all the information about a package, as loaded from a `Cargo.toml`.
#[derive(Clone, Debug)]
pub struct Manifest {
Expand All @@ -40,8 +49,8 @@ pub struct Manifest {
publish_lockfile: bool,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
original: Rc<TomlManifest>,
workspace: Rc<WorkspaceConfig>,
original: Rc<DefinedTomlManifest>,
features: Features,
edition: Edition,
im_a_teapot: Option<bool>,
Expand All @@ -66,7 +75,7 @@ pub struct Warnings(Vec<DelayedWarning>);
pub struct VirtualManifest {
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
workspace: Rc<WorkspaceConfig>,
profiles: Option<TomlProfiles>,
warnings: Warnings,
features: Features,
Expand Down Expand Up @@ -370,12 +379,12 @@ impl Manifest {
publish_lockfile: bool,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
workspace: Rc<WorkspaceConfig>,
features: Features,
edition: Edition,
im_a_teapot: Option<bool>,
default_run: Option<String>,
original: Rc<TomlManifest>,
original: Rc<DefinedTomlManifest>,
metabuild: Option<Vec<String>>,
resolve_behavior: Option<ResolveBehavior>,
) -> Manifest {
Expand Down Expand Up @@ -453,7 +462,7 @@ impl Manifest {
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
&self.replace
}
pub fn original(&self) -> &TomlManifest {
pub fn original(&self) -> &DefinedTomlManifest {
&self.original
}
pub fn patch(&self) -> &HashMap<Url, Vec<Dependency>> {
Expand All @@ -463,8 +472,8 @@ impl Manifest {
self.links.as_deref()
}

pub fn workspace_config(&self) -> &WorkspaceConfig {
&self.workspace
pub fn workspace_config(&self) -> Rc<WorkspaceConfig> {
Rc::clone(&self.workspace)
}

pub fn features(&self) -> &Features {
Expand Down Expand Up @@ -538,7 +547,7 @@ impl VirtualManifest {
pub fn new(
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
workspace: Rc<WorkspaceConfig>,
profiles: Option<TomlProfiles>,
features: Features,
resolve_behavior: Option<ResolveBehavior>,
Expand All @@ -562,8 +571,8 @@ impl VirtualManifest {
&self.patch
}

pub fn workspace_config(&self) -> &WorkspaceConfig {
&self.workspace
pub fn workspace_config(&self) -> Rc<WorkspaceConfig> {
Rc::clone(&self.workspace)
}

pub fn profiles(&self) -> Option<&TomlProfiles> {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pub mod resolver;
pub mod shell;
pub mod source;
pub mod summary;
mod workspace;
pub mod workspace;
4 changes: 3 additions & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ impl Package {
let manifest = self
.manifest()
.original()
.prepare_for_publish(ws, self.root())?;
.prepare_for_publish(ws, self.manifest_path())?
.into_toml_manifest();

let toml = toml::to_string(&manifest)?;
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
}
Expand Down
Loading