Add support for adding/removing development dependencies (#4327)

## Summary

Support adding/removing dependencies from `tool.uv.dev-dependencies`
with `uv add/remove --dev`.

Part of https://github.com/astral-sh/uv/issues/3959.
This commit is contained in:
Ibraheem Ahmed 2024-06-14 15:17:29 -04:00 committed by GitHub
parent 042fdea087
commit 294f0e0c41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 392 additions and 73 deletions

View file

@ -2,7 +2,7 @@ use std::fmt;
use std::str::FromStr;
use thiserror::Error;
use toml_edit::{Array, DocumentMut, Item, RawString, TomlError, Value};
use toml_edit::{Array, DocumentMut, Item, RawString, Table, TomlError, Value};
use pep508_rs::{PackageName, Requirement};
use pypi_types::VerbatimParsedUrl;
@ -33,79 +33,102 @@ impl PyProjectTomlMut {
})
}
/// Adds a dependency.
/// Adds a dependency to `project.dependencies`.
pub fn add_dependency(&mut self, req: &Requirement) -> Result<(), Error> {
let deps = &mut self.doc["project"]["dependencies"];
if deps.is_none() {
*deps = Item::Value(Value::Array(Array::new()));
}
let deps = deps.as_array_mut().ok_or(Error::MalformedDependencies)?;
add_dependency(req, &mut self.doc["project"]["dependencies"])
}
// Try to find matching dependencies.
let mut to_replace = Vec::new();
for (i, dep) in deps.iter().enumerate() {
if dep
.as_str()
.and_then(try_parse_requirement)
.filter(|dep| dep.name == req.name)
.is_some()
{
to_replace.push(i);
}
}
/// Adds a development dependency to `tool.uv.dev-dependencies`.
pub fn add_dev_dependency(&mut self, req: &Requirement) -> Result<(), Error> {
let tool = self.doc["tool"].or_insert({
let mut tool = Table::new();
tool.set_implicit(true);
Item::Table(tool)
});
let tool_uv = tool["uv"].or_insert(Item::Table(Table::new()));
if to_replace.is_empty() {
deps.push(req.to_string());
} else {
// Replace the first occurrence of the dependency and remove the rest.
deps.replace(to_replace[0], req.to_string());
for &i in to_replace[1..].iter().rev() {
deps.remove(i);
}
}
reformat_array_multiline(deps);
Ok(())
add_dependency(req, &mut tool_uv["dev-dependencies"])
}
/// Removes all occurrences of dependencies with the given name.
pub fn remove_dependency(&mut self, req: &PackageName) -> Result<Vec<Requirement>, Error> {
let deps = &mut self.doc["project"]["dependencies"];
if deps.is_none() {
return Ok(Vec::new());
}
let deps = deps.as_array_mut().ok_or(Error::MalformedDependencies)?;
// Try to find matching dependencies.
let mut to_remove = Vec::new();
for (i, dep) in deps.iter().enumerate() {
if dep
.as_str()
.and_then(try_parse_requirement)
.filter(|dep| dep.name == *req)
.is_some()
{
to_remove.push(i);
}
}
let removed = to_remove
.into_iter()
.rev() // Reverse to preserve indices as we remove them.
.filter_map(|i| {
deps.remove(i)
.as_str()
.and_then(|req| Requirement::from_str(req).ok())
})
.collect::<Vec<_>>();
if !removed.is_empty() {
reformat_array_multiline(deps);
}
Ok(removed)
remove_dependency(req, &mut self.doc["project"]["dependencies"])
}
/// Removes all occurrences of development dependencies with the given name.
pub fn remove_dev_dependency(&mut self, req: &PackageName) -> Result<Vec<Requirement>, Error> {
let Some(tool_uv) = self.doc.get_mut("tool").and_then(|tool| tool.get_mut("uv")) else {
return Ok(Vec::new());
};
remove_dependency(req, &mut tool_uv["dev-dependencies"])
}
}
/// Adds a dependency to the given `deps` array.
pub fn add_dependency(req: &Requirement, deps: &mut Item) -> Result<(), Error> {
let deps = deps
.or_insert(Item::Value(Value::Array(Array::new())))
.as_array_mut()
.ok_or(Error::MalformedDependencies)?;
// Find matching dependencies.
let to_replace = find_dependencies(&req.name, deps);
if to_replace.is_empty() {
deps.push(req.to_string());
} else {
// Replace the first occurrence of the dependency and remove the rest.
deps.replace(to_replace[0], req.to_string());
for &i in to_replace[1..].iter().rev() {
deps.remove(i);
}
}
reformat_array_multiline(deps);
Ok(())
}
/// Removes all occurrences of dependencies with the given name from the given `deps` array.
fn remove_dependency(req: &PackageName, deps: &mut Item) -> Result<Vec<Requirement>, Error> {
if deps.is_none() {
return Ok(Vec::new());
}
let deps = deps.as_array_mut().ok_or(Error::MalformedDependencies)?;
// Remove matching dependencies.
let removed = find_dependencies(req, deps)
.into_iter()
.rev() // Reverse to preserve indices as we remove them.
.filter_map(|i| {
deps.remove(i)
.as_str()
.and_then(|req| Requirement::from_str(req).ok())
})
.collect::<Vec<_>>();
if !removed.is_empty() {
reformat_array_multiline(deps);
}
Ok(removed)
}
// Returns a `Vec` containing the indices of all dependencies with the given name.
fn find_dependencies(name: &PackageName, deps: &Array) -> Vec<usize> {
let mut to_replace = Vec::new();
for (i, dep) in deps.iter().enumerate() {
if dep
.as_str()
.and_then(try_parse_requirement)
.filter(|dep| dep.name == *name)
.is_some()
{
to_replace.push(i);
}
}
to_replace
}
impl fmt::Display for PyProjectTomlMut {

View file

@ -1602,6 +1602,10 @@ pub(crate) struct AddArgs {
#[arg(required = true)]
pub(crate) requirements: Vec<String>,
/// Add the requirements as development dependencies.
#[arg(long)]
pub(crate) dev: bool,
#[command(flatten)]
pub(crate) installer: ResolverInstallerArgs,
@ -1634,6 +1638,10 @@ pub(crate) struct RemoveArgs {
#[arg(required = true)]
pub(crate) requirements: Vec<PackageName>,
/// Remove the requirements from development dependencies.
#[arg(long)]
pub(crate) dev: bool,
/// The Python interpreter into which packages should be installed.
///
/// By default, `uv` installs into the virtual environment in the current working directory or

View file

@ -22,6 +22,7 @@ use crate::settings::ResolverInstallerSettings;
#[allow(clippy::too_many_arguments)]
pub(crate) async fn add(
requirements: Vec<RequirementsSource>,
dev: bool,
python: Option<String>,
settings: ResolverInstallerSettings,
preview: PreviewMode,
@ -122,8 +123,12 @@ pub(crate) async fn add(
// Add the requirements to the `pyproject.toml`.
let mut pyproject = PyProjectTomlMut::from_toml(project.current_project().pyproject_toml())?;
for req in requirements {
pyproject.add_dependency(&pep508_rs::Requirement::from(req))?;
for req in requirements.into_iter().map(pep508_rs::Requirement::from) {
if dev {
pyproject.add_dev_dependency(&req)?;
} else {
pyproject.add_dependency(&req)?;
}
}
// Save the modified `pyproject.toml`.

View file

@ -16,6 +16,7 @@ use crate::settings::{InstallerSettings, ResolverSettings};
#[allow(clippy::too_many_arguments)]
pub(crate) async fn remove(
requirements: Vec<PackageName>,
dev: bool,
python: Option<String>,
preview: PreviewMode,
connectivity: Connectivity,
@ -33,12 +34,43 @@ pub(crate) async fn remove(
let mut pyproject = PyProjectTomlMut::from_toml(project.current_project().pyproject_toml())?;
for req in requirements {
if pyproject.remove_dependency(&req)?.is_empty() {
anyhow::bail!(
"The dependency `{}` could not be found in `dependencies`",
req
);
if dev {
let deps = pyproject.remove_dev_dependency(&req)?;
if deps.is_empty() {
// Check if there is a matching regular dependency.
if pyproject
.remove_dependency(&req)
.ok()
.filter(|deps| !deps.is_empty())
.is_some()
{
uv_warnings::warn_user!("`{req}` is not a development dependency; try calling `uv add` without the `--dev` flag");
}
anyhow::bail!("The dependency `{req}` could not be found in `dev-dependencies`");
}
continue;
}
let deps = pyproject.remove_dependency(&req)?;
if deps.is_empty() {
// Check if there is a matching development dependency.
if pyproject
.remove_dev_dependency(&req)
.ok()
.filter(|deps| !deps.is_empty())
.is_some()
{
uv_warnings::warn_user!(
"`{req}` is a development dependency; try calling `uv add --dev`"
);
}
anyhow::bail!("The dependency `{req}` could not be found in `dependencies`");
}
continue;
}
// Save the modified `pyproject.toml`.

View file

@ -693,6 +693,7 @@ async fn run() -> Result<ExitStatus> {
commands::add(
requirements,
args.dev,
args.python,
args.settings,
globals.preview,
@ -714,6 +715,7 @@ async fn run() -> Result<ExitStatus> {
commands::remove(
args.requirements,
args.dev,
args.python,
globals.preview,
globals.connectivity,

View file

@ -364,6 +364,7 @@ impl LockSettings {
#[derive(Debug, Clone)]
pub(crate) struct AddSettings {
pub(crate) requirements: Vec<String>,
pub(crate) dev: bool,
pub(crate) python: Option<String>,
pub(crate) refresh: Refresh,
pub(crate) settings: ResolverInstallerSettings,
@ -375,6 +376,7 @@ impl AddSettings {
pub(crate) fn resolve(args: AddArgs, filesystem: Option<FilesystemOptions>) -> Self {
let AddArgs {
requirements,
dev,
installer,
build,
refresh,
@ -383,6 +385,7 @@ impl AddSettings {
Self {
requirements,
dev,
python,
refresh: Refresh::from(refresh),
settings: ResolverInstallerSettings::combine(
@ -398,6 +401,7 @@ impl AddSettings {
#[derive(Debug, Clone)]
pub(crate) struct RemoveSettings {
pub(crate) requirements: Vec<PackageName>,
pub(crate) dev: bool,
pub(crate) python: Option<String>,
}
@ -406,12 +410,14 @@ impl RemoveSettings {
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn resolve(args: RemoveArgs, _filesystem: Option<FilesystemOptions>) -> Self {
let RemoveArgs {
dev,
requirements,
python,
} = args;
Self {
requirements,
dev,
python,
}
}

View file

@ -374,6 +374,129 @@ fn add_unnamed() -> Result<()> {
Ok(())
}
/// Add a development dependency.
#[test]
fn add_dev() -> Result<()> {
let context = TestContext::new("3.12");
let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
"#})?;
uv_snapshot!(context.filters(), context.add(&["anyio==3.7.0"]).arg("--dev"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv add` is experimental and may change without warning.
Resolved 4 packages in [TIME]
Downloaded 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==3.7.0
+ idna==3.7
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);
let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
[tool.uv]
dev-dependencies = [
"anyio==3.7.0",
]
"###
);
});
// `uv add` implies a full lock and sync, including development dependencies.
let lock = fs_err::read_to_string(context.temp_dir.join("uv.lock"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"
[[distribution]]
name = "anyio"
version = "3.7.0"
source = "registry+https://pypi.org/simple"
sdist = { url = "https://files.pythonhosted.org/packages/c6/b3/fefbf7e78ab3b805dec67d698dc18dd505af7a18a8dd08868c9b4fa736b5/anyio-3.7.0.tar.gz", hash = "sha256:275d9973793619a5374e1c89a4f4ad3f4b0a5510a2b5b939444bee8f4c4d37ce", size = 142737 }
wheels = [{ url = "https://files.pythonhosted.org/packages/68/fe/7ce1926952c8a403b35029e194555558514b365ad77d75125f521a2bec62/anyio-3.7.0-py3-none-any.whl", hash = "sha256:eddca883c4175f14df8aedce21054bfca3adb70ffe76a9f607aef9d7fa2ea7f0", size = 80873 }]
[[distribution.dependencies]]
name = "idna"
version = "3.7"
source = "registry+https://pypi.org/simple"
[[distribution.dependencies]]
name = "sniffio"
version = "1.3.1"
source = "registry+https://pypi.org/simple"
[[distribution]]
name = "idna"
version = "3.7"
source = "registry+https://pypi.org/simple"
sdist = { url = "https://files.pythonhosted.org/packages/21/ed/f86a79a07470cb07819390452f178b3bef1d375f2ec021ecfc709fc7cf07/idna-3.7.tar.gz", hash = "sha256:028ff3aadf0609c1fd278d8ea3089299412a7a8b9bd005dd08b9f8285bcb5cfc", size = 189575 }
wheels = [{ url = "https://files.pythonhosted.org/packages/e5/3e/741d8c82801c347547f8a2a06aa57dbb1992be9e948df2ea0eda2c8b79e8/idna-3.7-py3-none-any.whl", hash = "sha256:82fee1fc78add43492d3a1898bfa6d8a904cc97d8427f683ed8e798d07761aa0", size = 66836 }]
[[distribution]]
name = "project"
version = "0.1.0"
source = "editable+."
sdist = { path = "." }
[distribution.dev-dependencies]
[[distribution.dev-dependencies.dev]]
name = "anyio"
version = "3.7.0"
source = "registry+https://pypi.org/simple"
[[distribution]]
name = "sniffio"
version = "1.3.1"
source = "registry+https://pypi.org/simple"
sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372 }
wheels = [{ url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235 }]
"###
);
});
// Install from the lockfile.
uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv sync` is experimental and may change without warning.
Audited 4 packages in [TIME]
"###);
Ok(())
}
/// Update a PyPI requirement.
#[test]
fn update_registry() -> Result<()> {
@ -623,6 +746,126 @@ fn remove_registry() -> Result<()> {
Ok(())
}
/// Remove a development dependency.
#[test]
fn remove_dev() -> Result<()> {
let context = TestContext::new("3.12");
let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
[tool.uv]
dev-dependencies = ["anyio==3.7.0"]
"#})?;
uv_snapshot!(context.filters(), context.lock(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv lock` is experimental and may change without warning.
Resolved 4 packages in [TIME]
"###);
uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv sync` is experimental and may change without warning.
Downloaded 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);
uv_snapshot!(context.filters(), context.remove(&["anyio"]), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
warning: `uv remove` is experimental and may change without warning.
warning: `anyio` is a development dependency; try calling `uv add --dev`
error: The dependency `anyio` could not be found in `dependencies`
"###);
uv_snapshot!(context.filters(), context.remove(&["anyio"]).arg("--dev"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv remove` is experimental and may change without warning.
Resolved 1 package in [TIME]
Downloaded 1 package in [TIME]
Uninstalled 1 package in [TIME]
Installed 1 package in [TIME]
- project==0.1.0 (from file://[TEMP_DIR]/)
+ project==0.1.0 (from file://[TEMP_DIR]/)
"###);
let pyproject_toml = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
[tool.uv]
dev-dependencies = []
"###
);
});
let lock = fs_err::read_to_string(context.temp_dir.join("uv.lock"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
lock, @r###"
version = 1
requires-python = ">=3.12"
[[distribution]]
name = "project"
version = "0.1.0"
source = "editable+."
sdist = { path = "." }
"###
);
});
// Install from the lockfile.
uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv sync` is experimental and may change without warning.
Audited 1 package in [TIME]
"###);
Ok(())
}
/// Remove a PyPI requirement that occurs multiple times.
#[test]
fn remove_all_registry() -> Result<()> {