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

Detect cargo add foo will inherit a workspace dependency #3

Merged
merged 2 commits into from
Apr 26, 2022
Merged
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
58 changes: 31 additions & 27 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use dependency::RegistrySource;
use dependency::Source;
use manifest::LocalManifest;

use crate::ops::cargo_add::dependency::MaybeWorkspace;
use crate::ops::cargo_add::dependency::{MaybeWorkspace, WorkspaceSource};
pub use manifest::DepTable;

/// Information on what dependencies should be added
Expand Down Expand Up @@ -281,7 +281,11 @@ fn resolve_dependency(
};

if dependency.source().is_none() {
if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
// Checking for a workspace dependency happens first since a member could be specified
// in the workspace dependencies table as a dependency
if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() {
dependency = dependency.set_source(WorkspaceSource::new());
} else if let Some(package) = ws.members().find(|p| p.name().as_str() == dependency.name) {
// Only special-case workspaces when the user doesn't provide any extra
// information, otherwise, trust the user.
let mut src = PathSource::new(package.root());
Expand Down Expand Up @@ -543,31 +547,7 @@ fn populate_available_features(
let query = dependency.query(config)?;
let query = match query {
MaybeWorkspace::Workspace(_workspace) => {
let manifest = LocalManifest::try_new(ws.root_manifest())?;
let manifest = manifest
.data
.as_item()
.as_table_like()
.context("could not make `manifest.data` into a table")?;
let workspace = manifest
.get("workspace")
.context("could not find `workspace`")?
.as_table_like()
.context("could not make `manifest.data.workspace` into a table")?;
let dependencies = workspace
.get("dependencies")
.context("could not find `dependencies` table in `workspace`")?
.as_table_like()
.context("could not make `dependencies` into a table")?;
let dep_item = dependencies.get(dependency.toml_key()).context(format!(
"could not find {} in `workspace.dependencies`",
dependency.toml_key()
))?;
let dep = Dependency::from_toml(
ws.root_manifest().parent().unwrap(),
dependency.toml_key(),
dep_item,
)?;
let dep = find_workspace_dep(dependency.toml_key(), ws.root_manifest())?;
if let Some(features) = dep.features.clone() {
dependency = dependency.set_inherited_features(features);
}
Expand Down Expand Up @@ -712,3 +692,27 @@ fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {

true
}

fn find_workspace_dep(toml_key: &str, root_manifest: &Path) -> CargoResult<Dependency> {
let manifest = LocalManifest::try_new(root_manifest)?;
let manifest = manifest
.data
.as_item()
.as_table_like()
.context("could not make `manifest.data` into a table")?;
let workspace = manifest
.get("workspace")
.context("could not find `workspace`")?
.as_table_like()
.context("could not make `manifest.data.workspace` into a table")?;
let dependencies = workspace
.get("dependencies")
.context("could not find `dependencies` table in `workspace`")?
.as_table_like()
.context("could not make `dependencies` into a table")?;
let dep_item = dependencies.get(toml_key).context(format!(
"could not find {} in `workspace.dependencies`",
toml_key
))?;
Dependency::from_toml(root_manifest.parent().unwrap(), toml_key, dep_item)
}
5 changes: 5 additions & 0 deletions tests/snapshots/add/detect_workspace_inherit.in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.0.0"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"
Empty file.
5 changes: 5 additions & 0 deletions tests/snapshots/add/detect_workspace_inherit.out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo.workspace = true
1 change: 1 addition & 0 deletions tests/snapshots/add/detect_workspace_inherit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding foo (workspace) to dependencies.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency", features = ["merge"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "foo"
version = "0.0.0"

[features]
default-base = []
default-test-base = []
default-merge-base = []
default = ["default-base", "default-test-base", "default-merge-base"]
test-base = []
test = ["test-base", "default-test-base"]
merge-base = []
merge = ["merge-base", "default-merge-base"]
unrelated = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency", features = ["merge"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "foo"
version = "0.0.0"

[features]
default-base = []
default-test-base = []
default-merge-base = []
default = ["default-base", "default-test-base", "default-merge-base"]
test-base = []
test = ["test-base", "default-test-base"]
merge-base = []
merge = ["merge-base", "default-merge-base"]
unrelated = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo = { workspace = true, features = ["test"] }
10 changes: 10 additions & 0 deletions tests/snapshots/add/detect_workspace_inherit_features.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Adding foo (workspace) to dependencies.
Features:
+ default-base
+ default-merge-base
+ default-test-base
+ merge
+ merge-base
+ test
+ test-base
- unrelated
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["primary", "dependency"]

[workspace.dependencies]
foo = { version = "0.0.0", path = "./dependency"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["workspace-inheritance"]

[package]
name = "bar"
version = "0.0.0"

[dependencies]
foo = { workspace = true, optional = true }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding foo (workspace) to optional dependencies.
Empty file.
71 changes: 71 additions & 0 deletions tests/testsuite/cargo_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,77 @@ fn require_weak() {
assert().subset_matches("tests/snapshots/add/require_weak.out", &project_root);
}

#[cargo_test]
fn detect_workspace_inherit() {
init_registry();
let project = Project::from_template("tests/snapshots/add/detect_workspace_inherit.in");
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "-p", "bar"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/detect_workspace_inherit.stdout")
.stderr_matches_path("tests/snapshots/add/detect_workspace_inherit.stderr");

assert().subset_matches(
"tests/snapshots/add/detect_workspace_inherit.out",
&project_root,
);
}

#[cargo_test]
fn detect_workspace_inherit_features() {
init_registry();
let project =
Project::from_template("tests/snapshots/add/detect_workspace_inherit_features.in");
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "-p", "bar", "--features", "test"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/detect_workspace_inherit_features.stdout")
.stderr_matches_path("tests/snapshots/add/detect_workspace_inherit_features.stderr");

assert().subset_matches(
"tests/snapshots/add/detect_workspace_inherit_features.out",
&project_root,
);
}

#[cargo_test]
fn detect_workspace_inherit_optional() {
init_registry();
let project =
Project::from_template("tests/snapshots/add/detect_workspace_inherit_optional.in");
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "-p", "bar", "--optional"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path("tests/snapshots/add/detect_workspace_inherit_optional.stdout")
.stderr_matches_path("tests/snapshots/add/detect_workspace_inherit_optional.stderr");

assert().subset_matches(
"tests/snapshots/add/detect_workspace_inherit_optional.out",
&project_root,
);
}

#[cargo_test]
fn dev() {
init_registry();
Expand Down