From 8174e0928a3d66bbf0f4ff2eca522e2f3812e548 Mon Sep 17 00:00:00 2001 From: Aditya-PS-05 Date: Fri, 18 Oct 2024 00:01:12 +0530 Subject: [PATCH 1/2] uv run - child process added --- Cargo.toml | 2 +- crates/uv/src/commands/project/run.rs | 36 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6df7e5a7189e..36c2369afcde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -153,7 +153,7 @@ tempfile = { version = "3.12.0" } textwrap = { version = "0.16.1" } thiserror = { version = "1.0.63" } tl = { git = "https://github.com/charliermarsh/tl.git", rev = "6e25b2ee2513d75385101a8ff9f591ef51f314ec" } -tokio = { version = "1.40.0", features = ["fs", "io-util", "macros", "process", "signal", "sync"] } +tokio = { version = "1.40.0", features = ["fs", "io-util", "macros", "process", "signal", "sync", "io-std"] } tokio-stream = { version = "0.1.16" } tokio-util = { version = "0.7.12", features = ["compat"] } toml = { version = "0.8.19" } diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index 38894f4c55de..ca885c17bebc 100644 --- a/crates/uv/src/commands/project/run.rs +++ b/crates/uv/src/commands/project/run.rs @@ -2,14 +2,16 @@ use std::borrow::Cow; use std::collections::BTreeMap; use std::ffi::OsString; use std::fmt::Write; -use std::io::Read; +use std::io::{stdin, Read}; use std::path::{Path, PathBuf}; +use std::process::Stdio; // for synchronous reading from stdin use anstream::eprint; use anyhow::{anyhow, bail, Context}; use futures::StreamExt; use itertools::Itertools; use owo_colors::OwoColorize; +use tokio::io::{self, AsyncWriteExt}; use tokio::process::Command; use tracing::{debug, warn}; use url::Url; @@ -1210,9 +1212,35 @@ impl RunCommand { let is_dir = metadata.as_ref().map_or(false, std::fs::Metadata::is_dir); if target.eq_ignore_ascii_case("-") { - let mut buf = Vec::with_capacity(1024); - std::io::stdin().read_to_end(&mut buf)?; - Ok(Self::PythonStdin(buf)) + // Spawn the Python process with piped stdin + let mut child = Command::new("python3") + .stdin(Stdio::piped()) // Pipe parent's stdin to the child process + .stdout(Stdio::inherit()) // Use parent's stdout + .stderr(Stdio::inherit()) // Use parent's stderr + .spawn()?; // Spawn the child process + + // If we successfully opened the child's stdin + if let Some(mut child_stdin) = child.stdin.take() { + // Spawning a blocking task to read stdin and send it to the child process + let buffer = tokio::task::spawn_blocking(move || { + let mut buffer = Vec::new(); + stdin().read_to_end(&mut buffer).expect("Failed to read from stdin"); + buffer + }) + .await + .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Task join error: {}", e)))?; + + // Writing buffer to the child's stdin asynchronously + child_stdin.write_all(&buffer).await?; + } + + // Waiting for the child process to finish + let status = child.wait().await?; + if !status.success() { + eprintln!("Python script failed with status: {:?}", status); + } + + Ok(Self::PythonStdin(vec![])) } else if target.eq_ignore_ascii_case("python") { Ok(Self::Python(args.to_vec())) } else if target_path From dc5ac69f35271c82ea764670bd5afdb4d237ecea Mon Sep 17 00:00:00 2001 From: Aditya-PS-05 Date: Fri, 18 Oct 2024 00:02:23 +0530 Subject: [PATCH 2/2] cargo fmt --- crates/uv/src/commands/project/run.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index ca885c17bebc..7596568cc78a 100644 --- a/crates/uv/src/commands/project/run.rs +++ b/crates/uv/src/commands/project/run.rs @@ -1214,7 +1214,7 @@ impl RunCommand { if target.eq_ignore_ascii_case("-") { // Spawn the Python process with piped stdin let mut child = Command::new("python3") - .stdin(Stdio::piped()) // Pipe parent's stdin to the child process + .stdin(Stdio::piped()) // Pipe parent's stdin to the child process .stdout(Stdio::inherit()) // Use parent's stdout .stderr(Stdio::inherit()) // Use parent's stderr .spawn()?; // Spawn the child process @@ -1224,11 +1224,15 @@ impl RunCommand { // Spawning a blocking task to read stdin and send it to the child process let buffer = tokio::task::spawn_blocking(move || { let mut buffer = Vec::new(); - stdin().read_to_end(&mut buffer).expect("Failed to read from stdin"); + stdin() + .read_to_end(&mut buffer) + .expect("Failed to read from stdin"); buffer }) .await - .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("Task join error: {}", e)))?; + .map_err(|e| { + io::Error::new(io::ErrorKind::Other, format!("Task join error: {}", e)) + })?; // Writing buffer to the child's stdin asynchronously child_stdin.write_all(&buffer).await?;