diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf9b6a10d..1adaf20f69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - pip is no longer required to be enabled to activate a virtual environment. - The venv root is now the location of a located `requirements.txt`, otherwise the package root, or workspace root if `python.rootRequirementsOnly` is enabled. + - Tasks will now inherit the correct venv paths in `PATH`. ## 1.30.3 diff --git a/crates/app/src/commands/ci.rs b/crates/app/src/commands/ci.rs index 297bde9508..a3f48cf1c5 100644 --- a/crates/app/src/commands/ci.rs +++ b/crates/app/src/commands/ci.rs @@ -175,7 +175,7 @@ fn distribute_targets_across_jobs( let job_index = args.job.unwrap_or_default(); let job_total = args.job_total.unwrap_or_default(); - let batch_size = (targets.len() + job_total - 1) / job_total; + let batch_size = targets.len().div_ceil(job_total); let batched_targets; console.print_header("Distributing targets across jobs")?; diff --git a/crates/toolchain/src/detect/languages.rs b/crates/toolchain/src/detect/languages.rs index 15d2781b7e..a31cab8df7 100644 --- a/crates/toolchain/src/detect/languages.rs +++ b/crates/toolchain/src/detect/languages.rs @@ -38,6 +38,7 @@ pub static PYTHON: StaticStringList = &[ "pyproject.toml", ".pylock.toml", ".python-version", + ".venv", // pip "Pipfile", "Pipfile.lock", diff --git a/crates/toolchain/src/detect/task_platform.rs b/crates/toolchain/src/detect/task_platform.rs index d294942854..60de6b03be 100644 --- a/crates/toolchain/src/detect/task_platform.rs +++ b/crates/toolchain/src/detect/task_platform.rs @@ -60,7 +60,7 @@ pub fn detect_task_platform(command: &str, enabled_platforms: &[PlatformType]) - } if PYTHON_COMMANDS - .get_or_init(|| Regex::new("^(python|python3|pip|pip3)$").unwrap()) + .get_or_init(|| Regex::new("^(python|python3|python-3|pip|pip3|pip-3)$").unwrap()) .is_match(command) { return use_platform_if_enabled(PlatformType::Python, enabled_platforms); diff --git a/legacy/python/platform/src/actions/install_deps.rs b/legacy/python/platform/src/actions/install_deps.rs index f41f7fa59f..9a4af1b9e1 100644 --- a/legacy/python/platform/src/actions/install_deps.rs +++ b/legacy/python/platform/src/actions/install_deps.rs @@ -1,10 +1,8 @@ use moon_action::Operation; use moon_console::{Checkpoint, Console}; -use moon_python_tool::PythonTool; +use moon_python_tool::{find_requirements_txt, PythonTool}; use std::path::Path; -use crate::find_requirements_txt; - pub async fn install_deps( python: &PythonTool, workspace_root: &Path, @@ -33,7 +31,7 @@ pub async fn install_deps( operations.push( Operation::task_execution(format!("python {}", args.join(" "))) - .track_async(|| python.exec_python(args, workspace_root)) + .track_async(|| python.exec_python(args, working_dir, workspace_root)) .await?, ); } @@ -60,7 +58,7 @@ pub async fn install_deps( operations.push( Operation::task_execution(format!("python {}", args.join(" "))) - .track_async(|| python.exec_python(args, working_dir)) + .track_async(|| python.exec_python(args, working_dir, workspace_root)) .await?, ); } diff --git a/legacy/python/platform/src/lib.rs b/legacy/python/platform/src/lib.rs index 3e1a597bf1..1b0dbc7911 100644 --- a/legacy/python/platform/src/lib.rs +++ b/legacy/python/platform/src/lib.rs @@ -3,10 +3,3 @@ mod python_platform; mod toolchain_hash; pub use python_platform::*; - -use starbase_utils::fs; -use std::path::{Path, PathBuf}; - -fn find_requirements_txt(starting_dir: &Path, workspace_root: &Path) -> Option { - fs::find_upwards_until("requirements.txt", starting_dir, workspace_root) -} diff --git a/legacy/python/platform/src/python_platform.rs b/legacy/python/platform/src/python_platform.rs index d276919260..1d4a93113d 100644 --- a/legacy/python/platform/src/python_platform.rs +++ b/legacy/python/platform/src/python_platform.rs @@ -1,4 +1,4 @@ -use crate::{actions, find_requirements_txt, toolchain_hash::PythonToolchainHash}; +use crate::{actions, toolchain_hash::PythonToolchainHash}; use moon_action::Operation; use moon_action_context::ActionContext; use moon_common::{path::is_root_level_source, Id}; @@ -12,7 +12,7 @@ use moon_platform::{Platform, Runtime, RuntimeReq}; use moon_process::Command; use moon_project::Project; use moon_python_lang::pip_requirements::load_lockfile_dependencies; -use moon_python_tool::{get_python_tool_paths, PythonTool}; +use moon_python_tool::{find_requirements_txt, get_python_tool_paths, PythonTool}; use moon_task::Task; use moon_tool::{get_proto_version_env, prepend_path_env_var, Tool, ToolManager}; use moon_utils::async_trait; @@ -294,16 +294,14 @@ impl Platform for PythonPlatform { if let Ok(python) = self.toolchain.get_for_version(&runtime.requirement) { if let Some(version) = get_proto_version_env(&python.tool) { - let cwd = if python.config.root_requirements_only { - self.workspace_root.as_path() - } else { - working_dir - }; - command.env("PROTO_PYTHON_VERSION", version); command.env( "PATH", - prepend_path_env_var(get_python_tool_paths(python, cwd)), + prepend_path_env_var(get_python_tool_paths( + python, + working_dir, + &self.workspace_root, + )), ); } } diff --git a/legacy/python/tool/src/python_tool.rs b/legacy/python/tool/src/python_tool.rs index 28dcaff599..f663932d99 100644 --- a/legacy/python/tool/src/python_tool.rs +++ b/legacy/python/tool/src/python_tool.rs @@ -10,16 +10,25 @@ use moon_toolchain::RuntimeReq; use proto_core::flow::install::InstallOptions; use proto_core::{Id, ProtoEnvironment, Tool as ProtoTool, UnresolvedVersionSpec}; use rustc_hash::FxHashMap; +use starbase_utils::fs; use std::path::PathBuf; use std::sync::Arc; use std::{ffi::OsStr, path::Path}; use tracing::instrument; -pub fn get_python_tool_paths(python_tool: &PythonTool, working_dir: &Path) -> Vec { - let venv_python = working_dir.join(&python_tool.config.venv_name); +pub fn find_requirements_txt(starting_dir: &Path, workspace_root: &Path) -> Option { + fs::find_upwards_until("requirements.txt", starting_dir, workspace_root) +} - if venv_python.exists() { - vec![venv_python.join("Scripts"), venv_python.join("bin")] +pub fn get_python_tool_paths( + python_tool: &PythonTool, + working_dir: &Path, + workspace_root: &Path, +) -> Vec { + if let Some(venv_root) = + fs::find_upwards_until(&python_tool.config.venv_name, working_dir, workspace_root) + { + vec![venv_root.join("Scripts"), venv_root.join("bin")] } else { get_proto_paths(&python_tool.proto_env) } @@ -68,7 +77,12 @@ impl PythonTool { } #[instrument(skip_all)] - pub async fn exec_python(&self, args: I, working_dir: &Path) -> miette::Result<()> + pub async fn exec_python( + &self, + args: I, + working_dir: &Path, + workspace_root: &Path, + ) -> miette::Result<()> where I: IntoIterator, S: AsRef, @@ -78,7 +92,7 @@ impl PythonTool { .envs(get_proto_env_vars()) .env( "PATH", - prepend_path_env_var(get_python_tool_paths(self, working_dir)), + prepend_path_env_var(get_python_tool_paths(self, working_dir, workspace_root)), ) .cwd(working_dir) .with_console(self.console.clone())