Skip to content

Commit

Permalink
Add --prompt support to uv venv (astral-sh#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Feb 25, 2024
1 parent ae22370 commit d8d0314
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ _Unreleased_

- Added latest Python builds. #771

- When `uv` is used the prompt is now set to the project name. #773

<!-- released start -->

## 0.26.0
Expand Down
2 changes: 1 addition & 1 deletion rye/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn ensure_self_venv_with_toolchain(

// initialize the virtualenv
{
let uv_venv = uv.venv(&venv_dir, &py_bin, &version)?;
let uv_venv = uv.venv(&venv_dir, &py_bin, &version, None)?;
// write our marker
uv_venv.write_marker()?;
// update pip and our requirements
Expand Down
2 changes: 1 addition & 1 deletion rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub fn create_virtualenv(
// try to kill the empty venv if there is one as uv can't work otherwise.
fs::remove_dir(venv).ok();
let uv = Uv::ensure_exists(output.quieter())?
.venv(venv, &py_bin, py_ver)
.venv(venv, &py_bin, py_ver, Some(prompt))
.context("failed to initialize virtualenv")?;
uv.write_marker()?;
uv.sync_marker();
Expand Down
26 changes: 13 additions & 13 deletions rye/src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ impl Uv {
venv_dir: &Path,
py_bin: &Path,
version: &PythonVersion,
prompt: Option<&str>,
) -> Result<UvWithVenv, Error> {
let status = self
.cmd()
.arg("venv")
.arg("--python")
.arg(py_bin)
.arg(venv_dir)
.status()
.with_context(|| {
format!(
"unable to create self venv using {}. It might be that \
let mut cmd = self.cmd();
cmd.arg("venv").arg("--python").arg(py_bin);
if let Some(prompt) = prompt {
cmd.arg("--prompt").arg(prompt);
}
cmd.arg(venv_dir);
let status = cmd.status().with_context(|| {
format!(
"unable to create self venv using {}. It might be that \
the used Python build is incompatible with this machine. \
For more information see https://rye-up.com/guide/installation/",
py_bin.display()
)
})?;
py_bin.display()
)
})?;

if !status.success() {
return Err(anyhow!(
Expand Down
5 changes: 5 additions & 0 deletions rye/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ impl Space {
&self.project_dir
}

#[allow(unused)]
pub fn venv_path(&self) -> PathBuf {
self.project_dir.join(".venv")
}

#[allow(unused)]
pub fn lock_rye_home(&self) -> fslock::LockFile {
let mut lock = fslock::LockFile::open(&self.rye_home().join("lock")).unwrap();
Expand Down
16 changes: 16 additions & 0 deletions rye/tests/test_sync.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs;

use crate::common::{rye_cmd_snapshot, Space};

mod common;
Expand Down Expand Up @@ -26,6 +28,20 @@ fn test_empty_sync() {
Installed 1 package in [EXECUTION_TIME]
+ my-project==0.1.0 (from file:[TEMP_PATH]/project)
"###);

// is the prompt set?
#[cfg(unix)]
{
let script = space.venv_path().join("bin/activate");
let contents = fs::read_to_string(script).unwrap();
assert!(contents.contains("VIRTUAL_ENV_PROMPT=\"my-project\""));
}
#[cfg(windows)]
{
let script = space.venv_path().join("Scripts/activate.bat");
let contents = fs::read_to_string(script).unwrap();
assert!(contents.contains("@set \"VIRTUAL_ENV_PROMPT=my-project\""));
}
}

#[test]
Expand Down

0 comments on commit d8d0314

Please sign in to comment.