Skip to content
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

Merged
merged 1 commit into from
Apr 27, 2022

Conversation

Muscraft
Copy link
Owner

@Muscraft Muscraft commented Apr 25, 2022

Copy link

@epage epage left a 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.

@Muscraft
Copy link
Owner Author

So the error should be during the parsing of the CLI flags?

@epage
Copy link

epage commented Apr 26, 2022

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.

@Muscraft
Copy link
Owner Author

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?

@epage
Copy link

epage commented Apr 26, 2022

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

@Muscraft
Copy link
Owner Author

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.

@Muscraft Muscraft changed the title Error if any invalid keys are defined with workspace = true for `ca… Error if any invalid keys are defined when inheriting dependency Apr 27, 2022
@Muscraft Muscraft changed the title Error if any invalid keys are defined when inheriting dependency Error if any invalid keys are defined when inheriting a dependency Apr 27, 2022
Comment on lines +1029 to +1030
#[cargo_test]
fn invalid_key_inherit_dependency() {
Copy link

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?

Copy link
Owner Author

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

@@ -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";
Copy link

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

Copy link
Owner Author

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

Copy link

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.

Copy link
Owner Author

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

Comment on lines 288 to 302
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)
}
Copy link

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

Copy link
Owner Author

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

Copy link

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.

Copy link
Owner Author

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 where foo is a workspace dependency and foo-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

Copy link
Owner Author

@Muscraft Muscraft Apr 27, 2022

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

Copy link

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.

Comment on lines 343 to 350
format!(
"cannot override workspace dependency with `{}`, \
either change `workspace.dependencies.{}.{}` \
or define the dependency exclusively in the package's manifest",
key, toml_key, key
)
Copy link

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",

Copy link
Owner Author

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

Comment on lines 6 to 8

foo.workspace = true
Copy link

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]?

Copy link
Owner Author

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

}
// rename is `package`
if arg.rename.is_some() {
anyhow::bail!("{}", err_msg(toml_key, "rename"))
Copy link

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

Copy link
Owner Author

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"))
    }

snapbox::cmd::Command::cargo()
.masquerade_as_nightly_cargo()
.arg("add")
.args(["foo", "--registry", "registry", "-p", "bar"])
Copy link

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.

Copy link
Owner Author

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?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please

Comment on lines 275 to 277
if let Some(Source::Workspace(_)) = old_dep.source() {
check_invalid_ws_keys(old_dep.toml_key(), arg)?;
}
Copy link

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 of old_dep
  • Condition the check on the source being a Workspace

Copy link
Owner Author

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?

Copy link

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.

Copy link
Owner Author

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!

Copy link

@epage epage left a 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

Copy link

@epage epage left a 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

@Muscraft Muscraft merged this pull request into cargo-add-support Apr 27, 2022
bors added a commit to rust-lang/cargo that referenced this pull request Apr 28, 2022
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`
@Muscraft Muscraft deleted the error-invalid-keys branch May 27, 2022 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants