-
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
add a flag to cargo add
to put dependency in workspace and inherit it
#10608
Comments
Right now, you can explicitly name the source for a dependency (
But there is no way to explicitly state you want a workspace dependency (and have it error if it doesn't exist). There is also no way to explicitly add a dependency to Some questions I have
|
imo I find it confusing that you pass package names through the |
I thought about suggesting |
imo there isn't really a reason to specify multiple members at once. We don't have any other way of adding dependencies to multiple packages. The only case I can think of is an option to migrate a workspace to workspace dependencies but since that is "one time", I don't see as much value. |
In that case, do we go with |
Could you clarify how the flags differ? I feel like that wasn't too clear to me |
I think there should be two different flags one for inheriting a workspace dependency and one for adding a dependency to a workspace and then inheriting it (it would also just be for adding a dependency to a workspace or overriding one).
Removing setting members for |
I created an issue today proposing similar functionality, but it was closed in favor of this one (wasn't aware I was making a duplicate). Just going to present my ideas to further the conversation and to help eventually determine what the best approach would be. My proposal was as follows: Adding to
|
I think a
I'm not a fan of |
cargo add
for inheriting from a workspacecargo add
to put dependency in workspace and inherit it
In my experience, it seems people are all or nothing about inheriting from a workspace. We could
I feel like inheriting non-source information was a mistake so I think we should only put that in the workspace. |
To add real quick, we have several potential heuristic ideas
|
I don't think we need new flags: if someone specifies both But, in a monorepo, ideally my partners don't have to specify anything differently. A lot of this discussion is about trying to avoid regressions. I appreciate that. So instead, why not have monorepo maintainers opt into this behavior via [dependencies]
inherit = true If present and the workspace indeed has |
I agree about not having a new flag for this. There is a UX and maintenance cost to any flag or config we add. I think we can go far with heuristics. I don't think we should be doing heuristics off of As I hinted before, I don't think this justifies a config. |
On a different note, I wonder if the situation with #12162 is bad enough that we should block on that issue. |
Is it commonly expected that having a workspace means devs want workspace dependencies? In a lot of projects I've seen that use workspaces, they still have package dependencies. That's the only reason I suggested a config: the default behavior will change. Maybe it is for the better, but the change may not be expected. Or would heuristics include whether existing dependencies are inherited or not? |
See #10608 (comment) |
Sorry. I did skim through the comments but obviously missed that one. That said, thoughts about whether there's a mix and for any |
The second option says to only inherit if everything is inherited. In the mixed situation you mentioned, users would not get anything inherited automatically. Overall, I'm fine with not covering every use case thereby making this more opinionated. |
This is a very safe assumption, and a subset of the behavior on the first point that could be amended.
+1. By enabling this feature via the latter point, we'd be actively discouraging the deduplication-only style (without needing to display errors to the hold-outs). |
I'd like to move forward this feature, since I work with workspaces often. I've read through a bit of the discussion - here is a summary of my thoughts: The core idea is that there should be some way to add a dependency to the workspace dependency list, and inherit it in any number of workspace members, at the same time. Personally, only the version should exist in the workspace dependencies, and the inherited dependency should include any features added. Side note: The formatting should be consistent, whether you're already using A few options for UX:
Personally, 4 seems most controversial since many workspaces don't use workspace dependencies, and there would be no clear way to opt out. It would also be a backwards incompatible change. I think that either 1 or 2 is ideal, but don't have strong opinions on it. I'm not sure what the next steps should be on this, but perhaps I should work on a draft PR implementation? |
I'll expand on some possibilities for the first option: Add to workspace dependencies only: Inherit dependency only (or add without inheriting if it's not a workspace dependency): Do both: Add a feature for the dependency in the member crate: Add a dependency to multiple members (this isn't clearly necessary but could be useful): Hoist the feature into the workspace dependency (and remove it from members?): |
(emphasis added) Adding to any number of workspace members, migrating to workspace dependencies, or only adding to the workspace have previously not been a part of this plan. The plan for this has been "when you add the specified deps to the current package, write through to In general, I am concerned about moving forward with this with the state of
We were already leaning towards (2), see #10608 (comment)
The only existing-style check we do is for sort order. In every other way, we generate output in an opinionated format. |
If it's automatic, here's what I'd propose:
|
Is it not possible to add such a check? I think consistency is important in Cargo.toml, and this is one of the few places where the CLI produces inconsistent output that I have ended up having to retype manually. As you mentioned, overrides for features, optionality, etc, should be in the inherited dependency in members rather than the workspace dependency list, so it makes more sense for the default to be an inline table rather than the shorthand boolean value IMO. So if nothing else, maybe a separate proposal to change the default formatting rather than checking style. |
This was a deliberate choice when we merged |
Thanks for the context, I suppose it might make sense to add a minimal implementation now and it can be expanded on in the future. |
Do you think it's fine for me to open a PR this weekend to implement what has been discussed thus far? |
As I said earlier
Maybe we could just not support this, either
|
I think the first would be fine for now, and if you want to enable or disable default features it could be done manually until that issue is resolved. The error could be something along the lines of:
|
Could we go even further given rust's generally actionable error messages, pointing to further information if needed? The crux of the issues with
If a bit wordy, perhaps it could link to more information about workspace dependencies.
For clarification, do you mean erring on any entry or just one being |
We need to be careful about overly wordy error messages because almost no one reads them. Additionally, I'm not confident that the right solution is to be telling them to much with the workspace inheritance.
Error if those flags are used on the commandline when we'd inherit. Maybe also if
The questions I'm asking myself are
|
It's a good question. FWIW, I'm seeing a lot more projects use them as a way to normalize dependencies or, to a lesser extent, lints. Still, it's anecdotal but hopefully useful input nonetheless. |
While people using them is a good input, we need to consider more than that for what we should be driving people towards and for myself, I see there being enough caveats around them because of #12162 that I do not think they are. I also think they offer footgun for library maintainers because of action-at-a-distance can cause breaking changes. When releasing a package, I can usually look at the commits for the package package directory. |
Problem
There is no way to tell
cargo add
that you want to add a dependency from a workspace explicitly. It is currently only done implicitly if you runcargo 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 addfoo
tobar
from a workspace. This could error if the workspace does not define that dependency or add it to the workspace if not found.features
andoptional
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.The text was updated successfully, but these errors were encountered: