Error if any invalid keys are defined when inheriting a dependency#2
Error if any invalid keys are defined when inheriting a dependency#2Muscraft merged 1 commit intocargo-add-supportfrom
Conversation
049f6eb to
7b64cb8
Compare
ed0a124 to
06ecb3e
Compare
epage
left a comment
There was a problem hiding this comment.
We will have everything pre-parsed by the regular parser, so we shouldn't need to error check this.
In my re-wording, I think some context was lost. We need to error if the user sets any flags on the command-line that are incompatible with workspace = true.
|
So the error should be during the parsing of the CLI flags? |
It has to happen where we have access to the CLI flags and we know its a workspace dependency. Note that we pass in a struct to the ops that basically represents the CLI, so we know the details. Keep in mind that right now we are just loading an existing workspace entry. We'll soon also take the name and look up to find if its a workspace dependency. |
|
Would it make sense to wait until after adding in where we take the name and look up to find if it's a workspace dependency, to do this? |
|
We can implement the PRs in any order so long as we keep the work for both in mind though doing the other PR first will make this one more obvious for what to do |
|
I agree that one of the others would help make this slightly more clear. I'll do one of those and then come back to this. |
bfb9def to
7914967
Compare
workspace = true for `ca…06ecb3e to
a13ef84
Compare
tests/testsuite/cargo_add.rs
Outdated
There was a problem hiding this comment.
What about the case where the dependency already exists and we use one of these keys?
There was a problem hiding this comment.
I can add a test for this
src/cargo/ops/cargo_add/mod.rs
Outdated
There was a problem hiding this comment.
What if we rephrase this as
"cannot override workspace dependency with `{}`, either change `workspace.dependencies.{}.{}` or define the dependency exclusively in the package's manifest"
- I feel like "defined" is confusing language in this context
- I feel like talking in terms of "overriding" is more specific and clear than generically talking about "inheriting"
- This provides a remediation
There was a problem hiding this comment.
Does this override anything? To me, this only runs when it is not overriding something since it would've been found by get_existing_dependency. As for changing it I agree that we should change it to something else
There was a problem hiding this comment.
I guess there are two different kinds of overriding to talk about. This isn't writing over an existing fields value with a different one but it is instead overriding the field it inherits from workspace.dependencies.
There was a problem hiding this comment.
Yeah, that makes sense. I'll change it to what you said
src/cargo/ops/cargo_add/mod.rs
Outdated
There was a problem hiding this comment.
. This means you cannot define workspace with keys like version, registry, registry-index, path, git, branch, tag, rev, or package.
What about checking for
- path
- git
- version
- package
There was a problem hiding this comment.
The only package that is used here is to tell cargo add what package to add the dependency too. The others should be checked before this since they all would set a source from what I can tell
There was a problem hiding this comment.
The only package that is used here is to tell cargo add what package to add the dependency too.
Try a test case where you cargo add --rename foo foo-alt where foo is a workspace dependency and foo-alt is a workspace member.
The others should be checked before this since they all would set a source from what I can tell
Thats right, we either do or will cover this with testing switching sources. Probably good to add a comment about it.
There was a problem hiding this comment.
Try a test case where you
cargo add --rename foo foo-altwherefoois a workspace dependency andfoo-altis a workspace member.
So package is rename? or at least that's the way it looks to me
Thats right, we either do or will cover this with testing switching sources. Probably good to add a comment about it.
I'll add a comment about this
There was a problem hiding this comment.
I also do not know if we need branch, tag, or rev, since they can't be defined on their own
There was a problem hiding this comment.
I was thinking similar about branch, tag, and rev.
a13ef84 to
5c4bcca
Compare
src/cargo/ops/cargo_add/mod.rs
Outdated
There was a problem hiding this comment.
nit: this might read better if using the new format macro feature
"cannot override workspace dependency with `{field}`, \
either change `workspace.dependencies.{toml_key}.{field}` \
or define the dependency exclusively in the package's manifest",
src/cargo/ops/cargo_add/mod.rs
Outdated
There was a problem hiding this comment.
This is a case where the argument and field diverge (--rename to cause package to be set)
In general, I feel like the error message will be easier to read with arguments
error: cannot override workspace dependency with `--rename`, either change `workspace.dependencies.foo.package` or define the dependency exclusively in the package's manifest
There was a problem hiding this comment.
fn err_msg(toml_key: &str, flag: &str, field: &str) -> String {
format!(
"cannot override workspace dependency with `{flag}`, \
either change `workspace.dependencies.{toml_key}.{field}` \
or define the dependency exclusively in the package's manifest"
)
}
if arg.default_features.is_some() {
anyhow::bail!("{}", err_msg(toml_key, "--default-features", "default-features"))
}
if arg.registry.is_some() {
anyhow::bail!("{}", err_msg(toml_key, "--registry", "registry"))
}
// rename is `package`
if arg.rename.is_some() {
anyhow::bail!("{}", err_msg(toml_key, "--rename", "package"))
}
tests/testsuite/cargo_add.rs
Outdated
There was a problem hiding this comment.
Is there a reason this test is using --registry? I think it can obscure what you are trying to test by changing more variables.
There was a problem hiding this comment.
There is no specific reason that used --registry here, do you think I should change it to --default-features?
5c4bcca to
3b875d5
Compare
src/cargo/ops/cargo_add/mod.rs
Outdated
There was a problem hiding this comment.
What if we combined the two and moved this to the end of the function?
- Check the source on
dependencyinstead ofold_dep - Condition the check on the source being a Workspace
There was a problem hiding this comment.
That works for me. Does moving it to after if dependency.source().is_none(), instead of the end of the function work?
There was a problem hiding this comment.
It doesn't have to be literally at the end of the function but among the clauses at the end of the function.
Stylistically, make sure it has newlines before and after since its not logically coupled to any of the blocks.
There was a problem hiding this comment.
That's how I have it! I'll push the changes and merge it then!
epage
left a comment
There was a problem hiding this comment.
Ok, this time I remembered to actually approve
3b875d5 to
74d7f69
Compare
Cargo add support for workspace inheritance Tracking issue: #8415 RFC: rust-lang/rfcs#2906 This PR adds all the required support for workspace inheritance within `cargo-add`. It was split up across a few different PRs as it required `snapbox` support from #10581 and a new `toml_edit` version from #10603. `@epage` and I decided to go ahead with this PR and add in some of the changes those PRs made. `@epage's` name on the commits is from helping to rewrite commits and some very minor additions. Changes: - #10585 - Muscraft#1 - Muscraft#3 - Muscraft#2 - Muscraft#4 r? `@epage`
Tracking issue: rust-lang#8415
RFC: rust-lang/rfcs#2906
PRs in this RFC:
Cargo.tomlrust-lang/cargo#10517license-path, anddepednency.pathrust-lang/cargo#10538readmerust-lang/cargo#10548rust-versionrust-lang/cargo#10563excludeandincluderust-lang/cargo#10565workspaceto… rust-lang/cargo#10564InheritableFieldsin aLazyCellinside `… rust-lang/cargo#10568key.workspace = truetokey = { workspace = true }rust-lang/cargo#10584cargo-addsupportcargo add foowhen afoo.workspace = truealready exists rust-lang/cargo#10585cargo add foowill inherit a workspace dependency #3Changes:
Remaining implementation work for the RFC
cargo-addsupport, see epage's commentr? @epage