Skip to content

Commit 0cd5546

Browse files
committed
Part 2 of RFC2906 - allow inheriting from parent workspaces
1 parent 035cb09 commit 0cd5546

File tree

5 files changed

+685
-75
lines changed

5 files changed

+685
-75
lines changed

src/cargo/core/manifest.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ pub enum EitherManifest {
2626
Virtual(VirtualManifest),
2727
}
2828

29+
impl EitherManifest {
30+
pub(crate) fn workspace_config(&self) -> &WorkspaceConfig {
31+
match *self {
32+
EitherManifest::Real(ref r) => r.workspace_config(),
33+
EitherManifest::Virtual(ref v) => v.workspace_config(),
34+
}
35+
}
36+
}
37+
2938
/// Contains all the information about a package, as loaded from a `Cargo.toml`.
3039
///
3140
/// This is deserialized using the [`TomlManifest`] type.

src/cargo/core/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ pub use self::shell::{Shell, Verbosity};
1111
pub use self::source::{GitReference, Source, SourceId, SourceMap};
1212
pub use self::summary::{FeatureMap, FeatureValue, Summary};
1313
pub use self::workspace::{
14-
InheritableFields, MaybePackage, Workspace, WorkspaceConfig, WorkspaceRootConfig,
14+
find_workspace_root, InheritableFields, MaybePackage, Workspace, WorkspaceConfig,
15+
WorkspaceRootConfig,
1516
};
1617

1718
pub mod compiler;

src/cargo/core/workspace.rs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -591,16 +591,6 @@ impl<'cfg> Workspace<'cfg> {
591591
/// Returns an error if `manifest_path` isn't actually a valid manifest or
592592
/// if some other transient error happens.
593593
fn find_root(&mut self, manifest_path: &Path) -> CargoResult<Option<PathBuf>> {
594-
fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
595-
let path = member_manifest
596-
.parent()
597-
.unwrap()
598-
.join(root_link)
599-
.join("Cargo.toml");
600-
debug!("find_root - pointer {}", path.display());
601-
paths::normalize_path(&path)
602-
}
603-
604594
{
605595
let current = self.packages.load(manifest_path)?;
606596
match *current.workspace_config() {
@@ -1643,6 +1633,10 @@ impl WorkspaceRootConfig {
16431633
.collect::<Result<Vec<_>, _>>()?;
16441634
Ok(res)
16451635
}
1636+
1637+
pub fn inheritable(&self) -> &InheritableFields {
1638+
&self.inheritable_fields
1639+
}
16461640
}
16471641

16481642
/// A group of fields that are inheritable by members of the workspace
@@ -1832,7 +1826,53 @@ impl InheritableFields {
18321826
}
18331827
}
18341828

1835-
pub fn find_root_iter<'a>(
1829+
fn parse_manifest(manifest_path: &Path, config: &Config) -> CargoResult<EitherManifest> {
1830+
let key = manifest_path.parent().unwrap();
1831+
let source_id = SourceId::for_path(key)?;
1832+
let (manifest, _nested_paths) = read_manifest(manifest_path, source_id, config)?;
1833+
Ok(manifest)
1834+
}
1835+
1836+
pub fn find_workspace_root(manifest_path: &Path, config: &Config) -> CargoResult<Option<PathBuf>> {
1837+
find_root_iter(manifest_path, config)
1838+
.find_map(|ances_manifest_path| {
1839+
let manifest = parse_manifest(&ances_manifest_path, config);
1840+
match manifest {
1841+
Ok(manifest) => match *manifest.workspace_config() {
1842+
WorkspaceConfig::Root(ref ances_root_config) => {
1843+
debug!("find_root - found a root checking exclusion");
1844+
if !ances_root_config.is_excluded(manifest_path) {
1845+
debug!("find_root - found!");
1846+
Some(Ok(ances_manifest_path))
1847+
} else {
1848+
None
1849+
}
1850+
}
1851+
WorkspaceConfig::Member {
1852+
root: Some(ref path_to_root),
1853+
} => {
1854+
debug!("find_root - found pointer");
1855+
Some(Ok(read_root_pointer(&ances_manifest_path, path_to_root)))
1856+
}
1857+
WorkspaceConfig::Member { .. } => None,
1858+
},
1859+
Err(e) => Some(Err(e)),
1860+
}
1861+
})
1862+
.transpose()
1863+
}
1864+
1865+
fn read_root_pointer(member_manifest: &Path, root_link: &str) -> PathBuf {
1866+
let path = member_manifest
1867+
.parent()
1868+
.unwrap()
1869+
.join(root_link)
1870+
.join("Cargo.toml");
1871+
debug!("find_root - pointer {}", path.display());
1872+
paths::normalize_path(&path)
1873+
}
1874+
1875+
fn find_root_iter<'a>(
18361876
manifest_path: &'a Path,
18371877
config: &'a Config,
18381878
) -> impl Iterator<Item = PathBuf> + 'a {

0 commit comments

Comments
 (0)