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

Panic when crate not found #209

Closed
theduke opened this issue Apr 10, 2018 · 4 comments
Closed

Panic when crate not found #209

theduke opened this issue Apr 10, 2018 · 4 comments

Comments

@theduke
Copy link

theduke commented Apr 10, 2018

Just upgrade my nightly to (4b9b70c39 2018-04-09), and it seems that cargo-add does not handle non-existant crates gracefully anymore.

I mistyped a crate (serde_dervie) and got this error:

Command failed due to unhandled error: Invalid JSON (the crate may not exist)
Caused by: missing field `versions` at line 1 column 35
Backtrace: stack backtrace:
   0:     0x55b7225d188c - backtrace::backtrace::trace::heda8b332efd986ab
   1:     0x55b7225d0072 - backtrace::capture::Backtrace::new::h727f6efd73fa06c5
   2:     0x55b7225cfb2c - error_chain::make_backtrace::h4659a0010e2ff03a
   3:     0x55b722448398 - <core::result::Result<T, E> as cargo_edit::errors::ResultExt<T>>::chain_err::h49285f85800c0898
   4:     0x55b72245474b - cargo_edit::fetch::get_latest_dependency::h99ea9f82808078df
   5:     0x55b72243bfb7 - cargo_add::args::Args::parse_dependencies::h235fd639915b3939
   6:     0x55b7224421df - cargo_add::main::h8de47bb74222f147
   7:     0x55b72243ab42 - std::rt::lang_start::{{closure}}::h97d95e748c99d208
   8:     0x55b722688137 - std::rt::lang_start_internal::{{closure}}::h81dab1c80d2e9e5d
                        at libstd/rt.rs:59
                         - std::panicking::try::do_call::h7973a503106e370a
                        at libstd/panicking.rs:305
   9:     0x55b722699afe - __rust_maybe_catch_panic
                        at libpanic_unwind/lib.rs:102
  10:     0x55b72267dab5 - std::panicking::try::h21b7d973eaa3febb
                        at libstd/panicking.rs:284
                         - std::panic::catch_unwind::h07d8c36256237e04
                        at libstd/panic.rs:361
                         - std::rt::lang_start_internal::hde534450bd97bd5e
                        at libstd/rt.rs:58
  11:     0x55b722442ef3 - main
  12:     0x7fddf0444f49 - __libc_start_main
  13:     0x55b722438db9 - _start
  14:                0x0 - <unknown>
@ordian
Copy link
Collaborator

ordian commented Apr 10, 2018

I wasn't able to reproduce the panic neither with 0.3.0-beta.1, nor with master branch ( 88b3e49). What version of cargo-edit are you using?

❯ rustc +nightly --version                                               
rustc 1.27.0-nightly (4b9b70c39 2018-04-09)
❯ cargo +nightly add serde_dervie
Command failed due to unhandled error: Invalid JSON (the crate may not exist)

Caused by: missing field `versions` at line 1 column 35

As for the unhelpful message, we should fix it (@bjgill I thought #200 was supposed to fix this?).

@bjgill
Copy link
Collaborator

bjgill commented Apr 10, 2018

Thanks for the report. It looks like what you're seeing is the expected output from attempting to add a crate that doesn't actually exist whilst having backtrace enabled.

error-chain claims to capture a backtrace if the RUST_BACKTRACE environment variable is set - which we then print out if available. @theduke - do you have RUST_BACKTRACE set?

If this is indeed the case, whilst expected, this is nonetheless a self-evidently poor user experience. I think there are probably a couple of things we could/should do:

  1. Attempting to add a non-existent crate is probably not uncommon. We should ensure that we detect it reliably and then offer a better error message.
  2. People who've set the RUST_BACKTRACE environment variable might not expect the effect this has on our error reporting. We could hide it better (possibly with another environment variable?), or remove the feature altogether?

(as I remember, #200 only prevented us from hitting that error in the specific case where you were trying to upgrade a path dependency. I don't think it did anything to improve handling of the general error case of attempting to add a non-existent crate)

@theduke
Copy link
Author

theduke commented Apr 11, 2018

@bjgill yes I indeed have RUST_BACKTRACE, so that explains it, although still the behaviour seems to have changed because I remember gettign nicer error messages.

I think it's a reasonable to assume that a lot of developers using cargo-edit have it set, so your PR is appreciated.

bors bot added a commit that referenced this issue Jun 8, 2018
210: Add specific error for a missing crate r=ordian a=bjgill

The new error message is:

```
$ cargo add lets_hope_nobody_ever_publishes_a_crate_with_this_name
Command failed due to unhandled error: The crate `lets_hope_nobody_ever_publishes_a_crate_with_this_name` could not be found on crates.io.

Caused by: https://crates.io/api/v1/crates/lets_hope_nobody_ever_publishes_a_crate_with_this_name: Client Error: 404 Not Found
```

This is a partial fix for #209 (doesn't sort out the backtrace issue).

This PR also ensures that we catch errors when fetching from crates.io correctly. Previously, we only noticed if the JSON was in an unexpected format. Now, we check the status of the response first.

Also bumps the rustfmt version to the version shipped with stable 1.25.0.

Co-authored-by: Benjamin Gill <[email protected]>
@epage
Copy link
Collaborator

epage commented Sep 21, 2021

I think this can now be closed.

@ordian ordian closed this as completed Sep 21, 2021
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

No branches or pull requests

4 participants