Skip to content

Feature Request: cargo new --in-workspace [directory] [name] #6378

Closed
@jamescostian

Description

@jamescostian

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 of foo. If you use cargo new bar (or mkdir bar && cd bar && cargo init) you'll get warnings like this:

warning: compiling this new crate may not work due to invalid workspace configuration

current package believes it's in a workspace when it's not:
current:   [new project's Cargo.toml]
workspace: [encompassing workspace's Cargo.toml]

this may be fixable by ensuring that this crate is depended on by the workspace root: 
[encompassing workspace's Cargo.toml]

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 in code/foo and you want to make bar in code/bar, then you have to change the Cargo.toml files in both directories after using cargo new

The solution

In the case of creating bar as a subdirectory, you'd cd to the workspace root and use cargo new --in-workspace . bar and foo/Cargo.toml would be updated to include bar in its [workspace] section. In the case of creating bar outside of the root, you'd use cargo new --in-workspace ../foo bar as an example. Similar capabilities should also be given to cargo init for consistency

If 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's Cargo.toml (and the child's package.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 addresses foo/bar example; does not address the issue of creating code/bar when your root is code/foo)

Metadata

Metadata

Assignees

Labels

C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`Command-newS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions