Skip to content

Commit

Permalink
Avoid redundant members update in uv init
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 23, 2024
1 parent 8958599 commit fbb27a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 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
4 changes: 2 additions & 2 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,7 +759,7 @@ fn init_matches_members() -> Result<()> {
assert_snapshot!(
workspace, @r###"
[tool.uv.workspace]
members = ['packages/*', "packages/foo"]
members = ['packages/*']
"###
);
});
Expand Down

0 comments on commit fbb27a3

Please sign in to comment.