guppy-rs/guppy

Gotcha using `workspace-dotted` without specifying a version

Closed this issue · 2 comments

Setup

My hakari.toml has:

workspace-hack-line-style = "workspace-dotted"

My root Cargo.toml has:

[workspace.dependencies]
my-workspace-hack = { path = "/path/to/my-workspace-hack" }

After initial setup (generate then manage-deps) all workspace packages have the following added as expected:

[dependencies]
my-workspace-hack.workspace = true

Issue

Later, when re-running cargo hakari manage-deps (or running cargo hakari --dry-run --quiet in CI), hakari reports that many packages are missing the my-workspace-hack dependency and attempts to add them again (or fail in the case of the CI job).

I debugged this and discovered the error, namely that the workspace dependency must specify a version as well as a path, i.e.:

[workspace.dependencies]
my-workspace-hack = { version = "0.1.0", path = "/path/to/my-workspace-hack" }

I traced this to the following code, which in my case always hits the first else clause (link.version_req() == &VersionReq::STAR) when the hakari package has no version:

#[allow(clippy::if_same_then_else, clippy::needless_bool)]
fn needs_update_v2(hakari_package: &PackageMetadata<'_>, link: PackageLink<'_>) -> bool {
if !link.version_req().matches(hakari_package.version()) {
// The version number doesn't match: it must be updated.
true
} else if link.version_req() == &VersionReq::STAR {
// The version number isn't specified and force_version is true.
true
} else {
false
}
}

So whilst this is not a bug, it is a gotcha and the cause is non-obvious (I had to step through in the debugger). Perhaps something could be added to surface this (likely not what the user intended) behaviour more directly? Perhaps running without an given version should be an explicit opt-in?

Documentation

The documentation does talk about this to an extent, though in a different context (I skipped reading the section initially as it did not apply to my circumstances).

It say (emphasis mine):

This option lets you specify the path to the workspace-hack crate, once, in the root Cargo.toml

And then shows an example where only the version is given:

[workspace.dependencies]
my-workspace-hack = "0.1.0"  # or another version number if you've changed it

So perhaps the need to specify the version could be made more prominent in the documentation?

Thanks, good catch! I think we can update the check to not require that the version be specified -- I don't think there's a particular reason to require it unless the crate is being published.

This is now out in cargo-hakari 0.9.30.