From ed67289eeec0d21b43967ca050c6257c7d0bdde2 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Wed, 4 Sep 2024 09:02:03 -0500 Subject: [PATCH] Use the root project name for the project virtual environment prompt --- crates/uv/src/commands/project/mod.rs | 21 ++++++++++++++- crates/uv/tests/sync.rs | 39 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index c5b10fa327b7..bcfc6de043b5 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -401,10 +401,29 @@ pub(crate) async fn get_or_init_environment( venv.user_display().cyan() )?; + // Determine a prompt for the environment, in order of preference: + // + // 1) The name of the project + // 2) The name of the directory at the root of the workspace + // 3) No prompt + let prompt = workspace + .pyproject_toml() + .project + .as_ref() + .map(|p| p.name.to_string()) + .or_else(|| { + workspace + .install_path() + .file_name() + .map(|f| f.to_string_lossy().to_string()) + }) + .map(uv_virtualenv::Prompt::Static) + .unwrap_or(uv_virtualenv::Prompt::None); + Ok(uv_virtualenv::create_venv( &venv, interpreter, - uv_virtualenv::Prompt::None, + prompt, false, false, false, diff --git a/crates/uv/tests/sync.rs b/crates/uv/tests/sync.rs index c650e2837ff3..2c1c0491ea0b 100644 --- a/crates/uv/tests/sync.rs +++ b/crates/uv/tests/sync.rs @@ -1891,3 +1891,42 @@ fn sync_virtual_env_warning() -> Result<()> { Ok(()) } + +#[test] +fn sync_environment_prompt() -> Result<()> { + let context = TestContext::new_with_versions(&["3.12"]); + + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#" + [project] + name = "my-project" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["iniconfig"] + "#, + )?; + + // Running `uv sync` should create `.venv` + uv_snapshot!(context.filters(), context.sync(), @r###" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Using Python 3.12.[X] interpreter at: [PYTHON-3.12] + Creating virtualenv at: .venv + Resolved 2 packages in [TIME] + Prepared 1 package in [TIME] + Installed 1 package in [TIME] + + iniconfig==2.0.0 + "###); + + // The `pyvenv.cfg` should contain the prompt matching the project name + let pyvenv_cfg = + fs_err::read_to_string(context.temp_dir.join(".venv").join("pyvenv.cfg")).unwrap(); + + assert!(pyvenv_cfg.contains("prompt = my-project")); + + Ok(()) +}