Search code examples
rustrust-cargo

How do I tell Cargo to update dependencies beyond the current versions of my project?


I have a simple project that relies on pyo3. Currently/previously, it was using version 0.20.0, but recently a new version has landed which is version 0.21.2. I am looking for a Cargo command that will update the dependency, even if that then means I have to go back through the source and fix any breaking changes that were introduced. However, if I run:

> cargo update pyo3 --precise 0.21.2

Or just equally:

cargo update pyo3

Then I am met with the following error:

error: failed to select a version for the requirement `pyo3 = "^0.20.0"`
candidate versions found which didn't match: 0.21.2
location searched: crates.io index
required by package `eight-ball v0.1.0 (/home/harry/Documents/eight-ball)`
perhaps a crate was updated and forgotten to be re-vendored?

Here is the Cargo.toml file as it was before the update:

[package]
name = "eight-ball"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "eight_ball"
crate-type = ["cdylib"]

[dependencies]
itertools = "0.12.1"
pyo3 = "0.20.0"

If I go to the Cargo.toml file and manually change the version on pyo3 to the latest version and then build, everything works fine. It doesn't seem like what I am looking for is something crazy, so I feel like I am just using Cargo wrong.

What I wanted was to just be able to run a Cargo command to update me to version 0.21.2, but the obvious choice didn't work, and I couldn't see a --force option or similar. I don't know why it thought that I was so set on remaining at version 0.20.0.

How can I use cargo to update the dependencies on my project without having to modify the Cargo.toml directly?


Solution

  • You can install cargo-edit:

    cargo install cargo-edit
    

    This will add an upgrade command to Cargo. You can use it to:

    • check for newer incompatible versions:

      > cargo upgrade -i --dry-run
          Checking mycrate's dependencies
      name old req compatible latest new req
      ==== ======= ========== ====== =======
      pyo3 0.20.3  0.20.3     0.21.2 0.21.2 
      
    • update a dependency to latest incompatible version:

      > cargo upgrade -i -p pyo3
      
    • update a dependency to a specific incompatible version:

      > cargo upgrade -i -p pyo3@0.21.2
      

    For background, cargo-edit previously had cargo add and cargo rm that were eventually implemented in Cargo directly. This too may eventually be built-in but related issues indicate uncertainty about how that'd eventually work.