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

Fix uv run python when the executable has a different name #6362

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 19 additions & 4 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 @@ -606,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 @@ -624,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 @@ -731,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 @@ -740,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.
Copy link
Member

Choose a reason for hiding this comment

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

Could this not resolve to some Python that doesn't match the intended interpreter? Like you run uv run -p 3.9, but we then pick up Python 3.12 that's on your path?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah but what if you have a python executable in the interpreter bin that does some special behavior? Should we unconditionally switch to the sys executable? Should we just look in the interpreter bin instead of the whole PATH here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems problematic both ways. I'm okay with unconditional replacement, I guess?

Copy link
Member Author

Choose a reason for hiding this comment

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

Here's that approach #6363

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
Loading