From 09c8e07abd9e274cd049d67e188952046de6892e Mon Sep 17 00:00:00 2001 From: Chaojie Date: Fri, 29 Mar 2024 17:21:26 +0800 Subject: [PATCH] Fix rye list overwrites .venv (#942) --- CHANGELOG.md | 3 +++ rye/src/cli/list.rs | 12 ++++++--- rye/src/uv.rs | 2 +- rye/tests/test-list.rs | 58 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 rye/tests/test-list.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4df5342fe5..28e6848a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ _Unreleased_ - Always create `.gitignore` file in `rye init`. #919 +- `rye list` always prints the currently installed packages even this project is not managed by Rye. #940 + +- Fix error on using -v or -q with `rye fmt` or `rye lint`. #959 - Fix error on using -v or -q with `rye fmt` or `rye lint`. #959 - Fix rye fetch detection of registered toolchain. #931 diff --git a/rye/src/cli/list.rs b/rye/src/cli/list.rs index 150cbb4d07..1620b1e289 100644 --- a/rye/src/cli/list.rs +++ b/rye/src/cli/list.rs @@ -9,7 +9,7 @@ use crate::config::Config; use crate::consts::VENV_BIN; use crate::pyproject::PyProject; use crate::utils::{get_venv_python_bin, CommandOutput}; -use crate::uv::UvBuilder; +use crate::uv::{UvBuilder, UvWithVenv}; /// Prints the currently installed packages. #[derive(Parser, Debug)] @@ -28,16 +28,20 @@ pub fn execute(cmd: Args) -> Result<(), Error> { let self_venv = ensure_self_venv(CommandOutput::Normal)?; if Config::current().use_uv() { - UvBuilder::new() + let uv = UvBuilder::new() .with_output(CommandOutput::Normal) - .ensure_exists()? - .venv( + .ensure_exists()?; + if !project.rye_managed() { + UvWithVenv::new(uv, &project.venv_path(), &project.venv_python_version()?).freeze()?; + } else { + uv.venv( &project.venv_path(), &python, &project.venv_python_version()?, None, )? .freeze()?; + } } else { let status = Command::new(self_venv.join(VENV_BIN).join("pip")) .arg("--python") diff --git a/rye/src/uv.rs b/rye/src/uv.rs index aa762f3102..336d024b44 100644 --- a/rye/src/uv.rs +++ b/rye/src/uv.rs @@ -359,7 +359,7 @@ pub struct UvWithVenv { } impl UvWithVenv { - fn new(uv: Uv, venv_dir: &Path, version: &PythonVersion) -> Self { + pub fn new(uv: Uv, venv_dir: &Path, version: &PythonVersion) -> Self { UvWithVenv { uv, py_version: version.clone(), diff --git a/rye/tests/test-list.rs b/rye/tests/test-list.rs new file mode 100644 index 0000000000..1c7a55fadb --- /dev/null +++ b/rye/tests/test-list.rs @@ -0,0 +1,58 @@ +use crate::common::{rye_cmd_snapshot, Space}; +use toml_edit::value; + +mod common; + +#[test] +fn test_basic_list() { + let space = Space::new(); + space.init("my-project"); + + space + .rye_cmd() + .arg("add") + .arg("jinja2") + .status() + .expect("ok"); + + rye_cmd_snapshot!( + space.rye_cmd().arg("list"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + jinja2==3.1.2 + markupsafe==2.1.3 + -e file:[TEMP_PATH]/project + + ----- stderr ----- + "###); +} + +#[test] +fn test_list_not_rye_managed() { + let space = Space::new(); + space.init("my-project"); + + space.edit_toml("pyproject.toml", |doc| { + doc["tool"]["rye"]["managed"] = value(false); + }); + + space + .rye_cmd() + .arg("add") + .arg("jinja2") + .status() + .expect("Add package failed"); + + rye_cmd_snapshot!( + space.rye_cmd().arg("list"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + jinja2==3.1.2 + markupsafe==2.1.3 + -e file:[TEMP_PATH]/project + + ----- stderr ----- + "###); +}