I want to add some dependencies to workspace.dependencies then add them to a certain package.I've been searching the internet but didn't find anything that could help.
Now I have to call cargo add xxx on a package, waiting cargo to generate the code, cutting the generated code and paste it to Cargo.toml file at workspace root, then add xxx.workspace = true back to the Cargo.toml file at that package. So I wonder if a command like cargo add xxx --workspace to add dependencies to workspace. Thanks.
keyko
October 26, 2025, 10:53am
2
opened 10:18PM - 27 Apr 22 UTC
C-feature-request
A-workspace-inheritance
Command-add
S-triage
### Problem
There is no way to tell `cargo add` that you want to add a depend… ency from a workspace explicitly. It is currently only done implicitly if you run `cargo add foo` and there happens to be a workspace dependency that matches it. There is also no way to add a dependency to a workspace and some of its members at the same time.
### Proposed Solution
add flags similar to `--inherit` and `--workspace foo,bar` that explicitly allows you to add a dependency from a workspace and add a dependency to a workspace and some of its members.
### Notes
This fell out of #10606. During the discussion of what should be included in cargo add support for workspace inheritance before workspace inheritance gets stabilized. The implementation of this should ideally not happen before stabilization of workspace inheritance.
`--inherit`:
This would be the main one to add a dependency from a workspace. Usage similar to `cargo add foo --workspace -p bar`, which would add `foo` to `bar` from a workspace. This could error if the workspace does not define that dependency or add it to the workspace if not found. `features` and `optional` are allowed to be used with this. `features` needs to be talked about as it might be good to add it to the workspace dependency features if the workspace dependency is not found. `optional` can only go to the inheriting package.
`--workspace foo,bar`
This would be to add a new workspace dependency to a workspace and then to the members that are specified, 1 member is required. Any flags does not conflict with `[workspace.dependencies]` would be allowed to be defined as well here and they would always go to the workspace dependency.
Is that what you mean? Not yet added if so.