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

Display the target virtual environment path if non-default #7850

Merged
merged 2 commits into from
Oct 2, 2024
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
9 changes: 2 additions & 7 deletions crates/uv/src/commands/pip/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)?;
Expand Down
9 changes: 2 additions & 7 deletions crates/uv/src/commands/pip/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/uv/src/commands/pip/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions crates/uv/src/commands/pip/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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)?;
Expand Down
43 changes: 43 additions & 0 deletions crates/uv/src/commands/pip/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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) = std::path::absolute(env.root()) 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(());
}
}
Comment on lines +559 to +571
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These guard against cases seen in #4835 but that implementation was in operations::install which is used far more broadly. In contrast, here we target the uv pip interface only and display a message way earlier, not during install. I think it's important to display the message earlier, as it's relevant for the resolution and audit. I think these guards are okay to retain here, though they're not used now. Willing to remove them if someone feels strongly though.


// Do not report a default environment path
if let Ok(default) = std::path::absolute(PathBuf::from(".venv")) {
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,
Expand Down
8 changes: 2 additions & 6 deletions crates/uv/src/commands/pip/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;

Expand Down Expand Up @@ -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)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/uv/src/commands/pip/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 3 additions & 10 deletions crates/uv/src/commands/pip/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)?;
Expand Down
7 changes: 2 additions & 5 deletions crates/uv/src/commands/pip/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/uv/tests/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading
Loading