Create member pyproject.toml prior to workspace discovery (#5317)

## Summary

Otherwise, if the path is already a member, discovery fails.

Also adds a failing test for "adding members that are already covered by
`members`".
This commit is contained in:
Charlie Marsh 2024-07-22 20:03:53 -04:00 committed by GitHub
parent d232bfea00
commit 13dd8853d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 60 additions and 18 deletions

View file

@ -6,7 +6,7 @@ use owo_colors::OwoColorize;
use pep508_rs::PackageName;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
use uv_fs::{absolutize_path, Simplified};
use uv_warnings::warn_user_once;
use uv_workspace::pyproject_mut::PyProjectTomlMut;
use uv_workspace::{Workspace, WorkspaceError};
@ -59,24 +59,8 @@ pub(crate) async fn init(
);
}
// Create the directory for the project.
let src_dir = path.join("src").join(&*name.as_dist_info_name());
fs_err::create_dir_all(&src_dir)?;
// Canonicalize the path to the project.
let path = path.canonicalize()?;
// Discover the current workspace, if it exists.
let workspace = if isolated {
None
} else {
// Attempt to find a workspace root.
match Workspace::discover(&path, None).await {
Ok(workspace) => Some(workspace),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(err) => return Err(err.into()),
}
};
let path = absolutize_path(&path)?;
// Create the `pyproject.toml`.
let pyproject = indoc::formatdoc! {r#"
@ -92,11 +76,27 @@ pub(crate) async fn init(
readme = if no_readme { "" } else { "\nreadme = \"README.md\"" },
};
fs_err::create_dir_all(&path)?;
fs_err::write(path.join("pyproject.toml"), pyproject)?;
// Discover the current workspace, if it exists.
let workspace = if isolated {
None
} else {
// Attempt to find a workspace root.
let parent = path.parent().expect("Project path has no parent");
match Workspace::discover(parent, None).await {
Ok(workspace) => Some(workspace),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(err) => return Err(err.into()),
}
};
// Create `src/{name}/__init__.py` if it does not already exist.
let src_dir = path.join("src").join(&*name.as_dist_info_name());
let init_py = src_dir.join("__init__.py");
if !init_py.try_exists()? {
fs_err::create_dir_all(&src_dir)?;
fs_err::write(
init_py,
indoc::formatdoc! {r#"

View file

@ -724,3 +724,45 @@ fn init_virtual_workspace() -> Result<()> {
Ok(())
}
/// Run `uv init` from within a workspace. The path is already included via `members`.
#[test]
fn init_matches_member() -> Result<()> {
let context = TestContext::new("3.12");
let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {
r"
[tool.uv.workspace]
members = ['packages/*']
",
})?;
let packages = context.temp_dir.join("packages");
fs_err::create_dir_all(packages)?;
uv_snapshot!(context.filters(), context.init().current_dir(context.temp_dir.join("packages")).arg("foo"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv init` is experimental and may change without warning
Adding `foo` as member of workspace `[TEMP_DIR]/`
Initialized project `foo` at `[TEMP_DIR]/packages/foo`
"###);
let workspace = fs_err::read_to_string(context.temp_dir.join("pyproject.toml"))?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
workspace, @r###"
[tool.uv.workspace]
members = ['packages/*', "packages/foo"]
"###
);
});
Ok(())
}