Skip to content

Commit

Permalink
Fix uv run python when the executable has a different name
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Aug 21, 2024
1 parent ae06205 commit da48f30
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_scripts::{Pep723Error, Pep723Script};
use uv_warnings::warn_user_once;
use uv_workspace::{DiscoveryOptions, VirtualProject, Workspace, WorkspaceError};
use which::which_in_global;

use crate::commands::pip::loggers::{
DefaultInstallLogger, DefaultResolveLogger, SummaryInstallLogger, SummaryResolveLogger,
Expand Down Expand Up @@ -445,10 +446,8 @@ pub(crate) async fn run(
let python_request = if let Some(request) = python.as_deref() {
Some(PythonRequest::parse(request))
// (2) Request from `.python-version`
} else if let Some(request) = request_from_version_file(&CWD).await? {
Some(request)
} else {
None
request_from_version_file(&CWD).await?
};

let python = PythonInstallation::find_or_download(
Expand Down Expand Up @@ -608,9 +607,6 @@ pub(crate) async fn run(
.as_ref()
.map_or_else(|| &base_interpreter, |env| env.interpreter());

debug!("Running `{command}`");
let mut process = command.as_command(interpreter);

// Construct the `PATH` environment variable.
let new_path = std::env::join_paths(
ephemeral_env
Expand All @@ -626,6 +622,9 @@ pub(crate) async fn run(
.flat_map(std::env::split_paths),
),
)?;

debug!("Running `{command}`");
let mut process = command.as_command(&new_path, interpreter);
process.env("PATH", new_path);

// Spawn and wait for completion
Expand Down Expand Up @@ -733,7 +732,7 @@ impl RunCommand {
}

/// Convert a [`RunCommand`] into a [`Command`].
fn as_command(&self, interpreter: &Interpreter) -> Command {
fn as_command(&self, path: &OsString, interpreter: &Interpreter) -> Command {
match self {
Self::Python(target, args) => {
let mut process = Command::new(interpreter.sys_executable());
Expand All @@ -742,6 +741,20 @@ impl RunCommand {
process
}
Self::External(executable, args) => {
// If the executable is `python` and it's not on the path, rename it to the
// path reported by the interpreter.
let executable = if executable.eq_ignore_ascii_case("python")
&& which_in_global(executable, Some(&path))
.ok()
.into_iter()
.flatten()
.next()
.is_none()
{
interpreter.sys_executable().as_os_str()
} else {
executable
};
let mut process = Command::new(executable);
process.args(args);
process
Expand Down

0 comments on commit da48f30

Please sign in to comment.