Skip to content

Commit 050f809

Browse files
committed
migrate from failure to anyhow
1 parent 3ebd955 commit 050f809

25 files changed

+178
-188
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212

1313
### Changed
1414

15+
- **BREAKING** migrated generic error return types from `failure::Error` to `anyhow::Error`.
16+
- **BREAKING** `prepare::PrepareError` is now based on `thiserror::Error`, and not `failure::Fail` any more.
17+
- **BREAKING** `toolchain::ToolchainError` is now based on `thiserror::Error`, and not `failure::Fail` any more.
1518
- Update `attohttpc` dependency to 0.28
1619
- Update `base64` dependency to 0.22
1720
- Update `env_logger` dependency to 0.11

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ unstable-toolchain-ci = []
1818

1919
[dependencies]
2020
http = "1.1.0"
21-
failure = "0.1.3"
21+
anyhow = { version = "1.0.68", features = ["backtrace"]}
2222
futures-util = "0.3.5"
2323
log = "0.4.6"
2424
tokio = { version = "1.0", features = ["process", "time", "io-util", "rt", "rt-multi-thread"] }

src/build.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cmd::{Command, MountKind, Runnable, SandboxBuilder};
22
use crate::prepare::Prepare;
33
use crate::{Crate, Toolchain, Workspace};
4-
use failure::Error;
4+
use anyhow::Result;
55
use std::path::PathBuf;
66
use std::vec::Vec;
77

@@ -131,7 +131,7 @@ impl<'a> BuildBuilder<'a> {
131131
/// })?;
132132
/// # Ok(())
133133
/// # }
134-
pub fn run<R, F: FnOnce(&Build) -> Result<R, Error>>(self, f: F) -> Result<R, Error> {
134+
pub fn run<R, F: FnOnce(&Build) -> Result<R>>(self, f: F) -> Result<R> {
135135
self.build_dir
136136
.run(self.toolchain, self.krate, self.sandbox, self.patches, f)
137137
}
@@ -181,14 +181,14 @@ impl BuildDirectory {
181181
}
182182
}
183183

184-
pub(crate) fn run<R, F: FnOnce(&Build) -> Result<R, Error>>(
184+
pub(crate) fn run<R, F: FnOnce(&Build) -> Result<R>>(
185185
&mut self,
186186
toolchain: &Toolchain,
187187
krate: &Crate,
188188
sandbox: SandboxBuilder,
189189
patches: Vec<CratePatch>,
190190
f: F,
191-
) -> Result<R, Error> {
191+
) -> Result<R> {
192192
let source_dir = self.source_dir();
193193
if source_dir.exists() {
194194
crate::utils::remove_dir_all(&source_dir)?;
@@ -209,7 +209,7 @@ impl BuildDirectory {
209209
}
210210

211211
/// Remove all the contents of the build directory, freeing disk space.
212-
pub fn purge(&mut self) -> Result<(), Error> {
212+
pub fn purge(&mut self) -> Result<()> {
213213
let build_dir = self.build_dir();
214214
if build_dir.exists() {
215215
crate::utils::remove_dir_all(&build_dir)?;
@@ -322,7 +322,7 @@ impl<'ws> Build<'ws> {
322322
/// networking is disabled.
323323
#[cfg(any(feature = "unstable", doc))]
324324
#[cfg_attr(docs_rs, doc(cfg(feature = "unstable")))]
325-
pub fn fetch_build_std_dependencies(&self, targets: &[&str]) -> Result<(), Error> {
325+
pub fn fetch_build_std_dependencies(&self, targets: &[&str]) -> Result<()> {
326326
crate::prepare::fetch_deps(
327327
&self.dir.workspace,
328328
self.toolchain,

src/crates/git.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::CrateTrait;
22
use crate::cmd::{Command, ProcessLinesActions};
33
use crate::prepare::PrepareError;
44
use crate::Workspace;
5-
use failure::{Error, ResultExt};
5+
use anyhow::{Context as _, Result};
66
use log::{info, warn};
77
use std::path::{Path, PathBuf};
88

@@ -63,7 +63,7 @@ impl GitRepo {
6363
}
6464

6565
impl CrateTrait for GitRepo {
66-
fn fetch(&self, workspace: &Workspace) -> Result<(), Error> {
66+
fn fetch(&self, workspace: &Workspace) -> Result<()> {
6767
// The credential helper that suppresses the password prompt shows this message when a
6868
// repository requires authentication:
6969
//
@@ -86,7 +86,7 @@ impl CrateTrait for GitRepo {
8686
.cd(&path)
8787
.process_lines(&mut detect_private_repositories)
8888
.run()
89-
.with_context(|_| format!("failed to update {}", self.url))
89+
.with_context(|| format!("failed to update {}", self.url))
9090
} else {
9191
info!("cloning repository {}", self.url);
9292
Command::new(workspace, "git")
@@ -95,7 +95,7 @@ impl CrateTrait for GitRepo {
9595
.args(&[&path])
9696
.process_lines(&mut detect_private_repositories)
9797
.run()
98-
.with_context(|_| format!("failed to clone {}", self.url))
98+
.with_context(|| format!("failed to clone {}", self.url))
9999
};
100100

101101
if private_repository && res.is_err() {
@@ -105,20 +105,20 @@ impl CrateTrait for GitRepo {
105105
}
106106
}
107107

108-
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
108+
fn purge_from_cache(&self, workspace: &Workspace) -> Result<()> {
109109
let path = self.cached_path(workspace);
110110
if path.exists() {
111111
crate::utils::remove_dir_all(&path)?;
112112
}
113113
Ok(())
114114
}
115115

116-
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
116+
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<()> {
117117
Command::new(workspace, "git")
118118
.args(&["clone"])
119119
.args(&[self.cached_path(workspace).as_path(), dest])
120120
.run()
121-
.with_context(|_| format!("failed to checkout {}", self.url))?;
121+
.with_context(|| format!("failed to checkout {}", self.url))?;
122122
Ok(())
123123
}
124124
}

src/crates/local.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CrateTrait;
22
use crate::Workspace;
3-
use failure::Error;
3+
use anyhow::Result;
44
use log::info;
55
use std::path::{Path, PathBuf};
66
use walkdir::WalkDir;
@@ -16,17 +16,17 @@ impl Local {
1616
}
1717

1818
impl CrateTrait for Local {
19-
fn fetch(&self, _workspace: &Workspace) -> Result<(), Error> {
19+
fn fetch(&self, _workspace: &Workspace) -> Result<()> {
2020
// There is no fetch to do for a local crate.
2121
Ok(())
2222
}
2323

24-
fn purge_from_cache(&self, _workspace: &Workspace) -> Result<(), Error> {
24+
fn purge_from_cache(&self, _workspace: &Workspace) -> Result<()> {
2525
// There is no cache to purge for a local crate.
2626
Ok(())
2727
}
2828

29-
fn copy_source_to(&self, _workspace: &Workspace, dest: &Path) -> Result<(), Error> {
29+
fn copy_source_to(&self, _workspace: &Workspace, dest: &Path) -> Result<()> {
3030
info!(
3131
"copying local crate from {} to {}",
3232
self.path.display(),
@@ -43,7 +43,7 @@ impl std::fmt::Display for Local {
4343
}
4444
}
4545

46-
fn copy_dir(src: &Path, dest: &Path) -> Result<(), Error> {
46+
fn copy_dir(src: &Path, dest: &Path) -> Result<()> {
4747
let src = crate::utils::normalize_path(src);
4848
let dest = crate::utils::normalize_path(dest);
4949

@@ -75,10 +75,10 @@ fn copy_dir(src: &Path, dest: &Path) -> Result<(), Error> {
7575

7676
#[cfg(test)]
7777
mod tests {
78-
use failure::Error;
78+
use anyhow::Result;
7979

8080
#[test]
81-
fn test_copy_dir() -> Result<(), Error> {
81+
fn test_copy_dir() -> Result<()> {
8282
let tmp_src = tempfile::tempdir()?;
8383
let tmp_dest = tempfile::tempdir()?;
8484

@@ -99,7 +99,7 @@ mod tests {
9999
}
100100

101101
#[test]
102-
fn test_no_copy_target() -> Result<(), Error> {
102+
fn test_no_copy_target() -> Result<()> {
103103
let (src, dest) = (tempfile::tempdir()?, tempfile::tempdir()?);
104104
std::fs::create_dir(src.path().join("target"))?;
105105
std::fs::write(
@@ -116,7 +116,7 @@ mod tests {
116116
}
117117

118118
#[test]
119-
fn test_copy_symlinks() -> Result<(), Error> {
119+
fn test_copy_symlinks() -> Result<()> {
120120
use std::{fs, os, path::Path};
121121

122122
let tmp_src = tempfile::tempdir()?;

src/crates/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ mod local;
33
mod registry;
44

55
use crate::Workspace;
6-
use failure::Error;
6+
use anyhow::Result;
77
use log::info;
88
use std::path::Path;
99

1010
pub use registry::AlternativeRegistry;
1111

1212
trait CrateTrait: std::fmt::Display {
13-
fn fetch(&self, workspace: &Workspace) -> Result<(), Error>;
14-
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error>;
15-
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error>;
13+
fn fetch(&self, workspace: &Workspace) -> Result<()>;
14+
fn purge_from_cache(&self, workspace: &Workspace) -> Result<()>;
15+
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<()>;
1616
}
1717

1818
enum CrateType {
@@ -56,12 +56,12 @@ impl Crate {
5656

5757
/// Fetch the crate's source code and cache it in the workspace. This method will reach out to
5858
/// the network for some crate types.
59-
pub fn fetch(&self, workspace: &Workspace) -> Result<(), Error> {
59+
pub fn fetch(&self, workspace: &Workspace) -> Result<()> {
6060
self.as_trait().fetch(workspace)
6161
}
6262

6363
/// Remove the cached copy of this crate. The method will do nothing if the crate isn't cached.
64-
pub fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
64+
pub fn purge_from_cache(&self, workspace: &Workspace) -> Result<()> {
6565
self.as_trait().purge_from_cache(workspace)
6666
}
6767

@@ -75,7 +75,7 @@ impl Crate {
7575
}
7676
}
7777

78-
pub(crate) fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
78+
pub(crate) fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<()> {
7979
if dest.exists() {
8080
info!(
8181
"crate source directory {} already exists, cleaning it up",

src/crates/registry.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CrateTrait;
22
use crate::Workspace;
3-
use failure::{Error, ResultExt};
3+
use anyhow::{Context as _, Result};
44
use flate2::read::GzDecoder;
55
use log::info;
66
use std::fs::File;
@@ -88,7 +88,7 @@ impl RegistryCrate {
8888
.join(format!("{}-{}.crate", self.name, self.version))
8989
}
9090

91-
fn fetch_url(&self, workspace: &Workspace) -> Result<String, Error> {
91+
fn fetch_url(&self, workspace: &Workspace) -> Result<String> {
9292
match &self.registry {
9393
Registry::CratesIo => Ok(format!(
9494
"{0}/{1}/{1}-{2}.crate",
@@ -122,7 +122,7 @@ impl RegistryCrate {
122122
git2::build::RepoBuilder::new()
123123
.fetch_options(fo)
124124
.clone(url, &index_path)
125-
.with_context(|_| format!("unable to update_index at {}", url))?;
125+
.with_context(|| format!("unable to update_index at {}", url))?;
126126
info!("cloned registry index");
127127
}
128128
let config = std::fs::read_to_string(index_path.join("config.json"))?;
@@ -151,7 +151,7 @@ impl RegistryCrate {
151151
}
152152

153153
impl CrateTrait for RegistryCrate {
154-
fn fetch(&self, workspace: &Workspace) -> Result<(), Error> {
154+
fn fetch(&self, workspace: &Workspace) -> Result<()> {
155155
let local = self.cache_path(workspace);
156156
if local.exists() {
157157
info!("crate {} {} is already in cache", self.name, self.version);
@@ -173,15 +173,15 @@ impl CrateTrait for RegistryCrate {
173173
Ok(())
174174
}
175175

176-
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
176+
fn purge_from_cache(&self, workspace: &Workspace) -> Result<()> {
177177
let path = self.cache_path(workspace);
178178
if path.exists() {
179179
crate::utils::remove_file(&path)?;
180180
}
181181
Ok(())
182182
}
183183

184-
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
184+
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<()> {
185185
let cached = self.cache_path(workspace);
186186
let mut file = File::open(cached)?;
187187
let mut tar = Archive::new(GzDecoder::new(BufReader::new(&mut file)));
@@ -194,12 +194,10 @@ impl CrateTrait for RegistryCrate {
194194
);
195195
if let Err(err) = unpack_without_first_dir(&mut tar, dest) {
196196
let _ = crate::utils::remove_dir_all(dest);
197-
Err(err
198-
.context(format!(
199-
"unable to download {} version {}",
200-
self.name, self.version
201-
))
202-
.into())
197+
Err(err.context(format!(
198+
"unable to download {} version {}",
199+
self.name, self.version
200+
)))
203201
} else {
204202
Ok(())
205203
}
@@ -218,7 +216,7 @@ impl std::fmt::Display for RegistryCrate {
218216
}
219217
}
220218

221-
fn unpack_without_first_dir<R: Read>(archive: &mut Archive<R>, path: &Path) -> Result<(), Error> {
219+
fn unpack_without_first_dir<R: Read>(archive: &mut Archive<R>, path: &Path) -> Result<()> {
222220
let entries = archive.entries()?;
223221
for entry in entries {
224222
let mut entry = entry?;

src/inside_docker.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cmd::Command;
22
use crate::workspace::Workspace;
3+
use anyhow::Result;
34
use base64::{engine::general_purpose::STANDARD as b64, Engine};
4-
use failure::Error;
55
use getrandom::getrandom;
66
use log::info;
77

@@ -12,7 +12,7 @@ pub(crate) struct CurrentContainer {
1212
}
1313

1414
impl CurrentContainer {
15-
pub(crate) fn detect(workspace: &Workspace) -> Result<Option<Self>, Error> {
15+
pub(crate) fn detect(workspace: &Workspace) -> Result<Option<Self>> {
1616
if let Some(id) = probe_container_id(workspace)? {
1717
info!("inspecting the current container");
1818
let inspect = Command::new(workspace, "docker")
@@ -23,7 +23,7 @@ impl CurrentContainer {
2323
let content = inspect.stdout_lines().join("\n");
2424
let mut metadata: Vec<Metadata> = serde_json::from_str(&content)?;
2525
if metadata.len() != 1 {
26-
failure::bail!("invalid output returned by `docker inspect`");
26+
anyhow::bail!("invalid output returned by `docker inspect`");
2727
}
2828
Ok(Some(CurrentContainer {
2929
metadata: metadata.pop().unwrap(),
@@ -45,7 +45,7 @@ impl CurrentContainer {
4545
/// This function uses a simpler but slower method to get the ID: a file with a random string is
4646
/// created in the temp directory, the list of all the containers is fetched from Docker and then
4747
/// `cat` is executed inside each of them to check whether they have the same random string.
48-
pub(crate) fn probe_container_id(workspace: &Workspace) -> Result<Option<String>, Error> {
48+
pub(crate) fn probe_container_id(workspace: &Workspace) -> Result<Option<String>> {
4949
info!("detecting the ID of the container where rustwide is running");
5050

5151
// Create the probe on the current file system

src/native/unix.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CurrentUser;
22
use crate::cmd::KillFailedError;
3-
use failure::Error;
3+
use anyhow::Result;
44
use nix::{
55
sys::signal::{kill, Signal},
66
unistd::{Gid, Pid, Uid},
@@ -29,7 +29,7 @@ pub(crate) fn current_user() -> Option<CurrentUser> {
2929
})
3030
}
3131

32-
fn executable_mode_for(path: &Path) -> Result<u32, Error> {
32+
fn executable_mode_for(path: &Path) -> Result<u32> {
3333
let metadata = path.metadata()?;
3434

3535
let user = current_user().unwrap();
@@ -43,14 +43,14 @@ fn executable_mode_for(path: &Path) -> Result<u32, Error> {
4343
}
4444
}
4545

46-
pub(crate) fn is_executable<P: AsRef<Path>>(path: P) -> Result<bool, Error> {
46+
pub(crate) fn is_executable<P: AsRef<Path>>(path: P) -> Result<bool> {
4747
let path = path.as_ref();
4848

4949
let expected_mode = executable_mode_for(path)?;
5050
Ok(path.metadata()?.mode() & expected_mode == expected_mode)
5151
}
5252

53-
pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Result<(), Error> {
53+
pub(crate) fn make_executable<P: AsRef<Path>>(path: P) -> Result<()> {
5454
let path = path.as_ref();
5555

5656
// Set the executable and readable bits on the file

0 commit comments

Comments
 (0)