From 97a04524d724d21301d203835c8b575902552be0 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 5 Dec 2023 13:52:06 +0100 Subject: [PATCH] Activate venv before source dist build Fixes #552 --- crates/gourgeist/src/bare.rs | 2 +- crates/puffin-build/src/lib.rs | 24 +++++++++++++------- crates/puffin-interpreter/src/virtual_env.rs | 15 ++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/crates/gourgeist/src/bare.rs b/crates/gourgeist/src/bare.rs index db4d9d00c7c1..8f912878dc1a 100644 --- a/crates/gourgeist/src/bare.rs +++ b/crates/gourgeist/src/bare.rs @@ -82,7 +82,7 @@ pub fn create_bare_venv(location: &Utf8Path, interpreter: &Interpreter) -> io::R } #[cfg(windows)] { - location.join("Bin") + location.join("Scripts") } #[cfg(not(any(unix, windows)))] { diff --git a/crates/puffin-build/src/lib.rs b/crates/puffin-build/src/lib.rs index d94a2879299c..1e3829caf9ec 100644 --- a/crates/puffin-build/src/lib.rs +++ b/crates/puffin-build/src/lib.rs @@ -2,6 +2,7 @@ //! //! +use std::env; use std::fmt::{Display, Formatter}; use std::io; use std::io::BufRead; @@ -401,8 +402,7 @@ impl SourceBuild { name="prepare_metadata_for_build_wheel", python_version = %self.venv.interpreter().version() ); - let output = - run_python_script(&self.venv.python_executable(), &script, &self.source_tree).await?; + let output = run_python_script(&self.venv, &script, &self.source_tree).await?; drop(span); if !output.status.success() { return Err(Error::from_command_output( @@ -519,8 +519,7 @@ impl SourceBuild { name=format!("build_{}", self.build_kind), python_version = %self.venv.interpreter().version() ); - let output = - run_python_script(&self.venv.python_executable(), &script, &self.source_tree).await?; + let output = run_python_script(&self.venv, &script, &self.source_tree).await?; drop(span); if !output.status.success() { return Err(Error::from_command_output( @@ -587,7 +586,7 @@ async fn create_pep517_build_environment( name="build_wheel", python_version = %venv.interpreter().version() ); - let output = run_python_script(&venv.python_executable(), &script, source_tree).await?; + let output = run_python_script(venv, &script, source_tree).await?; drop(span); if !output.status.success() { return Err(Error::from_command_output( @@ -688,16 +687,25 @@ fn extract_archive(sdist: &Path, extracted: &PathBuf) -> Result /// It is the caller's responsibility to create an informative span. async fn run_python_script( - python_interpreter: &Path, + venv: &Virtualenv, script: &str, source_tree: &Path, ) -> Result { - Command::new(python_interpreter) + // `OsString` doesn't impl `Add` + let mut new_path = venv.bin_dir().into_os_string(); + if let Some(path) = env::var_os("PATH") { + new_path.push(":"); + new_path.push(path); + } + Command::new(venv.python_executable()) .args(["-c", script]) .current_dir(source_tree) + // Activate the venv + .env("VIRTUAL_ENV", venv.root()) + .env("PATH", new_path) .output() .await - .map_err(|err| Error::CommandFailed(python_interpreter.to_path_buf(), err)) + .map_err(|err| Error::CommandFailed(venv.python_executable(), err)) } #[cfg(test)] diff --git a/crates/puffin-interpreter/src/virtual_env.rs b/crates/puffin-interpreter/src/virtual_env.rs index 696080f98a30..1a5d25ece78f 100644 --- a/crates/puffin-interpreter/src/virtual_env.rs +++ b/crates/puffin-interpreter/src/virtual_env.rs @@ -84,6 +84,21 @@ impl Virtualenv { .platform .venv_site_packages(&self.root, self.interpreter().simple_version()) } + + pub fn bin_dir(&self) -> PathBuf { + #[cfg(unix)] + { + self.root().join("bin") + } + #[cfg(windows)] + { + self.root().join("Scripts") + } + #[cfg(not(any(unix, windows)))] + { + compile_error!("only unix (like mac and linux) and windows are supported") + } + } } /// Locate the current virtual environment.