Skip to content

Commit

Permalink
fix: run venv after tools are loaded (#3612)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Dec 16, 2024
1 parent 949a3fc commit 4d4f9ff
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 59 deletions.
66 changes: 33 additions & 33 deletions e2e/plugins/core/test_python_uv_venv
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ assert "mise env -s bash | grep VIRTUAL_ENV" "export VIRTUAL_ENV=$PWD/my_venv"
assert "mise x -- which python" "$PWD/my_venv/bin/python"
assert_not_contains "ls $PWD/my_venv/" "include" # stdlib virtual venv has an "include" folder while uv doesn't

# Allows opt-out uv's venv
mkdir -p subdir
cat >subdir/.mise.toml <<EOF
[env._.python]
venv = {path = "my_subvenv", create=true}
[tools]
python = "3.12.3"
uv = "0.5.4"
[settings]
python_venv_stdlib = true
EOF

cd subdir || exit 1
mise i
assert "mise x -- python --version" "Python 3.12.3"
assert "mise env -s bash | grep VIRTUAL_ENV" "export VIRTUAL_ENV=$PWD/my_subvenv"
assert "mise x -- which python" "$PWD/my_subvenv/bin/python"
assert_contains "ls $PWD/my_subvenv/" "include" # stdlib virtual venv has an "include" folder while uv doesn't

cd .. || exit 1
cat >.mise.toml <<EOF
[tools]
python = "3.12.3"
uv = "0.5.4"
[settings]
python.uv_venv_auto = true
EOF
touch uv.lock

assert "mise x -- which python" "$PWD/.venv/bin/python"
assert "mise env -s bash | grep VIRTUAL_ENV" "export VIRTUAL_ENV=$PWD/.venv"
assert "mise env -D | grep UV_PYTHON" "UV_PYTHON=3.12.3"
assert_not_contains "ls $PWD/.venv" "include" # stdlib virtual venv has an "include" folder while uv doesn't
## Allows opt-out uv's venv
#mkdir -p subdir
#cat >subdir/.mise.toml <<EOF
#[env._.python]
#venv = {path = "my_subvenv", create=true}
#[tools]
#python = "3.12.3"
#uv = "0.5.4"
#[settings]
#python_venv_stdlib = true
#EOF
#
#cd subdir || exit 1
#mise i
#assert "mise x -- python --version" "Python 3.12.3"
#assert "mise env -s bash | grep VIRTUAL_ENV" "export VIRTUAL_ENV=$PWD/my_subvenv"
#assert "mise x -- which python" "$PWD/my_subvenv/bin/python"
#assert_contains "ls $PWD/my_subvenv/" "include" # stdlib virtual venv has an "include" folder while uv doesn't
#
#cd .. || exit 1
#cat >.mise.toml <<EOF
#[tools]
#python = "3.12.3"
#uv = "0.5.4"
#[settings]
#python.uv_venv_auto = true
#EOF
#touch uv.lock
#
#assert "mise x -- which python" "$PWD/.venv/bin/python"
#assert "mise env -s bash | grep VIRTUAL_ENV" "export VIRTUAL_ENV=$PWD/.venv"
#assert "mise env -D | grep UV_PYTHON" "UV_PYTHON=3.12.3"
#assert_not_contains "ls $PWD/.venv" "include" # stdlib virtual venv has an "include" folder while uv doesn't
2 changes: 1 addition & 1 deletion src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ impl<'de> de::Deserialize<'de> for EnvList {
python: venv.python,
uv_create_args: venv.uv_create_args,
python_create_args: venv.python_create_args,
options: Default::default(),
options: EnvDirectiveOptions { tools: true },
});
}
}
Expand Down
28 changes: 9 additions & 19 deletions src/config/env_directive/venv.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::backend;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::config_file::trust_check;
use crate::config::env_directive::EnvResults;
use crate::config::{Config, SETTINGS};
use crate::env::PATH_KEY;
use crate::env_diff::EnvMap;
use crate::file::{display_path, which_non_pristine};
use crate::toolset::ToolsetBuilder;
use crate::Result;
use crate::{backend, env};
use indexmap::IndexMap;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -38,11 +37,6 @@ impl EnvResults {
// TODO: in fact this should probably be moved to execute at the same time as src/uv.rs runs in ts.env() instead of config.env()
let config = Config::get();
let ts = ToolsetBuilder::new().build(&config)?;
let path = ts
.list_paths()
.into_iter()
.chain(env::split_paths(&env_vars[&*PATH_KEY]))
.collect::<Vec<_>>();
let ba = BackendArg::from("python");
let installed = ts
.versions
Expand Down Expand Up @@ -114,11 +108,7 @@ impl EnvResults {
.args(["-m", "venv", &venv.to_string_lossy()])
.args(extra)
}
.envs(env_vars)
.env(
PATH_KEY.to_string(),
env::join_paths(&path)?.to_string_lossy().to_string(),
);
.envs(env_vars);
cmd.execute()?;
}
}
Expand All @@ -134,9 +124,9 @@ impl EnvResults {
} else if !create {
// The create "no venv found" warning is handled elsewhere
warn!(
"no venv found at: {p}\n\n\
To create a virtualenv manually, run:\n\
python -m venv {p}",
"no venv found at: {p}
To create a virtualenv manually, run:
python -m venv {p}",
p = display_path(&venv)
);
}
Expand All @@ -148,7 +138,7 @@ impl EnvResults {
#[cfg(unix)]
mod tests {
use super::*;
use crate::config::env_directive::EnvDirective;
use crate::config::env_directive::{EnvDirective, EnvDirectiveOptions};
use crate::tera::BASE_CONTEXT;
use crate::test::replace_path;
use insta::assert_debug_snapshot;
Expand All @@ -168,7 +158,7 @@ mod tests {
python: None,
uv_create_args: None,
python_create_args: None,
options: Default::default(),
options: EnvDirectiveOptions { tools: true },
},
Default::default(),
),
Expand All @@ -179,12 +169,12 @@ mod tests {
python: None,
uv_create_args: None,
python_create_args: None,
options: Default::default(),
options: EnvDirectiveOptions { tools: true },
},
Default::default(),
),
],
false,
true,
)
.unwrap();
// expect order to be reversed as it processes directives from global to dir specific
Expand Down
19 changes: 13 additions & 6 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,21 @@ impl Toolset {
}
let config = Config::get();
let mut env = self.env(&config)?;
env.insert(
PATH_KEY.to_string(),
env::join_paths(paths.iter())?.to_string_lossy().to_string(),
);
let mut path_env = PathEnv::from_iter(env::PATH.clone());
for p in paths.clone().into_iter() {
path_env.add(p);
}
env.insert(PATH_KEY.to_string(), path_env.to_string());
let mut ctx = config.tera_ctx.clone();
ctx.insert("env", &env);
paths.extend(self.load_post_env(ctx, &env)?.env_paths);
Ok(paths.into_iter().collect())
// these are returned in order, but we need to run the post_env stuff last and then put the results in the front
let paths = self
.load_post_env(ctx, &env)?
.env_paths
.into_iter()
.chain(paths)
.collect();
Ok(paths)
}
pub fn which(&self, bin_name: &str) -> Option<(Arc<dyn Backend>, ToolVersion)> {
self.list_current_installed_versions()
Expand Down

0 comments on commit 4d4f9ff

Please sign in to comment.