Skip to content

Commit

Permalink
Always invoke found interpreter when uv run python is used (#6363)
Browse files Browse the repository at this point in the history
Alternative to #6362

Resolves the error mentioned in #6361
  • Loading branch information
zanieb committed Aug 21, 2024
1 parent fa0c20d commit 787f2a7
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ pub(crate) async fn parse_script(
// Parse the input command.
let command = RunCommand::from(command);

let RunCommand::Python(target, _) = &command else {
let RunCommand::PythonScript(target, _) = &command else {
return Ok(None);
};

Expand Down Expand Up @@ -716,8 +716,10 @@ fn can_skip_ephemeral(

#[derive(Debug)]
enum RunCommand {
/// Execute `python`.
Python(Vec<OsString>),
/// Execute a `python` script.
Python(PathBuf, Vec<OsString>),
PythonScript(PathBuf, Vec<OsString>),
/// Execute an external command.
External(OsString, Vec<OsString>),
/// Execute an empty command (in practice, `python` with no arguments).
Expand All @@ -728,15 +730,21 @@ impl RunCommand {
/// Return the name of the target executable, for display purposes.
fn display_executable(&self) -> Cow<'_, str> {
match self {
Self::Python(_, _) | Self::Empty => Cow::Borrowed("python"),
Self::Python(_) => Cow::Borrowed("python"),
Self::PythonScript(_, _) | Self::Empty => Cow::Borrowed("python"),
Self::External(executable, _) => executable.to_string_lossy(),
}
}

/// Convert a [`RunCommand`] into a [`Command`].
fn as_command(&self, interpreter: &Interpreter) -> Command {
match self {
Self::Python(target, args) => {
Self::Python(args) => {
let mut process = Command::new(interpreter.sys_executable());
process.args(args);
process
}
Self::PythonScript(target, args) => {
let mut process = Command::new(interpreter.sys_executable());
process.arg(target);
process.args(args);
Expand All @@ -755,7 +763,14 @@ impl RunCommand {
impl std::fmt::Display for RunCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Python(target, args) => {
Self::Python(args) => {
write!(f, "python")?;
for arg in args {
write!(f, " {}", arg.to_string_lossy())?;
}
Ok(())
}
Self::PythonScript(target, args) => {
write!(f, "python {}", target.display())?;
for arg in args {
write!(f, " {}", arg.to_string_lossy())?;
Expand Down Expand Up @@ -786,12 +801,14 @@ impl From<&ExternalCommand> for RunCommand {
};

let target_path = PathBuf::from(&target);
if target_path
if target.eq_ignore_ascii_case("python") {
Self::Python(args.to_vec())
} else if target_path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("py"))
&& target_path.exists()
{
Self::Python(target_path, args.to_vec())
Self::PythonScript(target_path, args.to_vec())
} else {
Self::External(
target.clone(),
Expand Down

0 comments on commit 787f2a7

Please sign in to comment.