From 5b74754140d969ecd32c2a0d8445ae3898e3c583 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 19 Aug 2024 16:29:33 -0500 Subject: [PATCH] Add output when `uv add` and `uv remove` update scripts (#6231) Closes https://github.com/astral-sh/uv/issues/6214 --- crates/uv/src/commands/project/add.rs | 17 +++++++++++++---- crates/uv/src/commands/project/remove.rs | 19 +++++++++++++++---- crates/uv/tests/edit.rs | 19 +++++++++++++------ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 483b3cc94..8fd5613d0 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -1,4 +1,5 @@ use std::collections::hash_map::Entry; +use std::fmt::Write; use std::path::{Path, PathBuf}; use anyhow::{bail, Context, Result}; @@ -15,7 +16,7 @@ use uv_configuration::{ }; use uv_dispatch::BuildDispatch; use uv_distribution::DistributionDatabase; -use uv_fs::CWD; +use uv_fs::{Simplified, CWD}; use uv_git::GIT_STORE; use uv_normalize::PackageName; use uv_python::{ @@ -425,9 +426,17 @@ pub(crate) async fn add( } }; - // If `--script`, exit early. There's no reason to lock and sync. - let Target::Project(project, venv) = target else { - return Ok(ExitStatus::Success); + let (project, venv) = match target { + Target::Project(project, venv) => (project, venv), + // If `--script`, exit early. There's no reason to lock and sync. + Target::Script(script, _) => { + writeln!( + printer.stderr(), + "Updated `{}`", + script.path.user_display().cyan() + )?; + return Ok(ExitStatus::Success); + } }; // If `--frozen`, exit early. There's no reason to lock and sync, and we don't need a `uv.lock` diff --git a/crates/uv/src/commands/project/remove.rs b/crates/uv/src/commands/project/remove.rs index e5f01e582..bfa93c5a8 100644 --- a/crates/uv/src/commands/project/remove.rs +++ b/crates/uv/src/commands/project/remove.rs @@ -1,10 +1,13 @@ +use std::fmt::Write; + use anyhow::{Context, Result}; +use owo_colors::OwoColorize; use pep508_rs::PackageName; use uv_cache::Cache; use uv_client::Connectivity; use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode}; -use uv_fs::CWD; +use uv_fs::{Simplified, CWD}; use uv_python::{PythonDownloads, PythonPreference, PythonRequest}; use uv_scripts::Pep723Script; use uv_warnings::{warn_user, warn_user_once}; @@ -141,9 +144,17 @@ pub(crate) async fn remove( return Ok(ExitStatus::Success); } - // If `--script`, exit early. There's no reason to lock and sync. - let Target::Project(project) = target else { - return Ok(ExitStatus::Success); + let project = match target { + Target::Project(project) => project, + // If `--script`, exit early. There's no reason to lock and sync. + Target::Script(script) => { + writeln!( + printer.stderr(), + "Updated `{}`", + script.path.user_display().cyan() + )?; + return Ok(ExitStatus::Success); + } }; // Discover or create the virtual environment. diff --git a/crates/uv/tests/edit.rs b/crates/uv/tests/edit.rs index 7b138f4e4..a0372b579 100644 --- a/crates/uv/tests/edit.rs +++ b/crates/uv/tests/edit.rs @@ -3536,13 +3536,14 @@ fn add_script() -> Result<()> { pprint([(k, v["title"]) for k, v in data.items()][:10]) "#})?; - uv_snapshot!(context.filters(), context.add(&["anyio"]).arg("--script").arg(script.path()), @r###" + uv_snapshot!(context.filters(), context.add(&["anyio"]).arg("--script").arg("script.py"), @r###" success: true exit_code: 0 ----- stdout ----- ----- stderr ----- warning: `uv add` is experimental and may change without warning + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?; @@ -3588,13 +3589,14 @@ fn add_script_without_metadata_table() -> Result<()> { pprint([(k, v["title"]) for k, v in data.items()][:10]) "#})?; - uv_snapshot!(context.filters(), context.add(&["rich", "requests<3"]).arg("--script").arg(script.path()), @r###" + uv_snapshot!(context.filters(), context.add(&["rich", "requests<3"]).arg("--script").arg("script.py"), @r###" success: true exit_code: 0 ----- stdout ----- ----- stderr ----- warning: `uv add` is experimental and may change without warning + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?; @@ -3639,13 +3641,14 @@ fn add_script_without_metadata_table_with_shebang() -> Result<()> { pprint([(k, v["title"]) for k, v in data.items()][:10]) "#})?; - uv_snapshot!(context.filters(), context.add(&["rich", "requests<3"]).arg("--script").arg(script.path()), @r###" + uv_snapshot!(context.filters(), context.add(&["rich", "requests<3"]).arg("--script").arg("script.py"), @r###" success: true exit_code: 0 ----- stdout ----- ----- stderr ----- warning: `uv add` is experimental and may change without warning + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?; @@ -3691,13 +3694,14 @@ fn add_script_without_metadata_table_with_docstring() -> Result<()> { pprint([(k, v["title"]) for k, v in data.items()][:10]) "#})?; - uv_snapshot!(context.filters(), context.add(&["rich", "requests<3"]).arg("--script").arg(script.path()), @r###" + uv_snapshot!(context.filters(), context.add(&["rich", "requests<3"]).arg("--script").arg("script.py"), @r###" success: true exit_code: 0 ----- stdout ----- ----- stderr ----- warning: `uv add` is experimental and may change without warning + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?; @@ -3751,13 +3755,14 @@ fn remove_script() -> Result<()> { pprint([(k, v["title"]) for k, v in data.items()][:10]) "#})?; - uv_snapshot!(context.filters(), context.remove(&["anyio"]).arg("--script").arg(script.path()), @r###" + uv_snapshot!(context.filters(), context.remove(&["anyio"]).arg("--script").arg("script.py"), @r###" success: true exit_code: 0 ----- stdout ----- ----- stderr ----- warning: `uv remove` is experimental and may change without warning + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?; @@ -3809,13 +3814,14 @@ fn remove_last_dep_script() -> Result<()> { pprint([(k, v["title"]) for k, v in data.items()][:10]) "#})?; - uv_snapshot!(context.filters(), context.remove(&["rich"]).arg("--script").arg(script.path()), @r###" + uv_snapshot!(context.filters(), context.remove(&["rich"]).arg("--script").arg("script.py"), @r###" success: true exit_code: 0 ----- stdout ----- ----- stderr ----- warning: `uv remove` is experimental and may change without warning + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?; @@ -3890,6 +3896,7 @@ fn add_git_to_script() -> Result<()> { ----- stdout ----- ----- stderr ----- + Updated `script.py` "###); let script_content = fs_err::read_to_string(context.temp_dir.join("script.py"))?;