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

Fix: minimal versions check #1237

Closed
wants to merge 3 commits into from

Conversation

sebadob
Copy link

@sebadob sebadob commented Aug 29, 2024

Checklist

  • Updated guide with pertinent info (may not always apply).
  • Squash down commits to one or two logical commits which clearly describe the work you've done.
  • Unittest is a friend:)

This is a tiny fix for minimal versions. I stumbled about this issue when testing with the latest openraft:0.9.15 when executing in my pipeline:

cargo minimal-versions check --workspace

This change is Reviewable

@sebadob sebadob changed the title fix minimal versions check Fix: minimal versions check Aug 29, 2024
Copy link
Collaborator

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

I can reproduce the error with the latest commit, create a hello-world crate and add the latest Openraft as its dependency, then:

$ cd hello-world
$ cargo minimal-versions check
error[E0599]: no associated item named `ZERO` found for struct `rust_decimal::Decimal` in the current scope
  --> /home/steve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/byte-unit-5.1.4/src/byte/decimal.rs:27:29
   |
27 |         if size >= Decimal::ZERO {
   |                             ^^^^ associated item not found in `Decimal`

error[E0277]: the trait bound `rust_decimal::Decimal: From<u128>` is not satisfied
  --> /home/steve/.cargo/registry/src/index.crates.io-6f17d22bba15001f/byte-unit-5.1.4/src/byte/decimal.rs:73:45
   |
73 |                 _ => match size.checked_mul(Decimal::from(unit.as_bytes_u128())) {
   |                                             ^^^^^^^ the trait `From<u128>` is not implemented for `rust_decimal::Decimal`

...

As we can see, the errors originate from the byte_unit crate, then I tried:

$ git clone https://github.com/magiclen/Byte-Unit.git
$ cd Byte-Unit
$ cargo minimal-versions check
rror[E0599]: no associated item named `ZERO` found for struct `rust_decimal::Decimal` in the current scope
  --> src/byte/decimal.rs:27:29
   |
27 |         if size >= Decimal::ZERO {
   |                             ^^^^ associated item not found in `Decimal`

error[E0277]: the trait bound `rust_decimal::Decimal: From<u128>` is not satisfied
  --> src/byte/decimal.rs:73:45
   |
73 |                 _ => match size.checked_mul(Decimal::from(unit.as_bytes_u128())) {
   |                                             ^^^^^^^ the trait `From<u128>` is not implemented for `rust_decimal::Decimal`
   |

I got the same error, so I kinda think we should fix this in byte_unit, not Openraft🤔

Reviewable status: 0 of 2 files reviewed, all discussions resolved

@sebadob
Copy link
Author

sebadob commented Aug 29, 2024

I got the same error, so I kinda think we should fix this in byte_unit, not Openraft🤔

Oh yes, that would make sense as well, of course.
I will just open a PR over there. Let's see how fast this will be merged (and published).

openraft could implement a temporary fix though until this has been released on crates.io I guess?

Edit:

Opened a PR over there.

@drmingdrmer
Copy link
Member

@sebadob
Thank you for bringing this issue to our attention.

This issue stems from a dependency crate rather than our crate directly. Introducing a new unnecessary crate solely to fix this issue may not be the most elegant solution.

If the minimal-versions check is critical for your application, a more appropriate solution would be adding rust_decimal = "1.15" in the application crate, but not in Openraft's Cargo.toml.

@sebadob
Copy link
Author

sebadob commented Sep 1, 2024

Yes it's coming from a dependency, but it's not being added, it's there anyway and nothing additional.

Checking for minimal versions is a good practice for any crate. This example may not be the best one, because the issue has been introduced in a version 3 years ago, but the same happens for new releases as well (sadly, quite often), which can lead to very weird errors, when you are stuck on an older version in your application for whatever reason.

It's fine for me to just close the issue then.

@sebadob sebadob closed this Sep 1, 2024
@SteveLauC
Copy link
Collaborator

Checking for minimal versions is a good practice for any crate

Yeah, true, perhaps we can have this check in Openraft's CI as well:)

@drmingdrmer
Copy link
Member

Checking for minimal versions is a good practice for any crate

Yeah, true, perhaps we can have this check in Openraft's CI as well:)

Running it on in Openraft root dir yields an error. I'm not sure if it is a bug of minimal-versions parsing Cargo.toml or I missed anything.

cargo minimal-versions check --workspace

info: running `cargo update -Z minimal-versions`
error: failed to load manifest for workspace member `/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/tests`
referenced by workspace at `/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/Cargo.toml`

Caused by:
  failed to parse manifest at `/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/tests/Cargo.toml`

Caused by:
  feature `bt` includes `openraft/bt`, but `openraft` is not a dependency
error: process didn't exit successfully: `/Users/drdrxp/.rustup/toolchains/nightly-2024-07-02-aarch64-apple-darwin/bin/cargo update -Z minimal-versions` (exit status: 101)

And running cargo +nightly update -Z direct-minimal-versions or -Z minimal-versions directly just fails(because of version selecting algorithm being not that smart?). It looks like it just locks version of quote to 1.0.0 because it is used directly in our Cargo.toml and then refuses to use a higher version even when a dependency crate requires.

cargo +nightly update -Z direct-minimal-versions

    Updating crates.io index
error: failed to select a version for `quote`.
    ... required by package `syn v2.0.0`
    ... which satisfies dependency `syn = "^2.0"` of package `openraft-macros v0.10.0 (/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/macros)`
    ... which satisfies path dependency `openraft-macros` of package `openraft v0.10.0 (/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/openraft)`
    ... which satisfies path dependency `openraft` of package `openraft-rocksstore v0.1.0 (/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/stores/rocksstore)`
versions that meet the requirements `^1.0.25` are: 1.0.37, 1.0.36, 1.0.35, 1.0.34, 1.0.33, 1.0.32, 1.0.31, 1.0.30, 1.0.29, 1.0.28, 1.0.27, 1.0.26

all possible versions conflict with previously selected packages.

  previously selected package `quote v1.0.0`
    ... which satisfies dependency `quote = "^1.0"` of package `openraft-macros v0.10.0 (/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/macros)`
    ... which satisfies path dependency `openraft-macros` of package `openraft v0.10.0 (/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/openraft)`
    ... which satisfies path dependency `openraft` of package `openraft-rocksstore v0.1.0 (/Users/drdrxp/xp/vcs/github.com/drmingdrmer/openraft/stores/rocksstore)`

failed to select a version for `quote` which could resolve this conflict

And cargo-minimal-versions seems to affect only Cargo.lock but Openraft, as a lib not app, does not include Cargo.lock in the repo.

@sebadob
Copy link
Author

sebadob commented Sep 2, 2024

And cargo-minimal-versions seems to affect only Cargo.lock but Openraft, as a lib not app, does not include Cargo.lock in the repo.

Yes it does. It basically uses the Cargo.lock to check for errors. As a crate you have the lock file in the .gitignore, but it is created anyway all the time when running tests, clippy, and so on. Not sure though why you can't easily run it inside openraft root. It has some issues with tests dependencies when I try, exactly the first one you posted.

Edit:

Found the issue. It was because openraft is a dev dependency in tests/, which seems so confure the minimal-versions check. By just quickly moving it to "real" dependencies and cheking, I can execute the check and stumbled about something else with serde_instant for instance when executing cargo minimal-versions check --all-features:

error[E0599]: no method named `timestamp_nanos_opt` found for struct `DateTime` in the current scope
   --> openraft/src/metrics/serde_instant.rs:102:33
    |
102 |             let nano = datetime.timestamp_nanos_opt().ok_or(serde::ser::Error::custom("time out of range"))?;
    |                                 ^^^^^^^^^^^^^^^^^^^ method not found in `DateTime<Utc>`

error[E0599]: no function or associated item named `from_timestamp_nanos` found for struct `DateTime` in the current scope
   --> openraft/src/metrics/serde_instant.rs:124:46
    |
124 |                     let datetime = DateTime::from_timestamp_nanos(value as i64);
    |                                              ^^^^^^^^^^^^^^^^^^^^ function or associated item not found in `DateTime<_>`
    |
note: if you're trying to build a new `DateTime<_>` consider using one of the following associated functions:
      DateTime::<Tz>::from_utc
      DateTime::<FixedOffset>::parse_from_rfc2822
      DateTime::<FixedOffset>::parse_from_rfc3339
      DateTime::<FixedOffset>::parse_from_str
   --> /home/sd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chrono-0.4.0/src/datetime.rs:71:5
    |
71  |     pub fn from_utc(datetime: NaiveDateTime, offset: Tz::Offset) -> DateTime<Tz> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
191 |     pub fn parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
203 |     pub fn parse_from_rfc3339(s: &str) -> ParseResult<DateTime<FixedOffset>> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
216 |     pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@SteveLauC
Copy link
Collaborator

If cargo-minimal-versions does not work, we can use the -Zdirect-minimal-versions flag directly (nightly toolchain needed though), here is how Nix handles it:

https://github.com/nix-rust/nix/blob/e8879e310f3f1ca7277588de44204bf14eb1991c/.github/workflows/ci.yml#L333-L354

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.

3 participants