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

Add tests for --dry-run and cargo ws plan #157

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

popzxc
Copy link
Contributor

@popzxc popzxc commented Jul 17, 2024

I finally managed to run tests locally :)

This PR:

  • Adds instructions on how to run tests locally.
  • Removes cargo publish --dry-run step from cargo ws publish --dry-run (see this comment). Upd: reverted.
  • Adds tests for cargo ws publish --dry-run.
  • Adds tests for cargo ws plan

Copy link
Contributor Author

@popzxc popzxc left a comment

Choose a reason for hiding this comment

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

A few comments to assist with the review.

README.md Outdated
The recommended way to run tests for the crate is as follows:

```sh
cargo test --manifest-path cargo-workspaces/Cargo.toml -j1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not fully sure if that's an appropriate fix, but it seems to work consistently.
Without it, tests were always failing. Probably this issue may be related.

README.md Outdated
Comment on lines 351 to 352
If you observe unexpected differences in snapshots, you may want to override your compiler to
the same version as used in [CI](.github/workflows/ci.yml), e.g.:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CI is fixed on 1.70, and newer cargo doesn't emit # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html line.
Also, it looks like cargo install --path cargo-workspaces doesn't work with 1.70, so when I worked on it, I had to manually override compiler for tests and remove an override for installation.
I guess MSRV and snapshots should be updated, but I thought it'd be an unnecessary scope extension for this PR.

I believe that this comment is useful as a general observation regardless of that.

@@ -46,6 +46,7 @@ pub struct Publish {

impl Publish {
pub fn run(mut self, metadata: Metadata) -> Result {
let mut command_success = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we no longer bail on errrors, I thought that it's weird to print success ok if we've met problems.

README.md Outdated Show resolved Hide resolved
@@ -100,7 +101,10 @@ impl Publish {
warn!("build failed", "");
}

basic_checks(pkg)?;
command_success = command_success && basic_checks(pkg)?;
// We cannot use `cargo publish --dry-run` directly, because it checks if all the dependencies
Copy link
Owner

Choose a reason for hiding this comment

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

I kept the basic checks, but I was wondering if we still need them if --dry-run warns about all of the issues that we check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, --dry-run doesn't warn about them -- they are only checked during uploading.
Probably it makes sense to add this functionality to cargo directly, I'll create an issue there.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, that's probably the way we want to go in the future.

cargo-workspaces/src/publish.rs Outdated Show resolved Hide resolved
cargo-workspaces/src/utils/basic_checks.rs Outdated Show resolved Hide resolved
@popzxc popzxc changed the title Fix cargo ws publish --dry-run, add tests for --dry-run and cargo ws plan Add tests for --dry-run and cargo ws plan Jul 17, 2024
@popzxc
Copy link
Contributor Author

popzxc commented Jul 17, 2024

@pksunkara I've experimented with cargo ws publish --dry-run a bit more, and it seems that there are still unsolved issues.
For example, it doesn't seem to work with first-time-ever publishing (see sample test failure). Also in some of the workspaces I've checked, I still encountered issues with dependency resolution.

What would be the better way forward here? Add an extra flag to not perform cargo publish --dry-run (e.g. stop at build and basic checks)?

@pksunkara
Copy link
Owner

Also in some of the workspaces I've checked, I still encountered issues with dependency resolution.

Can you please describe them?

it doesn't seem to work with first-time-ever publishing (see sample test failure).

From what I read, the --no-verify should take care of it. Also, I tested this on my monorepo with non-published public crates and it was working.

The recommended way to run tests for the crate is as follows:

```sh
cargo test --manifest-path cargo-workspaces/Cargo.toml -j1
Copy link
Owner

Choose a reason for hiding this comment

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

You don't need the j1 if you add #[serial] to all the tests.

mod utils;
use insta::assert_snapshot;

#[test]
Copy link
Owner

Choose a reason for hiding this comment

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

I see some test failures caused by parallel execution. #[serial] should be added to all these tests.

@popzxc
Copy link
Contributor Author

popzxc commented Jul 17, 2024

Can you please describe them?

Besides one that was mentioned (publishing for the first time), I've met the following error when trying to bump packages in workspace from 0.1.0-rc.2 to 0.2.0-rc.2:

    Updating crates.io index
error: failed to prepare local package for uploading

Caused by:
  failed to select a version for the requirement `<ws_dependency_name> = "=0.2.0-rc.2"`
  candidate versions found which didn't match: 0.1.0-rc.2, 0.1.0-rc.1
  location searched: crates.io index
  required by package `<ws_dependency_name> v0.2.0-rc.2 (<path>)`
  if you are looking for the prerelease package it needs to be specified explicitly
      <ws_dependency_name> = { version = "0.1.0-rc.2" }
  perhaps a crate was updated and forgotten to be re-vendored?

It's pretty weird and I'm not sure why it happens. My current hypothesis is that it's because these packages have build scripts, but I didn't investigate further so far.

@pksunkara
Copy link
Owner

publishing for first time

It seems to work. Try the following:

●  → cargo info random28
Error: Resouce at url 'https://crates.io/api/v1/crates/random28' could not be found
●  → cargo new random28
     Created binary (application) `random28` package
●  → cd random28
● /random28  (stable)  (empty) → cargo ws publish --dry-run --allow-dirty --publish-as-is
warn Dry run doesn't check that all dependencies have been published.
error: config value `http.cainfo` is not set
info checking random28
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
warn check failed 'description' field should be set
warn check failed either 'license' or 'license-file' field should be set
    Updating crates.io index
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
   Packaging random28 v0.1.0 (/home/pksunkara/Coding/termapps/random28)
    Packaged 4 files, 920.0B (712.0B compressed)
   Uploading random28 v0.1.0 (/home/pksunkara/Coding/termapps/random28)
warning: aborting upload due to dry run
info success ok

@popzxc
Copy link
Contributor Author

popzxc commented Jul 18, 2024

This is probably becasue it's the only crate in the workspace (no ws deps). Try this:

$ cd cargo-workspaces/fixtures/normal
$ cargo ws publish --dry-run
warn Dry run doesn't check that all dependencies have been published. 
warn Dry run doesn't perform versioning. 
error: config value `http.cainfo` is not set
info checking dep1
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
    Updating crates.io index
warning: manifest has no documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
   Packaging dep1 v0.1.0 (/<...>/cargo-workspaces/fixtures/normal/dep1)
    Packaged 4 files, 925.0B (715.0B compressed)
   Uploading dep1 v0.1.0 (/<...>/cargo-workspaces/fixtures/normal/dep1)
warning: aborting upload due to dry run
info checking dep2
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
    Updating crates.io index
warning: manifest has no documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
   Packaging dep2 v0.1.0 (/<...>/cargo-workspaces/fixtures/normal/dep2)
    Packaged 4 files, 1.0KiB (756.0B compressed)
   Uploading dep2 v0.1.0 (/<...>/cargo-workspaces/fixtures/normal/dep2)
warning: aborting upload due to dry run
info checking top
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
    Updating crates.io index
warning: manifest has no documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
   Packaging top v0.1.0 (/<...>/cargo-workspaces/fixtures/normal/top)
    Updating crates.io index
error: failed to prepare local package for uploading

Caused by:
  no matching package named `dep1` found
  location searched: registry `crates-io`
  required by package `top v0.1.0 (/<...>/cargo-workspaces/fixtures/normal/top)`
warn publish failed top v0.1.0
info success ok

@pksunkara
Copy link
Owner

I have a ws project with deps where some of them are not published yet, and it's working in there. But it's not working in normal fixture. And I was trying with cargo publish --dry-run --no-verify directly.

There are a few differences between both and I have been trying to pinpoint the issue, but unfortunately, I was not able to yet.

@popzxc
Copy link
Contributor Author

popzxc commented Jul 19, 2024

Likewise. So, would it make sense to add an option to skip --dry-run step until we figure it out?

@pksunkara
Copy link
Owner

I will try to debug more on Sunday. I am currently thinking it's a bug in cargo itself. Will decide after that.

Skipping means, we have to skip the whole publish command.

@popzxc
Copy link
Contributor Author

popzxc commented Jul 24, 2024

@pksunkara sorry for pinging, have you been able to find out the reason?
I'm thinking on what to do with this PR. In theory, I can try to create a different fixture so that the test passes, but not sure if that's the correct approach.

@pksunkara
Copy link
Owner

I haven't had time to debug this, but let's resolve this week either way. Only one thing I would want to know is how does https://github.com/crate-ci/cargo-release dry run work on the same fixture? Does it give the same error as us?

@popzxc
Copy link
Contributor Author

popzxc commented Aug 9, 2024

@pksunkara sorry, had a busy week.

Looks like yes, cargo-release fails with the same error. Here's (trimmed) output of cargo release publish for the fixtures/normal:

error: failed to prepare local package for uploading

Caused by:
  no matching package named `dep1` found
  location searched: registry `crates-io`

Moreover, looks like there is an open issue about that there.

It doesn't look like it's easily solvable, so IMHO it probably makes sense to allow skipping actual --dry-run step, and check if the package passes basic checks & can be built only. This is not as comprehensive, of course, but I think it's a nice compromise for end users.

@pksunkara
Copy link
Owner

Reading that issue, it looks like --no-verify already allows people to skip it. Can you please confirm?

If it doesn't, then yeah, we should allow that.

@popzxc
Copy link
Contributor Author

popzxc commented Aug 9, 2024

I think the comments there refer to some internal behavior in cargo-release, because cargo publish --dry-run --no-verify skips the build but still checks dependencies, and thus fails:

cargo publish -p top --dry-run --no-verify
    Updating crates.io index
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
   Packaging top v0.1.0 (/home/<..>/cargo-workspaces/fixtures/normal/top)
    Updating crates.io index
error: failed to prepare local package for uploading

Caused by:
  no matching package named `dep2` found
  location searched: registry `crates-io`
  required by package `top v0.1.0 (/home/<..>/cargo-workspaces/fixtures/normal/top)`

@popzxc
Copy link
Contributor Author

popzxc commented Sep 2, 2024

@pksunkara Hey! Sorry, it's been a while, but I still want to drive this forward 😅

Could you please say which option sounds acceptable to you?
I see a few options here:

  1. Add a new CLI flag, e.g. --skip-upload, which will be used together with --dry-run to not actually perform cargo publish --dry-run step.
  2. Split basic checks and dry-run into two separate CLI flags (though basic checks would still prevent publishing).
  3. Allow "customizing" --dry-run flag, e.g. it's possible to run cargo ws publish --dry-run=full (basic & publish), cargo ws publish --dry-run=basic (only basic), cargo ws publish --dry-run=publish (only publish), or simply cargo ws publish --dry-run (defaults to full.

I'm open to other suggestions as well.
What would you prefer?

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