Use the root project name for the project virtual environment prompt (#7021)

Closes https://github.com/astral-sh/uv/issues/7001

Tested with a legacy virtual workspace and a project with a `[project]`
table.
This commit is contained in:
Zanie Blue 2024-09-04 10:16:31 -05:00 committed by GitHub
parent 1996067f81
commit a3a1bfd5ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 1 deletions

View file

@ -410,10 +410,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,

View file

@ -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(())
}