Skip to content

Commit

Permalink
Clean up workspace dependencies after cargo remove
Browse files Browse the repository at this point in the history
  • Loading branch information
cassaundra committed Oct 15, 2022
1 parent b592ba4 commit 96a8229
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
section,
dry_run,
};
remove(&options)?;
remove(&workspace, &options)?;

if !dry_run {
// Reload the workspace since we've changed dependencies
Expand Down
67 changes: 66 additions & 1 deletion src/cargo/ops/cargo_remove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Core of cargo-remove command

use crate::core::Package;
use crate::core::Workspace;
use crate::util::toml_mut::manifest::DepTable;
use crate::util::toml_mut::manifest::LocalManifest;
use crate::CargoResult;
Expand All @@ -22,7 +23,7 @@ pub struct RemoveOptions<'a> {
}

/// Remove dependencies from a manifest
pub fn remove(options: &RemoveOptions<'_>) -> CargoResult<()> {
pub fn remove(workspace: &Workspace<'_>, options: &RemoveOptions<'_>) -> CargoResult<()> {
let dep_table = options
.section
.to_table()
Expand All @@ -33,7 +34,17 @@ pub fn remove(options: &RemoveOptions<'_>) -> CargoResult<()> {
let manifest_path = options.spec.manifest_path().to_path_buf();
let mut manifest = LocalManifest::try_new(&manifest_path)?;

let mut workspace_manifest = if workspace.is_virtual() {
let data = cargo_util::paths::read(workspace.root_manifest())?;
let manifest: toml_edit::Document = data.parse()?;
Some(manifest)
} else {
None
};

for dep in &options.dependencies {
let is_workspace_dep = dependency_is_workspace(dep, &dep_table, &manifest);

let section = if dep_table.len() >= 3 {
format!("{} for target `{}`", &dep_table[2], &dep_table[1])
} else {
Expand All @@ -50,6 +61,21 @@ pub fn remove(options: &RemoveOptions<'_>) -> CargoResult<()> {
// crate, then we need to drop any explicitly activated features on
// that crate.
manifest.gc_dep(dep);

// If this was the last instance of a workspace dependency, remove it
// from the workspace dependencies.
if is_workspace_dep {
if let Some(workspace_manifest) = &mut workspace_manifest {
if !dependency_in_workspace(dep, options.spec, workspace) {
remove_dependency_from_workspace(dep, workspace_manifest);

options
.config
.shell()
.status("Removing", format!("{dep} from workspace dependencies"))?;
}
}
}
}

if options.dry_run {
Expand All @@ -59,7 +85,46 @@ pub fn remove(options: &RemoveOptions<'_>) -> CargoResult<()> {
.warn("aborting remove due to dry run")?;
} else {
manifest.write()?;

if let Some(workspace_manifest) = workspace_manifest {
cargo_util::paths::write(
workspace.root_manifest(),
workspace_manifest.to_string().as_bytes(),
)?;
}
}

Ok(())
}

/// Get whether or not a dependency is marked as `workspace`.
fn dependency_is_workspace(dep: &str, dep_table: &[String], manifest: &LocalManifest) -> bool {
if let Ok(toml_edit::Item::Table(table)) = manifest.get_table(dep_table) {
let value = table.get(dep).and_then(|i| i.get("workspace"));
if let Some(toml_edit::Item::Value(value)) = value {
return value.as_bool() == Some(true);
}
}

false
}

/// Get whether or not a dependency is depended upon in a workspace.
fn dependency_in_workspace(dep: &str, exclude: &Package, workspace: &Workspace<'_>) -> bool {
workspace.members().filter(|p| *p != exclude).any(|p| {
p.dependencies()
.iter()
.any(|d| d.package_name().as_str() == dep)
})
}

/// Remove a dependency from a virtual workspace.
fn remove_dependency_from_workspace(dep: &str, virtual_manifest: &mut toml_edit::Document) {
if let Some(toml_edit::Item::Table(table)) = virtual_manifest
.get_mut("workspace")
.and_then(|t| t.get_mut("dependencies"))
{
table.set_implicit(true);
table.remove(dep);
}
}
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod target;
mod target_build;
mod target_dev;
mod update_lock_file;
mod workspace;

fn init_registry() {
cargo_test_support::registry::init();
Expand Down
5 changes: 5 additions & 0 deletions tests/testsuite/cargo_remove/workspace/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = [ "my-package" ]

[workspace.dependencies]
semver = "0.1.0"
24 changes: 24 additions & 0 deletions tests/testsuite/cargo_remove/workspace/in/my-package/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "my-package"
version = "0.1.0"

[[bin]]
name = "main"
path = "src/main.rs"

[build-dependencies]
semver = { workspace = true }

[dependencies]
docopt = "0.6"
rustc-serialize = "0.4"
semver = "0.1"
toml = "0.1"
clippy = "0.4"

[dev-dependencies]
regex = "0.1.1"
serde = "1.0.90"

[features]
std = ["serde/std", "semver/std"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

25 changes: 25 additions & 0 deletions tests/testsuite/cargo_remove/workspace/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::curr_dir;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;

use crate::cargo_remove::init_registry;

#[cargo_test]
fn case() {
init_registry();
let project = Project::from_template(curr_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("remove")
.args(["--package", "my-package", "--build", "semver"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
}
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_remove/workspace/out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = [ "my-package" ]
21 changes: 21 additions & 0 deletions tests/testsuite/cargo_remove/workspace/out/my-package/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "my-package"
version = "0.1.0"

[[bin]]
name = "main"
path = "src/main.rs"

[dependencies]
docopt = "0.6"
rustc-serialize = "0.4"
semver = "0.1"
toml = "0.1"
clippy = "0.4"

[dev-dependencies]
regex = "0.1.1"
serde = "1.0.90"

[features]
std = ["serde/std", "semver/std"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions tests/testsuite/cargo_remove/workspace/stderr.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Removing semver from build-dependencies
Removing semver from workspace dependencies
Updating `dummy-registry` index
Empty file.

0 comments on commit 96a8229

Please sign in to comment.