Skip to content

Commit e0e5f77

Browse files
authored
feat(cli): improve cargo not found error message, closes #4428 (#4430)
1 parent 38f5db6 commit e0e5f77

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

.changes/cargo-not-found.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Improve error message when `cargo` is not installed.

tooling/cli/src/dev.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::{
2323
env::set_current_dir,
2424
ffi::OsStr,
2525
fs::FileType,
26-
io::{BufReader, Write},
26+
io::{BufReader, ErrorKind, Write},
2727
path::{Path, PathBuf},
2828
process::{exit, Command, Stdio},
2929
sync::{
@@ -495,8 +495,24 @@ fn start_app(
495495
command.stdout(os_pipe::dup_stdout().unwrap());
496496
command.stderr(Stdio::piped());
497497

498-
let child =
499-
SharedChild::spawn(&mut command).with_context(|| format!("failed to run {}", runner))?;
498+
let child = match SharedChild::spawn(&mut command) {
499+
Ok(c) => c,
500+
Err(e) => {
501+
if e.kind() == ErrorKind::NotFound {
502+
return Err(anyhow::anyhow!(
503+
"`{}` command not found.{}",
504+
runner,
505+
if runner == "cargo" {
506+
" Please follow the Tauri setup guide: https://tauri.app/v1/guides/getting-started/prerequisites"
507+
} else {
508+
""
509+
}
510+
));
511+
} else {
512+
return Err(e.into());
513+
}
514+
}
515+
};
500516
let child_arc = Arc::new(child);
501517
let child_stderr = child_arc.take_stderr().unwrap();
502518
let mut stderr = BufReader::new(child_stderr);

tooling/cli/src/interface/rust.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::{
66
fs::File,
7-
io::Read,
7+
io::{ErrorKind, Read},
88
path::{Path, PathBuf},
99
process::Command,
1010
str::FromStr,
@@ -101,18 +101,37 @@ struct CargoConfig {
101101
}
102102

103103
pub fn build_project(runner: String, args: Vec<String>) -> crate::Result<()> {
104-
let status = Command::new(&runner)
104+
match Command::new(&runner)
105105
.args(&["build", "--features=custom-protocol"])
106106
.args(args)
107107
.env("STATIC_VCRUNTIME", "true")
108-
.piped()?;
109-
if status.success() {
110-
Ok(())
111-
} else {
112-
Err(anyhow::anyhow!(
113-
"Result of `{} build` operation was unsuccessful",
114-
runner
115-
))
108+
.piped()
109+
{
110+
Ok(status) => {
111+
if status.success() {
112+
Ok(())
113+
} else {
114+
Err(anyhow::anyhow!(
115+
"Result of `{} build` operation was unsuccessful",
116+
runner
117+
))
118+
}
119+
}
120+
Err(e) => {
121+
if e.kind() == ErrorKind::NotFound {
122+
Err(anyhow::anyhow!(
123+
"`{}` command not found.{}",
124+
runner,
125+
if runner == "cargo" {
126+
" Please follow the Tauri setup guide: https://tauri.app/v1/guides/getting-started/prerequisites"
127+
} else {
128+
""
129+
}
130+
))
131+
} else {
132+
Err(e.into())
133+
}
134+
}
116135
}
117136
}
118137

tooling/cli/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ fn prettyprint_level(lvl: Level) -> &'static str {
176176
pub trait CommandExt {
177177
// The `pipe` function sets the stdout and stderr to properly
178178
// show the command output in the Node.js wrapper.
179-
fn piped(&mut self) -> Result<ExitStatus>;
179+
fn piped(&mut self) -> std::io::Result<ExitStatus>;
180180
fn output_ok(&mut self) -> crate::Result<()>;
181181
}
182182

183183
impl CommandExt for Command {
184-
fn piped(&mut self) -> crate::Result<ExitStatus> {
184+
fn piped(&mut self) -> std::io::Result<ExitStatus> {
185185
self.stdout(os_pipe::dup_stdout()?);
186186
self.stderr(os_pipe::dup_stderr()?);
187187
let program = self.get_program().to_string_lossy().into_owned();

0 commit comments

Comments
 (0)