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

Automatically inherit workspace fields when running cargo new #10870

Closed
epage opened this issue Jul 15, 2022 · 9 comments · Fixed by #12069
Closed

Automatically inherit workspace fields when running cargo new #10870

epage opened this issue Jul 15, 2022 · 9 comments · Fixed by #12069
Assignees
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-new E-medium Experience: Medium S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@epage
Copy link
Contributor

epage commented Jul 15, 2022

Problem

When I create a new workspace, cargo new populates some fields but leaves me to fill in the rest. If I run it within a workspace with workspace.package fields set, I most likely intend to inherit those in this new crate as well.

Proposed Solution

When cargo new is run, search up for a possible workspace. If one is found, check which workspace.package fields are set and then in the new package, set those fields to <field>.workspace = true.

Notes

See #10859

I don't think this is blocked on #6378 because cargo new in a workspace that uses globs in members will automatically pick up the new crate and doesn't need #6378 to take advantage of this.

@epage epage added Command-new C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` labels Jul 15, 2022
@Googleton
Copy link

Hi, just to make sure I understand this issue before possibly tackling it:

I have a top level workspace 'my-workspace' which has this in its Cargo.toml:

(...)
[workspace.package]
description = "hello world"

I then run cargo new my-package (in the same directory as my-workspace's Cargo.toml).
The expected content of the newly created Cargo.toml:

[package]
name = "my-package"
version = "0.1.0"
edition = "2021"
description.workspace = true

Of course, assuming we have the workspace inheritence feature enabled.

@epage
Copy link
Contributor Author

epage commented Jul 18, 2022

The expected content of the newly created Cargo.toml:

Yes

We have to make sure fields like edition are overwritten rather than duplicated (a risk if simple string manipulation is used).

I have a slight preference for the order of the fields in workspace.package to be mirrored in package.

Of course, assuming we have the workspace inheritence feature enabled.

I'd just wait until #10859 is merged. We are halfway done with the FCP which is blocking the stablization

@Googleton
Copy link

Thank you for the information.

I will be trying to do it while waiting for the merge, as a way to dig into the cargo codebase. I will keep your concerns/preferences in mind, it should make for a fun learning experience!

@epage
Copy link
Contributor Author

epage commented Jul 18, 2022

Tips

@Googleton
Copy link

To load the workspace, I was looking here: https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/cargo_new.rs#L844-L851

It seemed to me like this would be the correct place: If the result is not an Err, we have a proper workspace and can go from there to get InheritedFields and then write them into the newly created Cargo.toml

Again, thank you for the advice :)

@SummerGram
Copy link

Why did it open again?

@Rustin170506
Copy link
Member

@rustbot claim

@weihanglo weihanglo added S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review and removed E-help-wanted labels Apr 19, 2023
@ehuss
Copy link
Contributor

ehuss commented May 1, 2023

Just to be clear, will this only be done if the workspace.members has a glob that will pick up the new package?

@epage
Copy link
Contributor Author

epage commented May 1, 2023

For now, yes. #6378 is for adding it to the workspace. glob support I think is common enough to not block one of these on the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` Command-new E-medium Experience: Medium S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants