Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,198 changes: 606 additions & 592 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ repository = "killercup/cargo-edit"

[dependencies]
atty = { version = "0.2.14", optional = true }
anyhow = "1.0.28"
cargo_metadata = "0.9.1"
dirs = "2.0.2"
env_proxy = "0.4.0"
error-chain = "0.12.2"
failure = "0.1.7"
git2 = "0.13.0"
hex = "0.4.2"
regex = "1.3.5"
Expand All @@ -69,6 +68,7 @@ termcolor = "1.1.0"
toml = "0.5.6"
toml_edit = "0.1.5"
url = "2.1.1"
thiserror = "1.0.11"

[dependencies.semver]
features = ["serde"]
Expand All @@ -90,3 +90,4 @@ rm = ["cli"]
upgrade = ["cli"]
cli = ["atty", "structopt"]
test-external-apis = []
backtrace = []
11 changes: 6 additions & 5 deletions src/bin/add/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Handle `cargo add` arguments

use anyhow::{Context, Result};
use cargo_edit::{find, registry_url, Dependency};
use cargo_edit::{get_latest_dependency, CrateName};
use semver;
Expand Down Expand Up @@ -142,7 +143,7 @@ pub struct Args {
}

fn parse_version_req(s: &str) -> Result<&str> {
semver::VersionReq::parse(s).chain_err(|| "Invalid dependency version requirement")?;
semver::VersionReq::parse(s).with_context(|| "Invalid dependency version requirement")?;
Ok(s)
}

Expand Down Expand Up @@ -175,7 +176,7 @@ impl Args {
if let Some(ref url) = self.git {
let url = url.clone();
let version = dependency.version().unwrap().to_string();
return Err(ErrorKind::GitUrlWithVersion(url, version).into());
return Err(Error::GitUrlWithVersion(url, version).into());
}

if let Some(ref path) = self.path {
Expand Down Expand Up @@ -239,15 +240,15 @@ impl Args {
if self.crates.len() > 1
&& (self.git.is_some() || self.path.is_some() || self.vers.is_some())
{
return Err(ErrorKind::MultipleCratesWithGitOrPathOrVers.into());
return Err(Error::MultipleCratesWithGitOrPathOrVers.into());
}

if self.crates.len() > 1 && self.rename.is_some() {
return Err(ErrorKind::MultipleCratesWithRename.into());
return Err(Error::MultipleCratesWithRename.into());
}

if self.crates.len() > 1 && self.features.is_some() {
return Err(ErrorKind::MultipleCratesWithFeatures.into());
return Err(Error::MultipleCratesWithFeatures.into());
}

self.crates
Expand Down
74 changes: 36 additions & 38 deletions src/bin/add/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
unused_import_braces,
unused_qualifications
)]

#[macro_use]
extern crate error_chain;
// if the user has compiled with the `backtrace` feature, enable the stdlib `backtrace` feature
#![cfg_attr(feature = "backtrace", feature(backtrace))]

use crate::args::{Args, Command};
use anyhow::Result;
use cargo_edit::{
find, manifest_from_pkgid, registry_url, update_registry_index, Dependency, Manifest,
};
Expand All @@ -28,40 +28,37 @@ use toml_edit::Item as TomlItem;
mod args;

mod errors {
error_chain! {
errors {
/// Specified a dependency with both a git URL and a version.
GitUrlWithVersion(git: String, version: String) {
description("Specified git URL with version")
display("Cannot specify a git URL (`{}`) with a version (`{}`).", git, version)
}
/// Specified multiple crates with path or git or vers
MultipleCratesWithGitOrPathOrVers {
description("Specified multiple crates with path or git or vers")
display("Cannot specify multiple crates with path or git or vers")
}
/// Specified multiple crates with renaming.
MultipleCratesWithRename {
description("Specified multiple crates with rename")
display("Cannot specify multiple crates with rename")
}
/// Specified multiple crates with features.
MultipleCratesWithFeatures {
description("Specified multiple crates with features")
display("Cannot specify multiple crates with features")
}
}
links {
CargoEditLib(::cargo_edit::Error, ::cargo_edit::ErrorKind);
}
foreign_links {
Io(::std::io::Error);
}
use thiserror::Error as ThisError;

#[derive(Debug, ThisError)]
pub enum Error {
/// Specified a dependency with both a git URL and a version.
#[error("Cannot specify a git URL (`{0}`) with a version (`{1}`).")]
GitUrlWithVersion(String, String),
/// Specified multiple crates with path or git or vers
#[error("Cannot specify multiple crates with path or git or vers")]
MultipleCratesWithGitOrPathOrVers,
/// Specified multiple crates with renaming.
#[error("Cannot specify multiple crates with rename")]
MultipleCratesWithRename,
/// Specified multiple crates with features.
#[error("Cannot specify multiple crates with features")]
MultipleCratesWithFeatures,

/// An error originating from the cargo-edit library
#[error(transparent)]
CargoEditLib(#[from] cargo_edit::Error),

/// An IO error
#[error(transparent)]
Io(#[from] std::io::Error),

/// An error from the semver crate
#[error(transparent)]
SemVerParse(#[from] semver::ReqParseError),
}
}

use crate::errors::*;

fn print_msg(dep: &Dependency, section: &[String], optional: bool) -> Result<()> {
let colorchoice = if atty::is(atty::Stream::Stdout) {
ColorChoice::Auto
Expand Down Expand Up @@ -154,12 +151,13 @@ fn main() {
if let Err(err) = handle_add(&args) {
eprintln!("Command failed due to unhandled error: {}\n", err);

for e in err.iter().skip(1) {
eprintln!("Caused by: {}", e);
for source in err.chain().skip(1) {
eprintln!("Caused by: {}", source);
}

if let Some(backtrace) = err.backtrace() {
eprintln!("Backtrace: {:?}", backtrace);
Comment on lines -161 to -162
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it not possible to keep backtraces with anyhow? :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyhow can display backtraces as long as it was complied by nightly, since the stdlib backtrace feature is still unstable. I've tried to convince failure::Fail::backtrace to accept an anyhow::Error and try to get a backtrace out of it, but haven't had any luck. anyhow::Error will deref to a dyn Error + Send + Sync + 'static, but Fail::backtrace complains that it is unsized.

I've added a feature that will allow a user to compile with backtrace support as long as they are on nightly, but if there is a better way I'd be more than happy to use it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@killercup are backtraces used for identifying bugs in cargo-edit if it fails to run on some input? maybe it's not that helpful for the users after all

Copy link

@yaahc yaahc Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have sworn that anyhow brought in a dependency on backtrace-rs on stable but apparently not. I don't mean to shamelessly plug my own stuff but if you want stable backtraces you can swap from anyhow to eyre which is a fork that adds support for custom context types, which you could define to include a backtrace::Backtrace

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Losing backtraces is kind of a shame from a technical standpoint, but I don't think they are being relied on by anyone using cargo-edit, because we have such amazing error descriptions! 😉

Kidding aside, I think this is fine. When we finally end up packaging binaries we can compile them with nightly and get some backtraces for free.

@yaahc, I just got what the name is about. Amazing. I'm also totally fine with using your fork. Or switching to it in a later PR.

#[cfg(feature = "backtrace")]
{
eprintln!("Backtrace: {:?}", err.backtrace());
}

process::exit(1);
Expand Down
34 changes: 19 additions & 15 deletions src/bin/rm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
unused_import_braces,
unused_qualifications
)]
// if the user has compiled with the `backtrace` feature, enable the stdlib `backtrace` feature
#![cfg_attr(feature = "backtrace", feature(backtrace))]

#[macro_use]
extern crate error_chain;

use anyhow::Result;
use cargo_edit::{manifest_from_pkgid, Manifest};
use std::borrow::Cow;
use std::io::Write;
Expand All @@ -23,16 +23,19 @@ use structopt::StructOpt;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

mod errors {
error_chain! {
links {
CargoEditLib(::cargo_edit::Error, ::cargo_edit::ErrorKind);
}
foreign_links {
Io(::std::io::Error);
}
use thiserror::Error as ThisError;

#[derive(Debug, ThisError)]
pub enum Error {
/// An error originating from the cargo-edit library
#[error(transparent)]
CargoEditLib(#[from] cargo_edit::Error),

/// An IO error
#[error(transparent)]
Io(#[from] std::io::Error),
}
}
use crate::errors::*;

#[derive(Debug, StructOpt)]
#[structopt(bin_name = "cargo")]
Expand Down Expand Up @@ -139,12 +142,13 @@ fn main() {
if let Err(err) = handle_rm(&args) {
eprintln!("Command failed due to unhandled error: {}\n", err);

for e in err.iter().skip(1) {
eprintln!("Caused by: {}", e);
for source in err.chain().skip(1) {
eprintln!("Caused by: {}", source);
}

if let Some(backtrace) = err.backtrace() {
eprintln!("Backtrace: {:?}", backtrace);
#[cfg(feature = "backtrace")]
{
eprintln!("Backtrace: {:?}", err.backtrace());
}

process::exit(1);
Expand Down
Loading