Want cargo publish --idempotent
Problem
Sometimes a cargo publish run of multiple packages will fail. It is then necessary to restart it.
It would be nice if we could run the whole publication from the top, and simply skip crates that were already published. There are probably other uses.
Proposed Solution
cargo publish --idempotent
If the current version of the crate has already been published, skips the crate and exits with a success status.
(Ideally it would download the crate and check it's identical to what is proposed to publish now, but I think that crates.io does unpredictable-to-cargo mangling of the uploaded data which makes such a check infeasible.)
Notes
No response
Sometimes a cargo publish run of multiple packages will fail. It is then necessary to restart it.
Could you clarify the use case? Did you mean one cargo publish invocation to publish multiple packages? cargo publish doesn't support that at the moment. Workspace publish is a feature tracked here.
If the current version of the crate has already been published, skips the crate and exits with a success status.
There is a similar request https://github.com/rust-lang/cargo/issues/3662. I wonder if we should keep that and close this one.
Could you clarify the use case? Did you mean one
cargo publishinvocation to publish multiple packages?cargo publishdoesn't support that at the moment. Workspace publish is a feature tracked here.
Yes. Because cargo can't publish a workspace in one go, we all have add-on tools of varying sophistication to publish our multi-crate workspaces. The add-on tools iterate over the crates in a workspace, calling cargo publish for each one.
I think #1169 looks quite hard. It has been open for a very long time and although there's work on it, a good solution there will be a little while.
One of the ways that these add-on tools are suboptimal is that after a failure of any kind, the workspace can be left "half-published". Recovering from such a situation involves either complicated predictive logic in the tool, or manual tweaking of intermediate publication lists, etc.
So in the meantime, while #1169 is worked on, our add-on tools would be simpler and more reliable of cargo offered an idempotent publish operation.
And, I think any reasonable solution to #1169 needs to cope with failures halfway through, and for that situation to be recoverable. That would mean that to implement #1169, cargo needs an idempotent publish operation for its own use anyway. Ie, the underlying functionality in my request here is a necessary part of #1169.
But it is also separately useful, so it's worth exposing as an operation to external callers. The notion is fairly well-defined and, I think, not that hard to implement.
If the current version of the crate has already been published, skips the crate and exits with a success status.
There is a similar request #3662. I wonder if we should keep that and close this one.
#3662 requests early failure. I'm requesting treating "this one is already published" as a success. Similar underlying logic might be involved with both, but the overall semantics are rather different.
Failure on republish is precisely the opposite of what is needed for reliable publication of a whole workspace (whether as part of a first-class cargo feature for this, or as part of an add-on tool that invokes cargo). Since, after a failure, one ought to stop. Whereas when publication of a whole workspace, "you already did this one" should mean "carry on".
So in the meantime, while https://github.com/rust-lang/cargo/issues/1169 is worked on, our add-on tools would be simpler and more reliable of cargo offered an idempotent publish operation.
As an author of such an add-on tool (cargo-release), I don't see how having this would make the tool much simpler or more reliable.
But I also feel automatic recovery is a tricky enough subject that I give the underlying steps to the user so they can decide how they want to recover, which includes a cargo release publish --unpublished command.
And, I think any reasonable solution to https://github.com/rust-lang/cargo/issues/1169 needs to cope with failures halfway through, and for that situation to be recoverable. That would mean that to implement https://github.com/rust-lang/cargo/issues/1169, cargo needs an idempotent publish operation for its own use anyway. Ie, the underlying functionality in my request here is a necessary part of https://github.com/rust-lang/cargo/issues/1169.
In terms of core functionality, I see workspace publishing as the next step and we can better survey where things are at and expand from there. Having workspace-publishing in will help us better evaluate how this mode should work rather than designing this most first and then hoping it remains coherent.
Had some more conversations with people about this and a problem that came up is that if a version is already in the index, its unclear if its already published or the user forgot to bump the version. At least in this proposal, it requires opt-in.
Had some more conversations with people about this and a problem that came up is that if a version is already in the index, its unclear if its already published or the user forgot to bump the version. At least in this proposal, it requires opt-in.
Indeed so. Absolutely, it has to be an opt-in for this reason.
In discussing #15006, a thought that might be relevant here:
All three options involve magic in a way and a concern I have with that is the defaults for cargo publish: the default is unreversable and there is no confirmation prompt. If we could go back and change it, I would make --dry-run the default and have a confirmation prompt on --execute (like cargo release). In this state, erroring out on unexpected input is much safer than a "guess my intent flag" as users have to go out of there way to be able to review what will happen.
With -Zpackage-workspace this becomes more relevant. At least if it failed in the middle suddenly workspace publish becomes useless. To publish again for a large workspace you'll need to exclude publish=false and already published packages.
Two cents here: I'd suggest --skip-already-published instead of --idempotent since that's a bit more specific in this context.
This is, however, not the same. If we actually want idempotent publishing, I'd way we need to calculate the hash of the crate and compare that instead of the version (so we actually know it is the same byte-by-byte crate and not a forgotten version bump). Just don't want to hijack the op's issue if that's now what he asked for...
We cannot do --skip-already-published without doing the thing you suggested, especially the forgotten version bump part.
calculate the hash of the crate and compare that instead of the version (so we actually know it is the same byte-by-byte crate and not a forgotten version bump).
This might be really doable without any new flag added. I can't see a reason people using workspace publish don't want the --idempotent or -skip-already-published flag.
I can't see a reason people using workspace publish don't want the --idempotent or -skip-already-published flag.
+1 to that, I think it should be the default. The AWS SDK has had logic handling this for several years as part of our publishing tool.
My one worry would be the increased traffic on crates.io from the proposed equivalence checks. We have already caused throttling on crates.io with our publishing (see https://github.com/rust-lang/crates.io/issues/11588). That issue has been resolved, but I would hate for the traffic increase to cause any more issues.
This might be really doable without any new flag added. I can't see a reason people using workspace publish don't want the
--idempotentor-skip-already-publishedflag.
Would there still be the option of adding a flag that doesn't check the hash? For example, checking the hash won't work for cargo's own workflow which has this check which would be nice to remove.
Sorry @ehuss, I didn't get the connection between checking tarball checksum and publish.py. Can't publish.py be replaced entire with cargo publish --workspace plus https://github.com/rust-lang/cargo/pull/16012? It might need some --exclude for sure, but cargo publish already checks registry index existence.
I believe there will be files that change that are mostly inconsequential. For example, Cargo.lock, or normalization changes in Cargo.toml. Our existing checks won't handle those. We especially don't want publishing to fail, since it won't be caught until release day, and we may not notice at all since it is buried in a log file nobody looks at.
(We also don't catch workspace inheritance changes, though arguably those may be things we want to bump the version for.)
I also think that you may not want to force a version bump in all circumstances. Let's say I change a README in my project, but I don't want to publish a new version just for that. My workflow is to usually leave the version number unchanged, and then decide when I make a release which packages to actually publish by bumping their versions.
Understood, thanks!
The idempotency I understood here requires you to publish packages from the exact same source without any modification. For example, If you don't check in Cargo you are on your own journey, or you need to ensure registry index stay the same with --offline, because the input changed and broke idempotency.
Changing README but don't want to publish it is a new workflow than the idempotent workspace publish to me. Cargo doesn't know what the requirement is to skip a package safely. Don't check hash is a escape hatch but like you said there are more configurable points we might want to consider.
BTW, I guess Cargo's workflow won't be affected if the proposed solution get accepted, because publish.py always publish from the exact identical source for all packages, and bump-check xtask requires every crate has a version bump even with just single line changed in README.
BTW, I guess Cargo's workflow won't be affected if the proposed solution get accepted, because publish.py always publish from the exact identical source for all packages, and bump-check xtask requires every crate has a version bump even with just single line changed in README.
If we leave publish.py the same, I don't think it will be affected by #16012. But it also means we won't be able to use publish --workspace (and similar to my other projects, it won't help me and I continue to need to use a manual approach).
bump-check doesn't really do the same thing, either. It ignores changes to Cargo.lock (which is usually good), but it misses changes to workspace inheritance (which is usually bad). It also forces a bump when changing things like README or a test, even though we don't really need (or want) a bump for those.
Even with bump-check, we would not be able to switch to publish --workspace.
Now I see it. Thanks for the explanation!
So basically there are at least two workflows when people want to skip already published packages
- Idempotent workspace publish from the exactly identical source
- Useful when you failed to publish in the middle and want to retry.
- Usually everything bumps its version and is published in lockstep.
- You don't want to skip publish just because you forgot to bump version for a package.
- rust-lang/cargo#16012 should take care of this
- Several workspace publish from different commits/changes in the workspace
- Workspace members are loosely coupled or completely independent.
- One package may get version bumped but the others don't but still what to skip those that version didn't changed.
- The rust-lang/cargo#16012 doesn't help this, because Cargo.lock or workspace inheritance may change the content of packages having no version bump and fail the publish
- It is hard to find a safe way to determine whether a package is safe to skip beyond checking version in registry index.
The original request seems to mostly focus on the first one, so I did rust-lang/cargo#16012. However, we might want to consider future support for the second workflow, or at least ensure that when it comes into play, we won’t block our way to implement it.
How many different workflows should we support? How do we decide which ones we should support?
Also, if we're just now hearing about people wanting checksum comparisons, then maybe this all needs more baking in third-party release management tools.
Also, if we're just now hearing about people wanting checksum comparisons, then maybe this all needs more baking in third-party release management tools.
Nah. I thought of the same solution when thinking again the issue of workspace publish being not atomic, and then realized @MOZGIII has already proposed the same thing. We want to have a reliable way to resume for a broken workspace publish, especially for network errors. Checksum comparison is just a guardrail to ensure we're resuming from the same state.
Even for small workspaces once your workspace publish Job failed, you cannot publish through CI again and can only manually publish. As a result you will lose trusted publishing. So you either do whatever AWS SDK is doing, or don't use cargo publish --workspace at all and instead use third-party release tool.
you cannot publish through CI again and can only manually publish. As a result you will lose trusted publishing.
When we discussed trusted publishing going in, the idea was that it wasn't more trusted than direct publishing, but more trusted than other forms of CI publishing. I worry if we're framing direct publishing as a negative alternative to trusted publishing.
Also, I feel like if a release process fails, you will have to do manual cleanup either way; a flag would just help simplify this.
Also, I'm not concerned if people aren't using --workspace itself; there are many workflows where it wouldn't be applicable. What is valueable is multiple-package publishing within the workspace.
Also, I'm not concerned if people aren't using
--workspaceitself; there are many workflows where it wouldn't be applicable. What is valueable is multiple-package publishing within the workspace.
Putting --workspace as first step is just for making it less controversial (but apparently it didn't 😓). multiple-package publishing actually will also benefits from the idempotent behavior. In the wild large workspace use cargo publish a lot and have reimplemented the same already_published check. For examples
- Eclipse Zenoh reimplemented the
already_publishedcheck. So do oxc and cargo-mono (monorepo tool swc uses). - Apache OpenDAL run
cargo publishin CI. Failed in the middle will require manual publish or temporarily change to the workflow yaml. - polars has 20+ workspace members to publish. If they opt-in workspace/multi-package publish and failed in the middle, it is a bit annoying for them to resume, as the need to figure out what hasn't and reconstruct the CLI. So does Apache arrow-rs, windows-rs.
Yeah if a project is that big, should just use some release management tool for sure, but those tool still need to deal with already_published and retry logic (like AWS Rust SDK). The idempotent feature would help make this part more correct and less re-work on release tool side. Upon this point, maybe cargo-publish as a plumbing level command should provide a flag instead of auto opt-in idempotent behavior.
I worry if we're framing direct publishing as a negative alternative to trusted publishing.
It is not "negative", but trusted publishing does provides a better and easier provenance tracking than manual publish. Also, while being a bug in cargo, publishing on different platform may get different results especially on Windows.