mirror of
https://github.com/astral-sh/uv.git
synced 2025-11-01 20:31:12 +00:00
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:
parent
1996067f81
commit
a3a1bfd5ec
2 changed files with 59 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue