Skip to content

Commit

Permalink
Part 2 of RFC2906 - allow inheriting from parent workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Apr 4, 2022
1 parent 035cb09 commit 0cd5546
Show file tree
Hide file tree
Showing 5 changed files with 685 additions and 75 deletions.
9 changes: 9 additions & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ pub enum EitherManifest {
Virtual(VirtualManifest),
}

impl EitherManifest {
pub(crate) fn workspace_config(&self) -> &WorkspaceConfig {
match *self {
EitherManifest::Real(ref r) => r.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
3 changes: 2 additions & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ 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::{
InheritableFields, MaybePackage, Workspace, WorkspaceConfig, WorkspaceRootConfig,
find_workspace_root, InheritableFields, MaybePackage, Workspace, WorkspaceConfig,
WorkspaceRootConfig,
};

pub mod compiler;
Expand Down
62 changes: 51 additions & 11 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,16 +591,6 @@ impl<'cfg> Workspace<'cfg> {
/// Returns an error if `manifest_path` isn't actually a valid manifest or
/// if some other transient error happens.
fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
let path = member_manifest
.parent()
.unwrap()
.join(root_link)
.join("Cargo.toml");
debug!("find_root - pointer {}", path.display());
paths::normalize_path(&path)
}

{
let current = self.packages.load(manifest_path)?;
match *current.workspace_config() {
Expand Down Expand Up @@ -1643,6 +1633,10 @@ impl WorkspaceRootConfig {
.collect::<Result<Vec<_>, _>>()?;
Ok(res)
}

pub fn inheritable(&self) -> &InheritableFields {
&self.inheritable_fields
}
}

/// A group of fields that are inheritable by members of the workspace
Expand Down Expand Up @@ -1832,7 +1826,53 @@ impl InheritableFields {
}
}

pub fn find_root_iter<'a>(
fn parse_manifest(manifest_path: &Path, config: &Config) -> CargoResult<EitherManifest> {
let key = manifest_path.parent().unwrap();
let source_id = SourceId::for_path(key)?;
let (manifest, _nested_paths) = read_manifest(manifest_path, source_id, config)?;
Ok(manifest)
}

pub fn find_workspace_root(manifest_path: &Path, config: &Config) -> CargoResult<Option<PathBuf>> {
find_root_iter(manifest_path, config)
.find_map(|ances_manifest_path| {
let manifest = parse_manifest(&ances_manifest_path, config);
match manifest {
Ok(manifest) => match *manifest.workspace_config() {
WorkspaceConfig::Root(ref ances_root_config) => {
debug!("find_root - found a root checking exclusion");
if !ances_root_config.is_excluded(manifest_path) {
debug!("find_root - found!");
Some(Ok(ances_manifest_path))
} else {
None
}
}
WorkspaceConfig::Member {
root: Some(ref path_to_root),
} => {
debug!("find_root - found pointer");
Some(Ok(read_root_pointer(&ances_manifest_path, path_to_root)))
}
WorkspaceConfig::Member { .. } => None,
},
Err(e) => Some(Err(e)),
}
})
.transpose()
}

fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
let path = member_manifest
.parent()
.unwrap()
.join(root_link)
.join("Cargo.toml");
debug!("find_root - pointer {}", path.display());
paths::normalize_path(&path)
}

fn find_root_iter<'a>(
manifest_path: &'a Path,
config: &'a Config,
) -> impl Iterator<Item = PathBuf> + 'a {
Expand Down
Loading

0 comments on commit 0cd5546

Please sign in to comment.