aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/main.rs
diff options
context:
space:
mode:
authorPascal Hertleif <[email protected]>2019-11-13 19:51:57 +0000
committerPascal Hertleif <[email protected]>2019-11-13 19:51:57 +0000
commit5075c77957e127be51e1b5271567f906abeb50c6 (patch)
treec8e8d8f29011ad2b2472a201561f3f3dc1627d8f /xtask/src/main.rs
parent5e3c1c2b5f63e57f98a7d02f75a559d225341b1c (diff)
Use anyhow::Result in xtask, add contexts
This builds on #2231 but was actually done before that. You see, the cause for #2231 was that I got this error message: Error: Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }) } Just switching to `anyhow::Result` got me stack traces (when setting `RUST_LIB_BACKTRACE=1`) that at least showed stack backtrace: 0: std::backtrace::Backtrace::create 1: std::backtrace::Backtrace::capture 2: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from 3: xtask::install_server 4: xtask::install 5: xtask::main 6: std::rt::lang_start::{{closure}} 7: std::panicking::try::do_call 8: __rust_maybe_catch_panic 9: std::rt::lang_start_internal 10: std::rt::lang_start 11: main With the added contexts (not at all exhaustive), the error became Error: install server Caused by: 0: build AutoCfg with target directory 1: No such file or directory (os error 2) Since anyhow is such a small thing (no new transitive dependencies!), and in general gives you `Result<T, Box<dyn Error>>` on steroids, I think this a nice small change. The only slightly annoying thing was to replace all the `Err(format!(…))?` calls (haven't even looked at whether we can make it support wrapping strings though), but the `bail!` macro is shorter anyway :)
Diffstat (limited to 'xtask/src/main.rs')
-rw-r--r--xtask/src/main.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 84842b428..7eab1c949 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -9,6 +9,7 @@
9//! `.cargo/config`. 9//! `.cargo/config`.
10mod help; 10mod help;
11 11
12use anyhow::Context;
12use autocfg; 13use autocfg;
13use core::fmt::Write; 14use core::fmt::Write;
14use core::str; 15use core::str;
@@ -114,21 +115,21 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
114 write!(&mut invalid_flags, "{}, ", flag)?; 115 write!(&mut invalid_flags, "{}, ", flag)?;
115 } 116 }
116 let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); 117 let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
117 Err(format!("Invalid flags: {}", invalid_flags).into()) 118 anyhow::bail!("Invalid flags: {}", invalid_flags)
118 } else { 119 } else {
119 Err(e.to_string().into()) 120 anyhow::bail!(e.to_string())
120 } 121 }
121} 122}
122 123
123fn install(opts: InstallOpt) -> Result<()> { 124fn install(opts: InstallOpt) -> Result<()> {
124 if cfg!(target_os = "macos") { 125 if cfg!(target_os = "macos") {
125 fix_path_for_mac()? 126 fix_path_for_mac().context("Fix path for mac")?
126 } 127 }
127 if let Some(server) = opts.server { 128 if let Some(server) = opts.server {
128 install_server(server)?; 129 install_server(server).context("install server")?;
129 } 130 }
130 if let Some(client) = opts.client { 131 if let Some(client) = opts.client {
131 install_client(client)?; 132 install_client(client).context("install client")?;
132 } 133 }
133 Ok(()) 134 Ok(())
134} 135}
@@ -140,7 +141,7 @@ fn fix_path_for_mac() -> Result<()> {
140 const ROOT_DIR: &str = ""; 141 const ROOT_DIR: &str = "";
141 let home_dir = match env::var("HOME") { 142 let home_dir = match env::var("HOME") {
142 Ok(home) => home, 143 Ok(home) => home,
143 Err(e) => Err(format!("Failed getting HOME from environment with error: {}.", e))?, 144 Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e),
144 }; 145 };
145 146
146 [ROOT_DIR, &home_dir] 147 [ROOT_DIR, &home_dir]
@@ -154,12 +155,12 @@ fn fix_path_for_mac() -> Result<()> {
154 if !vscode_path.is_empty() { 155 if !vscode_path.is_empty() {
155 let vars = match env::var_os("PATH") { 156 let vars = match env::var_os("PATH") {
156 Some(path) => path, 157 Some(path) => path,
157 None => Err("Could not get PATH variable from env.")?, 158 None => anyhow::bail!("Could not get PATH variable from env."),
158 }; 159 };
159 160
160 let mut paths = env::split_paths(&vars).collect::<Vec<_>>(); 161 let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
161 paths.append(&mut vscode_path); 162 paths.append(&mut vscode_path);
162 let new_paths = env::join_paths(paths)?; 163 let new_paths = env::join_paths(paths).context("build env PATH")?;
163 env::set_var("PATH", &new_paths); 164 env::set_var("PATH", &new_paths);
164 } 165 }
165 166
@@ -198,7 +199,7 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
198 199
199 let code_binary = match code_binary { 200 let code_binary = match code_binary {
200 Some(it) => it, 201 Some(it) => it,
201 None => Err("Can't execute `code --version`. Perhaps it is not in $PATH?")?, 202 None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"),
202 }; 203 };
203 204
204 Cmd { 205 Cmd {
@@ -219,8 +220,10 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
219 .run_with_output()?; 220 .run_with_output()?;
220 221
221 if !str::from_utf8(&output.stdout)?.contains("ra-lsp") { 222 if !str::from_utf8(&output.stdout)?.contains("ra-lsp") {
222 Err("Could not install the Visual Studio Code extension. \ 223 anyhow::bail!(
223 Please make sure you have at least NodeJS 10.x installed and try again.")?; 224 "Could not install the Visual Studio Code extension. \
225 Please make sure you have at least NodeJS 10.x installed and try again."
226 );
224 } 227 }
225 228
226 Ok(()) 229 Ok(())