Updating crates.io index failed

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

[package]
name = "example"
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\projects\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.

Same question posted on SO here: How to fix "updating crates.io index failed" problem when I build the Rust project - Stack Overflow

1 Like

According to your SO post you tried adding this to Cargo's config.toml without success:

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

With that command Cargo uses your git executable to fetch the crates.io index, see here: Configuration - The Cargo Book. Maybe if you try fetching the index manually, we can get a more descriptive error message? I.e. can you create a new empty git repository somewhere and try fetching the index in there and tell us what happens:

> mkdir temp
> cd temp
> git init
> git fetch https://github.com/rust-lang/crates.io-index

Thank you for your reply. I will try.

I tried it, but it doesn't work for me.

I tried. But it doesn't work for me.

E:\Workspace\Rust\projects\temp>git fetch https://github.com/rust-lang/crates.io-index
remote: Enumerating objects: 293372, done.
remote: Counting objects: 100% (966/966), done.
remote: Compressing objects: 100% (488/488), done.
error: 6533 bytes of body are still expected MiB | 5.00 KiB/s
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output

Could you run this:

> git config --global core.compression 0

and try again, please?

Thanks. I fixed it.

cargo +nightly -Z sparse-registry build
This helped me.

Thanks.

Thanks, Jonas.

cargo +nightly -Z sparse-registry build

This helped me.

Thanks.

Oliver

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.