-
Notifications
You must be signed in to change notification settings - Fork 0
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
Error if any invalid keys are defined when inheriting a dependency #2
Conversation
049f6eb
to
7b64cb8
Compare
ed0a124
to
06ecb3e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
#[cargo_test] | ||
fn invalid_key_inherit_dependency() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the case where the dependency already exists and we use one of these keys?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a test for this
src/cargo/ops/cargo_add/mod.rs
Outdated
@@ -284,6 +284,22 @@ fn resolve_dependency( | |||
// Checking for a workspace dependency happens first since a member could be specified | |||
// in the workspace dependencies table as a dependency | |||
if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() { | |||
let err_msg = "cannot be defined when inheriting a workspace dependency"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense. I'll change it to what you said
src/cargo/ops/cargo_add/mod.rs
Outdated
if arg.default_features.is_some() { | ||
anyhow::bail!("`default-features` {}", err_msg) | ||
} | ||
if arg.registry.is_some() { | ||
anyhow::bail!("`registry` {}", err_msg) | ||
} | ||
if arg.branch.is_some() { | ||
anyhow::bail!("`branch` {}", err_msg) | ||
} | ||
if arg.tag.is_some() { | ||
anyhow::bail!("`tag` {}", err_msg) | ||
} | ||
if arg.rev.is_some() { | ||
anyhow::bail!("`rev` {}", err_msg) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
. 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try a test case where you
cargo add --rename foo foo-alt
wherefoo
is a workspace dependency andfoo-alt
is 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking similar about branch, tag, and rev.
a13ef84
to
5c4bcca
Compare
src/cargo/ops/cargo_add/mod.rs
Outdated
format!( | ||
"cannot override workspace dependency with `{}`, \ | ||
either change `workspace.dependencies.{}.{}` \ | ||
or define the dependency exclusively in the package's manifest", | ||
key, toml_key, key | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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",
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change it to this
|
||
foo.workspace = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this missing a [dependencies]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it is
src/cargo/ops/cargo_add/mod.rs
Outdated
} | ||
// rename is `package` | ||
if arg.rename.is_some() { | ||
anyhow::bail!("{}", err_msg(toml_key, "rename")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
snapbox::cmd::Command::cargo() | ||
.masquerade_as_nightly_cargo() | ||
.arg("add") | ||
.args(["foo", "--registry", "registry", "-p", "bar"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no specific reason that used --registry
here, do you think I should change it to --default-features
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please
5c4bcca
to
3b875d5
Compare
src/cargo/ops/cargo_add/mod.rs
Outdated
if let Some(Source::Workspace(_)) = old_dep.source() { | ||
check_invalid_ws_keys(old_dep.toml_key(), arg)?; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we combined the two and moved this to the end of the function?
- Check the source on
dependency
instead ofold_dep
- Condition the check on the source being a Workspace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how I have it! I'll push the changes and merge it then!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once/if the checks get consolidated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.toml
rust-lang/cargo#10517license-path
, anddepednency.path
rust-lang/cargo#10538readme
rust-lang/cargo#10548rust-version
rust-lang/cargo#10563exclude
andinclude
rust-lang/cargo#10565workspace
to… rust-lang/cargo#10564InheritableFields
in aLazyCell
inside `… rust-lang/cargo#10568key.workspace = true
tokey = { workspace = true }
rust-lang/cargo#10584cargo-add
supportcargo add foo
when afoo.workspace = true
already exists rust-lang/cargo#10585cargo add foo
will inherit a workspace dependency #3Changes:
Remaining implementation work for the RFC
cargo-add
support, see epage's commentr? @epage