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

Avoid redundant members update in uv init #5321

Merged
merged 1 commit into from
Jul 23, 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
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
Loading