Skip to content

Commit

Permalink
Avoid redundant members update in uv init (#5321)
Browse files Browse the repository at this point in the history
## Summary

If the path is already covered by `members`, we don't need to update it.

Closes #5320.
  • Loading branch information
charliermarsh committed Jul 23, 2024
1 parent 2f768b8 commit 41ca357
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ impl Workspace {
}
}

/// Returns `true` if the path is a workspace member.
pub fn includes(&self, project_path: &Path) -> bool {
self.packages
.values()
.any(|member| project_path == member.root())
}

/// Collect the workspace member projects from the `members` and `excludes` entries.
async fn collect_members(
workspace_root: PathBuf,
Expand Down
8 changes: 8 additions & 0 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ pub(crate) async fn init(
name.cyan(),
workspace.install_path().simplified_display().cyan()
)?;
} else if workspace.includes(&path) {
// If the member is already included in the workspace, skip the `members` addition.
writeln!(
printer.stderr(),
"Project `{}` is already a member of workspace `{}`",
name.cyan(),
workspace.install_path().simplified_display().cyan()
)?;
} else {
// Add the package to the workspace.
let mut pyproject = PyProjectTomlMut::from_toml(workspace.pyproject_toml())?;
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ fn init_matches_members() -> Result<()> {
----- stderr -----
warning: `uv init` is experimental and may change without warning
Adding `foo` as member of workspace `[TEMP_DIR]/`
Project `foo` is already a member of workspace `[TEMP_DIR]/`
Initialized project `foo` at `[TEMP_DIR]/packages/foo`
"###);

Expand All @@ -759,15 +759,15 @@ fn init_matches_members() -> Result<()> {
assert_snapshot!(
workspace, @r###"
[tool.uv.workspace]
members = ['packages/*', "packages/foo"]
members = ['packages/*']
"###
);
});

Ok(())
}

/// Run `uv init` from within a workspace. The path is already included via `members`.
/// Run `uv init` from within a workspace. The path is excluded via `exclude`.
#[test]
fn init_matches_exclude() -> Result<()> {
let context = TestContext::new("3.12");
Expand Down

0 comments on commit 41ca357

Please sign in to comment.