rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.67k stars 2.41k forks source link

Recover after crate extraction is interrupted #3661

Open zoumi opened 7 years ago

zoumi commented 7 years ago

When I was running cargo build --release, my computer suddenly poweroff(cargo was downloading crate then). So,I restart my computer, run cargo build --release again. The error occur:

$ cargo build --release
error: unable to get packages from source

Caused by:
failed to download replaced source `registry https://github.com/rust-lang/crates.io-index`

Caused by:
failed to parse manifest at `/home/zoumi/.cargo/registry/src/mirrors.xxxx.com-      07d6cef1b9e54f94/syntex_errors-0.42.0/Cargo.toml`

Caused by:
no `package` or `project` section found.

It seem the file is broken, and cargo isn't able to recover. (Sorry for my poor english.)

zoumi commented 7 years ago

The file /syntex_errors-0.42.0/Cargo.toml is empty.

zoumi commented 7 years ago

If I delete the file, another error occur: error: unable to get packages from source

Caused by: failed to download replaced source registry https://github.com/rust-lang/crates.io-index

Caused by: failed to read /home/zoumi/.cargo/registry/src/mirrors.xxxx.com-07d6cef1b9e54f94/syntex_errors-0.42.0/Cargo.toml

Caused by: No such file or directory (os error 2)

How can I repaire my crate src? Delete /home/zoumi/.cargo/registry/src ?

Luthaf commented 7 years ago

If it is not too bad for you (if you have a good internet connection and are OK with downloading all the crates again), you can just remove the $HOME/.cargo/registry/ and $HOME/.cargo/git/ directories.

alexcrichton commented 7 years ago

Curious!

As @Luthaf mentioned you can delete the whole registry folder and Cargo will recreate it. In theory this shouldn't happen as Cargo's hardened against failures like this, but there's perhaps a bug in those mitigations!

Marcono1234 commented 1 year ago

It looks like I just experienced the same issue on Windows 10, with:

For me the PC crashed while rust-analyzer was building the project. After the PC restarted rust-analyzer failed to build the project.

For example:

Caused by:
  unable to get packages from source

Caused by:
  failed to download replaced source registry `crates-io`

Caused by:
  failed to parse manifest at `<user>\.cargo\registry\src\index.crates.io-6f17d22bba15001f\serde_json-1.0.106\Cargo.toml`

Caused by:
  could not parse input as TOML

Caused by:
  TOML parse error at line 1, column 1

That Cargo.toml file indeed seems to be corrupted and contains ~2KB of NUL bytes (and nothing else). There was also a Cargo.toml.orig file whose content seems to be correct wellformed TOML. And also a .cargo-ok and a .cargo_vcs_info.json file.

Running cargo clean followed by cargo test (which uses serde_json as dev-dependency) from the command line also causes this error.

Deleting those broken dependencies under <user>\.cargo\registry\src\index.crates.io-6f17d22bba15001f solved the issue.

epage commented 2 months ago

The title of this issue is about interrupted downloads but the examples are for interrupted extractions which needs a separate solution.

Renaming accordingly.

epage commented 1 month ago

This was opened in 2017 and our solution to this has changed a lot since then, for a history of it see https://github.com/rust-lang/cargo/blob/c956e9f41c8a483385492b5b6c76cada8dd781cc/src/cargo/sources/registry/mod.rs#L551-L599 while we don't write the .cargo-ok file atomically, if its not a valid json file, we clear the cache and re- extract it.

What would be useful to know for anyone having problems with interruption recovery when extracting the files