Search code examples
rustbuildrust-cargorust-crates

How to fix "updating crates.io index failed" problem when I build the Rust project


I have a problem building the Rust project. I have changed the Cargo.toml like the following:

[package]
name = "here"
version = "0.1.0"
edition = "2021"

[dependencies]
rand="0.8.5"

Only added rand to dependencies. But, when I build the project, updating crates.io index took too long and the result was like the following:

E:\Workspace\rust\example>cargo build
Updating crates.io index
warning: spurious network error (2 tries remaining): failed to read data: The server returned an invalid or unrecognized response
; class=Os (2)
warning: spurious network error (1 tries remaining): failed to read data: The server returned an invalid or unrecognized response
; class=Os (2)
error: failed to get rand as a dependency of package game1 v0.1.0 (C:\Users\myname\Desktop\Files\game1)

Caused by:
failed to load source for dependency rand

Caused by:
Unable to update registry https://github.com/rust-lang/crates.io-index

Caused by:
failed to fetch https://github.com/rust-lang/crates.io-index

Caused by:
failed to read data: The server returned an invalid or unrecognized response
; class=Os (2)

This is the full error message. What is this? How can I fix it? Please, help me.

I tried like the following:(https://github.com/rust-lang/cargo/issues/9335)

[net]
git-fetch-with-cli = true

in C:\Users\my_name.cargo\config

But, the error is not resolved. Help me.

I use windows 10 OS.


Solution

  • Not a full solution, but since Rust 1.58.0 you can use sparse registry. In .cargo/config.toml, set registries.crates-io.protocol to "sparse".