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

fix: only inherit workspace package table if the new package is a member #13261

Merged
Changes from 1 commit
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
Prev Previous commit
refactor: separate get_display_path_and_check_membership
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Jan 9, 2024
commit db4ed038c19eb59d865bb1f5ba2af1e8ba79ffd7
25 changes: 12 additions & 13 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,8 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
// This should not block the creation of the new project. It is only a best effort to
// inherit the workspace package keys.
if let Ok(mut workspace_document) = root_manifest.parse::<toml_edit::Document>() {
let (display_path, can_be_a_member) = get_display_path_and_check_membership(
&root_manifest_path,
&path,
&workspace_document,
)?;
let display_path = get_display_path(&root_manifest_path, &path)?;
let can_be_a_member = can_be_workspace_member(&display_path, &workspace_document)?;
// Only try to inherit the workspace stuff if the new package can be a member of the workspace.
if can_be_a_member {
if let Some(workspace_package_keys) = workspace_document
Expand Down Expand Up @@ -1003,11 +1000,7 @@ fn update_manifest_with_new_member(
)
}

fn get_display_path_and_check_membership(
root_manifest_path: &Path,
package_path: &Path,
workspace_document: &toml_edit::Document,
) -> CargoResult<(String, bool)> {
fn get_display_path(root_manifest_path: &Path, package_path: &Path) -> CargoResult<String> {
// Find the relative path for the package from the workspace root directory.
let workspace_root = root_manifest_path.parent().with_context(|| {
format!(
Expand All @@ -1031,7 +1024,14 @@ fn get_display_path_and_check_membership(
components.push(comp);
}
let display_path = components.join("/");
Ok(display_path)
}

// Check if the package can be a member of the workspace.
fn can_be_workspace_member(
display_path: &str,
workspace_document: &toml_edit::Document,
) -> CargoResult<bool> {
if let Some(exclude) = workspace_document
.get("workspace")
.and_then(|workspace| workspace.get("exclude"))
Expand All @@ -1042,10 +1042,9 @@ fn get_display_path_and_check_membership(
.as_str()
.with_context(|| format!("invalid non-string exclude path `{}`", member))?;
if pat == display_path {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this problem comes from here. Shouldn't we consider glob here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to #6009, we don't support globs on exclude...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the problem with globs or that exclude is a path prefix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to #6009, we don't support globs on exclude...

Oh, I forgot that 🤣

Is the problem with globs or that exclude is a path prefix?

No. It works well.

return Ok((display_path, false));
return Ok(false);
}
}
}

Ok((display_path, true))
Ok(true)
}