-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Feature Request: cargo new --in-workspace [directory] [name] #6378
Comments
I've ran into "compiling this new crate may not work" error a few times, too. It's a bit silly that Cargo makes a non-working crate, knows it, even knows how to fix it, but just leaves it broken. To me it would make most sense if Cargo just automatically added the new crate to the |
That sounds pretty reasonable, and the only way I could see that breaking anything is if someone is depending on using |
There's a reverse problem as well, exemplified by this thread: https://salsa.zulipchat.com/#narrow/stream/145099-general/topic/revamped.20API/near/155324150 If a user creates, via I think it's clear now that workspaces are the default mode for working on multi-crate projects. I suggest the following change to
That is, |
What is the progress here? I would really like this feature. |
I'm no longer using rust, so I'm not interested in contributing to fixing this. But if anyone is willing to take the torch on this, I think it would be beneficial to the rust community (or, at least to the parts of the rust community that like modular code) |
Now that I think about this more, I'm coming to the conclusion that adding a flag to |
There are 3 options here:
AFAIK 2 creates a create that won't work, so it's broken. Option 3 is asking whether you want a broken crate or a non-broken crate, which is absurd (or a very very edge case at best). So that leaves 1 as the only sensible option? |
But 1 removes backwards compatibility and makes automation hard. I do agree, if we were doing this from the ground up we should do the first. |
Why would any existing users rely on When do you need to automate creation of unusable crates? |
It's still completely usable. It's just not part of the workspace. Try it in a local repository. It works fine, just there are two |
So let's say that
|
Are you sure you're reproducing the problem correctly? Two crates can be nested if there is no workspace involved. But as soon as you create a workspace at a higher level, cargo new level1
cd level1
echo "[workspace]" >> Cargo.toml
cargo new level2
cd level2
cargo test
|
Hello , I got the error described in this issue when I follow the book there https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html , but with 3 projects instead of one : Imagine you have the following [workspace]
members = [
"client",
"server",
"shared"
] Then
-> No error I understand the error is thrown because cargo tries to read the other crates ( They are not yet created ) so we get error . That is very logical. But : Is this the expected behavior while creating a workspace? Thank you in advance. I am a baby Rustacean, so maybe my questions are not smart :P , so you can tell me :D |
A workaround: add the crate name to the workspace member list before |
That's not really helpful, since this ticket is a feature request for not having to use that workaround. |
So I have started looking at how to implement this feature, and in my opinion it would be quite awkward to do given the current code structure Current code structure issue
Importantly, all errors are The logical place to put code that add the newly created package to the pre-existing workspace would be in Possible remediesI am not sure how to move forward on this issue. Possible solutions that come to mind are:
Appendixcargo/src/cargo/ops/cargo_new.rs Lines 708 to 847 in ad50d0d
|
I think it would be good to start using precise error enums in Cargo, at least in "leaf" functions. For another feature I needed to handle network errors, and I had to downcast Anyhow errors, which didn't feel elegant. |
I support this feature request for the sake of having some sort of suggested default to organize workspaces. Let's look at three of the most popular game engines written in Rust:
It's just a matter of opening |
cargo has now switched from Considerations from lessons learned from cargo-add
|
We talked about this issue today in the cargo team meeting. Particularly with closing #12360, we are interested in removing the Some potential solutions
The general feel I had from the cargo team is that we prefer to opt-in by default
On a related note, should we support a flag to explicitly say where the workspace is for when it can't be auto-discovered? Personally, I would consider being nested under the workspace where it can be auto-detected the golden path and we don't need to be going out of our way to streamline manually discovered workspaces. A part of me wants this to be:
However, I'm concerned that is a little too magical but that is also a future problem... |
I think it's necessary for good ergonomics that such feature won't interfere with existing wildcards in |
Sorry that I didn't make it clear but my assumption was only to add if needed. |
We talked about this in the cargo team meeting. We are fine moving forward with We are fine with deferring an opt-out. Designing the opt-out will likely require enough of the design of #8365 to make sure they don't cause each other problems. In finalizing the PR for this Issue, I would recommend creating a separate issue for us to track opt-out support. The relevant section of code starts here. We can load the workspace with toml_edit and edit |
I'm going to take a stab at adding the new package to the members list. @rustbot claim |
The problem
Imagine you're working in a project called
foo
and it's a workspace. You want to create a new member,bar
, as a subdirectory offoo
. If you usecargo new bar
(ormkdir bar && cd bar && cargo init
) you'll get warnings like this:Ideally, that manual work should be automated (if the user wants it).
There's more manual work if you don't want subdirectories. For example, if
foo
is incode/foo
and you want to makebar
incode/bar
, then you have to change theCargo.toml
files in both directories after usingcargo new
The solution
In the case of creating
bar
as a subdirectory, you'dcd
to the workspace root and usecargo new --in-workspace . bar
andfoo/Cargo.toml
would be updated to includebar
in its[workspace]
section. In the case of creatingbar
outside of the root, you'd usecargo new --in-workspace ../foo bar
as an example. Similar capabilities should also be given tocargo init
for consistencyIf the directory passed to
--in-workspace
is not a workspace, then there would be an error.I've never contributed to cargo before (and am pretty new to Rust) but I don't think this is too complicated, and if the idea is accepted I'd love to try and implement it. The changes would take place in this file. Add a boolean to NewOptions, change its constructor, update this behavior to change the
workspaces
section of the root'sCargo.toml
(and the child'spackage.workspace
if the child isn't a subdirectory of the root), and it'll be mostly done.Notes
This is not a breaking change. The new behavior only goes into effect when a new argument (
--in-workspace
) is specified.Relevant reading: the original RFC
Alternative ideas:
cargo new --root [directory] [name]
cargo new --member [name]
- strictly for subdirectories (only addressesfoo/bar
example; does not address the issue of creatingcode/bar
when your root iscode/foo
)The text was updated successfully, but these errors were encountered: