From 93a2c7c30d621a2d19092cfa6e241bf45ce96698 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 1 Oct 2024 21:55:35 -0500 Subject: [PATCH 1/2] Display the target virtual environment path if non-default --- crates/uv/src/commands/pip/check.rs | 9 ++--- crates/uv/src/commands/pip/freeze.rs | 9 ++--- crates/uv/src/commands/pip/install.rs | 7 ++-- crates/uv/src/commands/pip/list.rs | 8 ++--- crates/uv/src/commands/pip/operations.rs | 43 ++++++++++++++++++++++++ crates/uv/src/commands/pip/show.rs | 8 ++--- crates/uv/src/commands/pip/sync.rs | 7 ++-- crates/uv/src/commands/pip/tree.rs | 13 ++----- crates/uv/src/commands/pip/uninstall.rs | 7 ++-- crates/uv/tests/export.rs | 1 + crates/uv/tests/pip_install.rs | 16 +++++++++ 11 files changed, 77 insertions(+), 51 deletions(-) diff --git a/crates/uv/src/commands/pip/check.rs b/crates/uv/src/commands/pip/check.rs index 9ba2b297e776..d53e6a4691a1 100644 --- a/crates/uv/src/commands/pip/check.rs +++ b/crates/uv/src/commands/pip/check.rs @@ -3,14 +3,13 @@ use std::time::Instant; use anyhow::Result; use owo_colors::OwoColorize; -use tracing::debug; use uv_cache::Cache; use uv_distribution_types::{Diagnostic, InstalledDist}; -use uv_fs::Simplified; use uv_installer::{SitePackages, SitePackagesDiagnostic}; use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::{elapsed, ExitStatus}; use crate::printer::Printer; @@ -30,11 +29,7 @@ pub(crate) fn pip_check( cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, cache, printer)?; // Build the installed index. let site_packages = SitePackages::from_environment(&environment)?; diff --git a/crates/uv/src/commands/pip/freeze.rs b/crates/uv/src/commands/pip/freeze.rs index ea2dd6277ab1..32405b80906a 100644 --- a/crates/uv/src/commands/pip/freeze.rs +++ b/crates/uv/src/commands/pip/freeze.rs @@ -3,14 +3,13 @@ use std::fmt::Write; use anyhow::Result; use itertools::Itertools; use owo_colors::OwoColorize; -use tracing::debug; use uv_cache::Cache; use uv_distribution_types::{Diagnostic, InstalledDist, Name}; -use uv_fs::Simplified; use uv_installer::SitePackages; use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::ExitStatus; use crate::printer::Printer; @@ -30,11 +29,7 @@ pub(crate) fn pip_freeze( cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, cache, printer)?; // Build the installed index. let site_packages = SitePackages::from_environment(&environment)?; diff --git a/crates/uv/src/commands/pip/install.rs b/crates/uv/src/commands/pip/install.rs index b939df6d82a6..bd31e1b734b3 100644 --- a/crates/uv/src/commands/pip/install.rs +++ b/crates/uv/src/commands/pip/install.rs @@ -33,6 +33,7 @@ use uv_resolver::{ use uv_types::{BuildIsolation, HashStrategy}; use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger, InstallLogger}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::pip::operations::Modifications; use crate::commands::pip::{operations, resolution_markers, resolution_tags}; use crate::commands::{diagnostics, ExitStatus, SharedState}; @@ -147,11 +148,7 @@ pub(crate) async fn pip_install( &cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, &cache, printer)?; // Apply any `--target` or `--prefix` directories. let environment = if let Some(target) = target { diff --git a/crates/uv/src/commands/pip/list.rs b/crates/uv/src/commands/pip/list.rs index 3fd915d9925d..7401ed0a7268 100644 --- a/crates/uv/src/commands/pip/list.rs +++ b/crates/uv/src/commands/pip/list.rs @@ -5,7 +5,6 @@ use anyhow::Result; use itertools::Itertools; use owo_colors::OwoColorize; use serde::Serialize; -use tracing::debug; use unicode_width::UnicodeWidthStr; use uv_cache::Cache; @@ -17,6 +16,7 @@ use uv_normalize::PackageName; use uv_python::PythonRequest; use uv_python::{EnvironmentPreference, PythonEnvironment}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::ExitStatus; use crate::printer::Printer; @@ -39,11 +39,7 @@ pub(crate) fn pip_list( cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, cache, printer)?; // Build the installed index. let site_packages = SitePackages::from_environment(&environment)?; diff --git a/crates/uv/src/commands/pip/operations.rs b/crates/uv/src/commands/pip/operations.rs index 8ff32ad0d51b..ed36ab9c7968 100644 --- a/crates/uv/src/commands/pip/operations.rs +++ b/crates/uv/src/commands/pip/operations.rs @@ -7,6 +7,7 @@ use std::collections::{BTreeSet, HashSet}; use std::fmt::Write; use std::path::PathBuf; use tracing::debug; +use uv_tool::InstalledTools; use uv_cache::Cache; use uv_client::{BaseClientBuilder, RegistryClient}; @@ -538,6 +539,48 @@ pub(crate) async fn install( Ok(changelog) } +/// Display a message about the target environment for the operation. +pub(crate) fn report_target_environment( + env: &PythonEnvironment, + cache: &Cache, + printer: Printer, +) -> Result<(), Error> { + let message = format!( + "Using Python {} environment at {}", + env.interpreter().python_version(), + env.root().user_display() + ); + + let Ok(target) = env.root().canonicalize() else { + debug!("{}", message); + return Ok(()); + }; + + // Do not report environments in the cache + if target.starts_with(cache.root()) { + debug!("{}", message); + return Ok(()); + } + + // Do not report tool environments + if let Ok(tools) = InstalledTools::from_settings() { + if target.starts_with(tools.root()) { + debug!("{}", message); + return Ok(()); + } + } + + // Do not report a default environment path + if let Ok(default) = PathBuf::from(".venv").canonicalize() { + if target == default { + debug!("{}", message); + return Ok(()); + } + } + + Ok(writeln!(printer.stderr(), "{}", message.dimmed())?) +} + /// Report on the results of a dry-run installation. fn report_dry_run( resolution: &Resolution, diff --git a/crates/uv/src/commands/pip/show.rs b/crates/uv/src/commands/pip/show.rs index e519619a1ba7..7c1f249c1962 100644 --- a/crates/uv/src/commands/pip/show.rs +++ b/crates/uv/src/commands/pip/show.rs @@ -4,7 +4,6 @@ use anyhow::Result; use itertools::{Either, Itertools}; use owo_colors::OwoColorize; use rustc_hash::FxHashMap; -use tracing::debug; use uv_cache::Cache; use uv_distribution_types::{Diagnostic, Name}; @@ -13,6 +12,7 @@ use uv_installer::SitePackages; use uv_normalize::PackageName; use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::ExitStatus; use crate::printer::Printer; @@ -45,11 +45,7 @@ pub(crate) fn pip_show( cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, cache, printer)?; // Build the installed index. let site_packages = SitePackages::from_environment(&environment)?; diff --git a/crates/uv/src/commands/pip/sync.rs b/crates/uv/src/commands/pip/sync.rs index a5ea9c02fa3f..3d49dbf7f053 100644 --- a/crates/uv/src/commands/pip/sync.rs +++ b/crates/uv/src/commands/pip/sync.rs @@ -29,6 +29,7 @@ use uv_resolver::{ use uv_types::{BuildIsolation, HashStrategy}; use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::pip::operations::Modifications; use crate::commands::pip::{operations, resolution_markers, resolution_tags}; use crate::commands::{diagnostics, ExitStatus, SharedState}; @@ -131,11 +132,7 @@ pub(crate) async fn pip_sync( &cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, &cache, printer)?; // Apply any `--target` or `--prefix` directories. let environment = if let Some(target) = target { diff --git a/crates/uv/src/commands/pip/tree.rs b/crates/uv/src/commands/pip/tree.rs index 4f64860ea35e..a1d5e8146ce0 100644 --- a/crates/uv/src/commands/pip/tree.rs +++ b/crates/uv/src/commands/pip/tree.rs @@ -4,19 +4,16 @@ use anyhow::Result; use indexmap::IndexMap; use owo_colors::OwoColorize; use rustc_hash::{FxHashMap, FxHashSet}; -use tracing::debug; use uv_cache::Cache; use uv_distribution::Metadata; use uv_distribution_types::{Diagnostic, Name}; -use uv_fs::Simplified; use uv_installer::SitePackages; use uv_normalize::PackageName; use uv_pypi_types::{RequirementSource, ResolverMarkerEnvironment}; -use uv_python::EnvironmentPreference; -use uv_python::PythonEnvironment; -use uv_python::PythonRequest; +use uv_python::{EnvironmentPreference, PythonEnvironment, PythonRequest}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::ExitStatus; use crate::printer::Printer; @@ -42,11 +39,7 @@ pub(crate) fn pip_tree( cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan() - ); + report_target_environment(&environment, cache, printer)?; // Read packages from the virtual environment. let site_packages = SitePackages::from_environment(&environment)?; diff --git a/crates/uv/src/commands/pip/uninstall.rs b/crates/uv/src/commands/pip/uninstall.rs index f39bcdc49920..d831eeb4a132 100644 --- a/crates/uv/src/commands/pip/uninstall.rs +++ b/crates/uv/src/commands/pip/uninstall.rs @@ -18,6 +18,7 @@ use uv_python::PythonRequest; use uv_python::{Prefix, PythonEnvironment, Target}; use uv_requirements::{RequirementsSource, RequirementsSpecification}; +use crate::commands::pip::operations::report_target_environment; use crate::commands::{elapsed, ExitStatus}; use crate::printer::Printer; @@ -57,11 +58,7 @@ pub(crate) async fn pip_uninstall( &cache, )?; - debug!( - "Using Python {} environment at {}", - environment.interpreter().python_version(), - environment.python_executable().user_display().cyan(), - ); + report_target_environment(&environment, &cache, printer)?; // Apply any `--target` or `--prefix` directories. let environment = if let Some(target) = target { diff --git a/crates/uv/tests/export.rs b/crates/uv/tests/export.rs index d482b33f31ad..f132773e4565 100644 --- a/crates/uv/tests/export.rs +++ b/crates/uv/tests/export.rs @@ -537,6 +537,7 @@ fn relative_path() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 3 packages in [TIME] Prepared 3 packages in [TIME] Installed 3 packages in [TIME] diff --git a/crates/uv/tests/pip_install.rs b/crates/uv/tests/pip_install.rs index e8a889512599..0dd4459af3ea 100644 --- a/crates/uv/tests/pip_install.rs +++ b/crates/uv/tests/pip_install.rs @@ -1124,6 +1124,7 @@ fn install_editable_bare_cli() { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 1 package in [TIME] Prepared 1 package in [TIME] Installed 1 package in [TIME] @@ -1150,6 +1151,7 @@ fn install_editable_bare_requirements_txt() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 1 package in [TIME] Prepared 1 package in [TIME] Installed 1 package in [TIME] @@ -3265,6 +3267,7 @@ requires-python = ">=3.8" ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 4 packages in [TIME] Installed 4 packages in [TIME] @@ -3284,6 +3287,7 @@ requires-python = ">=3.8" ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Audited 1 package in [TIME] "### ); @@ -3309,6 +3313,7 @@ requires-python = ">=3.8" ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 2 packages in [TIME] Uninstalled 2 packages in [TIME] @@ -3356,6 +3361,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 4 packages in [TIME] Installed 4 packages in [TIME] @@ -3375,6 +3381,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Audited 1 package in [TIME] "### ); @@ -3391,6 +3398,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 1 package in [TIME] Uninstalled 1 package in [TIME] @@ -3411,6 +3419,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 1 package in [TIME] Uninstalled 1 package in [TIME] @@ -3441,6 +3450,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Audited 1 package in [TIME] "### ); @@ -3473,6 +3483,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 1 package in [TIME] Uninstalled 1 package in [TIME] @@ -3493,6 +3504,7 @@ fn invalidate_path_on_cache_key() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 1 package in [TIME] Uninstalled 1 package in [TIME] @@ -3548,6 +3560,7 @@ fn invalidate_path_on_commit() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 4 packages in [TIME] Installed 4 packages in [TIME] @@ -3567,6 +3580,7 @@ fn invalidate_path_on_commit() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Audited 1 package in [TIME] "### ); @@ -3589,6 +3603,7 @@ fn invalidate_path_on_commit() -> Result<()> { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 4 packages in [TIME] Prepared 1 package in [TIME] Uninstalled 1 package in [TIME] @@ -4688,6 +4703,7 @@ fn deptry_gitignore() { ----- stdout ----- ----- stderr ----- + Using Python 3.12.[X] environment at [VENV]/ Resolved 3 packages in [TIME] Prepared 3 packages in [TIME] Installed 3 packages in [TIME] From ec07503c667f69f873ff2904d7c8aa9176c65e5d Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 1 Oct 2024 22:06:40 -0500 Subject: [PATCH 2/2] Use absolute instead of canonical paths --- crates/uv/src/commands/pip/operations.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/uv/src/commands/pip/operations.rs b/crates/uv/src/commands/pip/operations.rs index ed36ab9c7968..406c3f9a1265 100644 --- a/crates/uv/src/commands/pip/operations.rs +++ b/crates/uv/src/commands/pip/operations.rs @@ -551,7 +551,7 @@ pub(crate) fn report_target_environment( env.root().user_display() ); - let Ok(target) = env.root().canonicalize() else { + let Ok(target) = std::path::absolute(env.root()) else { debug!("{}", message); return Ok(()); }; @@ -571,7 +571,7 @@ pub(crate) fn report_target_environment( } // Do not report a default environment path - if let Ok(default) = PathBuf::from(".venv").canonicalize() { + if let Ok(default) = std::path::absolute(PathBuf::from(".venv")) { if target == default { debug!("{}", message); return Ok(());