Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions crates/uv/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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