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

Show an interpreter-focused message for --target and --prefix #9373

Merged
merged 1 commit into from
Nov 27, 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
43 changes: 27 additions & 16 deletions crates/uv/src/commands/pip/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use uv_installer::{SatisfiesResult, SitePackages};
use uv_pep508::PackageName;
use uv_pypi_types::{Conflicts, Requirement};
use uv_python::{
EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, PythonVersion, Target,
EnvironmentPreference, Prefix, PythonEnvironment, PythonInstallation, PythonPreference,
PythonRequest, PythonVersion, Target,
};
use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_resolver::{
Expand All @@ -32,8 +33,8 @@ 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::{report_interpreter, report_target_environment};
use crate::commands::pip::{operations, resolution_markers, resolution_tags};
use crate::commands::{diagnostics, ExitStatus, SharedState};
use crate::printer::Printer;
Expand Down Expand Up @@ -76,6 +77,7 @@ pub(crate) async fn pip_install(
break_system_packages: bool,
target: Option<Target>,
prefix: Option<Prefix>,
python_preference: PythonPreference,
concurrency: Concurrency,
native_tls: bool,
allow_insecure_host: &[TrustedHost],
Expand Down Expand Up @@ -138,22 +140,31 @@ pub(crate) async fn pip_install(
)
.collect();

// Determine whether we're modifying the discovered environment, or a separate target.
let mutable = !(target.is_some() || prefix.is_some());

// Detect the current Python interpreter.
let environment = PythonEnvironment::find(
&python
.as_deref()
.map(PythonRequest::parse)
.unwrap_or_default(),
EnvironmentPreference::from_system_flag(system, mutable),
&cache,
)?;

if mutable {
let environment = if target.is_some() || prefix.is_some() {
let installation = PythonInstallation::find(
&python
.as_deref()
.map(PythonRequest::parse)
.unwrap_or_default(),
EnvironmentPreference::from_system_flag(system, false),
python_preference,
&cache,
)?;
report_interpreter(&installation, true, printer)?;
PythonEnvironment::from_installation(installation)
} else {
let environment = PythonEnvironment::find(
&python
.as_deref()
.map(PythonRequest::parse)
.unwrap_or_default(),
EnvironmentPreference::from_system_flag(system, true),
&cache,
)?;
report_target_environment(&environment, &cache, printer)?;
}
environment
};

// Apply any `--target` or `--prefix` directories.
let environment = if let Some(target) = target {
Expand Down
61 changes: 59 additions & 2 deletions crates/uv/src/commands/pip/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use uv_installer::{Plan, Planner, Preparer, SitePackages};
use uv_normalize::{GroupName, PackageName};
use uv_platform_tags::Tags;
use uv_pypi_types::{Conflicts, ResolverMarkerEnvironment};
use uv_python::PythonEnvironment;
use uv_python::{PythonEnvironment, PythonInstallation};
use uv_requirements::{
LookaheadResolver, NamedRequirementsResolver, RequirementsSource, RequirementsSpecification,
SourceTreeResolver,
Expand Down Expand Up @@ -571,14 +571,71 @@ pub(crate) async fn install(
Ok(changelog)
}

/// Display a message about the interpreter that was selected for the operation.
pub(crate) fn report_interpreter(
python: &PythonInstallation,
dimmed: bool,
printer: Printer,
) -> Result<(), Error> {
let managed = python.source().is_managed();
let implementation = python.implementation();
let interpreter = python.interpreter();

if dimmed {
Copy link
Member Author

Choose a reason for hiding this comment

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

Pretty clunky but like... in uv venv, we show this at full color, with cyan paths. In uv pip install, we show it dimmed. (This is largely preserving existing behavior, open to changing one of the two.)

if managed {
writeln!(
printer.stderr(),
"{}",
format!(
"Using {} {}",
implementation.pretty(),
interpreter.python_version()
)
.dimmed()
)?;
} else {
writeln!(
printer.stderr(),
"{}",
format!(
"Using {} {} interpreter at: {}",
implementation.pretty(),
interpreter.python_version(),
interpreter.sys_executable().user_display()
)
.dimmed()
)?;
}
} else {
if managed {
writeln!(
printer.stderr(),
"Using {} {}",
implementation.pretty(),
interpreter.python_version().cyan()
)?;
} else {
writeln!(
printer.stderr(),
"Using {} {} interpreter at: {}",
implementation.pretty(),
interpreter.python_version(),
interpreter.sys_executable().user_display().cyan()
)?;
}
}

Ok(())
}

/// 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 {}",
"Using Python {} environment at: {}",
env.interpreter().python_version(),
env.root().user_display()
);
Expand Down
43 changes: 27 additions & 16 deletions crates/uv/src/commands/pip/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use uv_installer::SitePackages;
use uv_pep508::PackageName;
use uv_pypi_types::Conflicts;
use uv_python::{
EnvironmentPreference, Prefix, PythonEnvironment, PythonRequest, PythonVersion, Target,
EnvironmentPreference, Prefix, PythonEnvironment, PythonInstallation, PythonPreference,
PythonRequest, PythonVersion, Target,
};
use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_resolver::{
Expand All @@ -29,8 +30,8 @@ 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::{report_interpreter, report_target_environment};
use crate::commands::pip::{operations, resolution_markers, resolution_tags};
use crate::commands::{diagnostics, ExitStatus, SharedState};
use crate::printer::Printer;
Expand Down Expand Up @@ -65,6 +66,7 @@ pub(crate) async fn pip_sync(
target: Option<Target>,
prefix: Option<Prefix>,
sources: SourceStrategy,
python_preference: PythonPreference,
concurrency: Concurrency,
native_tls: bool,
allow_insecure_host: &[TrustedHost],
Expand Down Expand Up @@ -122,22 +124,31 @@ pub(crate) async fn pip_sync(
}
}

// Determine whether we're modifying the discovered environment, or a separate target.
let mutable = !(target.is_some() || prefix.is_some());

// Detect the current Python interpreter.
let environment = PythonEnvironment::find(
&python
.as_deref()
.map(PythonRequest::parse)
.unwrap_or_default(),
EnvironmentPreference::from_system_flag(system, mutable),
&cache,
)?;

if mutable {
let environment = if target.is_some() || prefix.is_some() {
let installation = PythonInstallation::find(
&python
.as_deref()
.map(PythonRequest::parse)
.unwrap_or_default(),
EnvironmentPreference::from_system_flag(system, false),
python_preference,
&cache,
)?;
report_interpreter(&installation, true, printer)?;
PythonEnvironment::from_installation(installation)
} else {
let environment = PythonEnvironment::find(
&python
.as_deref()
.map(PythonRequest::parse)
.unwrap_or_default(),
EnvironmentPreference::from_system_flag(system, true),
&cache,
)?;
report_target_environment(&environment, &cache, printer)?;
}
environment
};

// Apply any `--target` or `--prefix` directories.
let environment = if let Some(target) = target {
Expand Down
55 changes: 18 additions & 37 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use uv_warnings::{warn_user, warn_user_once};
use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceError};

use crate::commands::pip::loggers::{DefaultInstallLogger, InstallLogger};
use crate::commands::pip::operations::Changelog;
use crate::commands::pip::operations::{report_interpreter, Changelog};
use crate::commands::project::{validate_requires_python, WorkspacePython};
use crate::commands::reporters::PythonDownloadReporter;
use crate::commands::{ExitStatus, SharedState};
Expand Down Expand Up @@ -201,23 +201,23 @@ async fn venv_impl(
.into_diagnostic()?;

// Locate the Python interpreter to use in the environment
let python = PythonInstallation::find_or_download(
python_request.as_ref(),
EnvironmentPreference::OnlySystem,
python_preference,
python_downloads,
&client_builder,
cache,
Some(&reporter),
install_mirrors.python_install_mirror.as_deref(),
install_mirrors.pypy_install_mirror.as_deref(),
)
.await
.into_diagnostic()?;

let managed = python.source().is_managed();
let implementation = python.implementation();
let interpreter = python.into_interpreter();
let interpreter = {
let python = PythonInstallation::find_or_download(
python_request.as_ref(),
EnvironmentPreference::OnlySystem,
python_preference,
python_downloads,
&client_builder,
cache,
Some(&reporter),
install_mirrors.python_install_mirror.as_deref(),
install_mirrors.pypy_install_mirror.as_deref(),
)
.await
.into_diagnostic()?;
report_interpreter(&python, false, printer).into_diagnostic()?;
python.into_interpreter()
};

// Add all authenticated sources to the cache.
for index in index_locations.allowed_indexes() {
Expand All @@ -226,25 +226,6 @@ async fn venv_impl(
}
}

if managed {
writeln!(
printer.stderr(),
"Using {} {}",
implementation.pretty(),
interpreter.python_version().cyan()
)
.into_diagnostic()?;
} else {
writeln!(
printer.stderr(),
"Using {} {} interpreter at: {}",
implementation.pretty(),
interpreter.python_version(),
interpreter.sys_executable().user_display().cyan()
)
.into_diagnostic()?;
}

// Check if the discovered Python version is incompatible with the current workspace
if let Some(requires_python) = requires_python {
match validate_requires_python(
Expand Down
2 changes: 2 additions & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
args.settings.target,
args.settings.prefix,
args.settings.sources,
globals.python_preference,
globals.concurrency,
globals.native_tls,
&globals.allow_insecure_host,
Expand Down Expand Up @@ -525,6 +526,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
args.settings.break_system_packages,
args.settings.target,
args.settings.prefix,
globals.python_preference,
globals.concurrency,
globals.native_tls,
&globals.allow_insecure_host,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/it/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ fn relative_path() -> Result<()> {
----- stdout -----

----- stderr -----
Using Python 3.12.[X] environment at [VENV]/
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