Skip to content

Commit

Permalink
Fix rye list overwrites .venv (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
ischaojie authored Mar 29, 2024
1 parent 4dea727 commit 09c8e07
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions rye/src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion rye/src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
58 changes: 58 additions & 0 deletions rye/tests/test-list.rs
Original file line number Diff line number Diff line change
@@ -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 -----
"###);
}

0 comments on commit 09c8e07

Please sign in to comment.