Skip to content

Commit

Permalink
Fix venv paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 7, 2024
1 parent 78c1689 commit 1ca1254
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion crates/app/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down
1 change: 1 addition & 0 deletions crates/toolchain/src/detect/languages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub static PYTHON: StaticStringList = &[
"pyproject.toml",
".pylock.toml",
".python-version",
".venv",
// pip
"Pipfile",
"Pipfile.lock",
Expand Down
2 changes: 1 addition & 1 deletion crates/toolchain/src/detect/task_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions legacy/python/platform/src/actions/install_deps.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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?,
);
}
Expand All @@ -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?,
);
}
Expand Down
7 changes: 0 additions & 7 deletions legacy/python/platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
fs::find_upwards_until("requirements.txt", starting_dir, workspace_root)
}
16 changes: 7 additions & 9 deletions legacy/python/platform/src/python_platform.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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,
)),
);
}
}
Expand Down
26 changes: 20 additions & 6 deletions legacy/python/tool/src/python_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
let venv_python = working_dir.join(&python_tool.config.venv_name);
pub fn find_requirements_txt(starting_dir: &Path, workspace_root: &Path) -> Option<PathBuf> {
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<PathBuf> {
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)
}
Expand Down Expand Up @@ -68,7 +77,12 @@ impl PythonTool {
}

#[instrument(skip_all)]
pub async fn exec_python<I, S>(&self, args: I, working_dir: &Path) -> miette::Result<()>
pub async fn exec_python<I, S>(
&self,
args: I,
working_dir: &Path,
workspace_root: &Path,
) -> miette::Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
Expand All @@ -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())
Expand Down

0 comments on commit 1ca1254

Please sign in to comment.