Skip to content

Commit

Permalink
Merge pull request #203 from zombiepigdragon/fix_root_package
Browse files Browse the repository at this point in the history
Add fallback to root_package when no_deps is set
  • Loading branch information
oli-obk committed Sep 6, 2022
2 parents 12c913f + 725f8d9 commit c0e2194
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/lib.rs
Expand Up @@ -155,10 +155,22 @@ pub struct Metadata {
}

impl Metadata {
/// Get the root package of this metadata instance.
/// Get the workspace's root package of this metadata instance.
pub fn root_package(&self) -> Option<&Package> {
let root = self.resolve.as_ref()?.root.as_ref()?;
self.packages.iter().find(|pkg| &pkg.id == root)
match &self.resolve {
Some(resolve) => {
// if dependencies are resolved, use Cargo's answer
let root = resolve.root.as_ref()?;
self.packages.iter().find(|pkg| &pkg.id == root)
}
None => {
// if dependencies aren't resolved, check for a root package manually
let root_manifest_path = self.workspace_root.join("Cargo.toml");
self.packages
.iter()
.find(|pkg| pkg.manifest_path == root_manifest_path)
}
}
}

/// Get the workspace packages.
Expand Down Expand Up @@ -533,7 +545,7 @@ pub struct MetadataCommand {
manifest_path: Option<PathBuf>,
/// Current directory of the `cargo metadata` process.
current_dir: Option<PathBuf>,
/// Output information only about the root package and don't fetch dependencies.
/// Output information only about workspace members and don't fetch dependencies.
no_deps: bool,
/// Collections of `CargoOpt::SomeFeatures(..)`
features: Vec<String>,
Expand Down Expand Up @@ -569,7 +581,7 @@ impl MetadataCommand {
self.current_dir = Some(path.into());
self
}
/// Output information only about the root package and don't fetch dependencies.
/// Output information only about workspace members and don't fetch dependencies.
pub fn no_deps(&mut self) -> &mut MetadataCommand {
self.no_deps = true;
self
Expand Down
1 change: 1 addition & 0 deletions tests/all/Cargo.toml
@@ -1,3 +1,4 @@
# Used in tests/test_samples.rs
[package]
name = "all"
version = "0.1.0"
Expand Down
8 changes: 8 additions & 0 deletions tests/basic_workspace/Cargo.toml
@@ -0,0 +1,8 @@
[package]
name = "ex_bin"
version = "0.1.0"

[dependencies]
ex_lib = { version = "0.1.0", path = "./ex_lib" }

[workspace]
7 changes: 7 additions & 0 deletions tests/basic_workspace/ex_lib/Cargo.toml
@@ -0,0 +1,7 @@
[package]
name = "ex_lib"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
1 change: 1 addition & 0 deletions tests/basic_workspace/ex_lib/src/lib.rs
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/basic_workspace/src/main.rs
@@ -0,0 +1 @@
fn main() {}
22 changes: 22 additions & 0 deletions tests/test_samples.rs
Expand Up @@ -623,3 +623,25 @@ fn depkind_to_string() {
assert_eq!(DependencyKind::Build.to_string(), "build");
assert_eq!(DependencyKind::Unknown.to_string(), "Unknown");
}

#[test]
fn basic_workspace_root_package_exists() {
// First try with dependencies
let meta = MetadataCommand::new()
.manifest_path("tests/basic_workspace/Cargo.toml")
.exec()
.unwrap();
assert_eq!(meta.root_package().unwrap().name, "ex_bin");
// Now with no_deps, it should still work exactly the same
let meta = MetadataCommand::new()
.manifest_path("tests/basic_workspace/Cargo.toml")
.no_deps()
.exec()
.unwrap();
assert_eq!(
meta.root_package()
.expect("workspace root still exists when no_deps used")
.name,
"ex_bin"
);
}

0 comments on commit c0e2194

Please sign in to comment.