Skip to content

Commit b562288

Browse files
authored
fix(cli): exit on non-compilation Cargo errors, closes #3930 (#3942)
1 parent 81705bb commit b562288

File tree

12 files changed

+148
-79
lines changed

12 files changed

+148
-79
lines changed
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+
Exit CLI when Cargo returns a non-compilation error in `tauri dev`.

.changes/command-stdio-return.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
"api": patch
4+
---
5+
6+
**Breaking change:** The process Command API stdio lines now includes the trailing `\r`.

.changes/io-read-line-util.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": patch
3+
---
4+
5+
Added the `io` module with the `read_line` method.

core/tauri-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ json5 = { version = "0.4", optional = true }
3232
json-patch = "0.2"
3333
glob = { version = "0.3.0", optional = true }
3434
walkdir = { version = "2", optional = true }
35+
memchr = "2.4"
3536

3637
[target."cfg(target_os = \"linux\")".dependencies]
3738
heck = "0.4"

core/tauri-utils/src/io.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
//! IO helpers.
6+
7+
use std::io::BufRead;
8+
9+
/// Read a line breaking in both \n and \r.
10+
///
11+
/// Adapted from https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line
12+
pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::Result<usize> {
13+
let mut read = 0;
14+
loop {
15+
let (done, used) = {
16+
let available = match r.fill_buf() {
17+
Ok(n) => n,
18+
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
19+
Err(e) => return Err(e),
20+
};
21+
match memchr::memchr(b'\n', available) {
22+
Some(i) => {
23+
let end = i + 1;
24+
buf.extend_from_slice(&available[..end]);
25+
(true, end)
26+
}
27+
None => match memchr::memchr(b'\r', available) {
28+
Some(i) => {
29+
let end = i + 1;
30+
buf.extend_from_slice(&available[..end]);
31+
(true, end)
32+
}
33+
None => {
34+
buf.extend_from_slice(available);
35+
(false, available.len())
36+
}
37+
},
38+
}
39+
};
40+
r.consume(used);
41+
read += used;
42+
if done || used == 0 {
43+
if buf.ends_with(&[b'\n']) {
44+
buf.pop();
45+
}
46+
return Ok(read);
47+
}
48+
}
49+
}

core/tauri-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
1212
pub mod assets;
1313
pub mod config;
1414
pub mod html;
15+
pub mod io;
1516
pub mod platform;
1617
/// Prepare application resources and sidecars.
1718
#[cfg(feature = "resources")]

core/tauri/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ attohttpc = { version = "0.19", features = [ "json", "form" ], optional = true }
7878
open = { version = "2.0", optional = true }
7979
shared_child = { version = "1.0", optional = true }
8080
os_pipe = { version = "1.0", optional = true }
81-
memchr = { version = "2.4", optional = true }
8281
rfd = { version = "0.8", optional = true }
8382
raw-window-handle = "0.4.2"
8483
minisign-verify = { version = "0.2", optional = true }
@@ -138,7 +137,7 @@ http-api = [ "attohttpc" ]
138137
shell-open-api = [ "open", "regex", "tauri-macros/shell-scope" ]
139138
fs-extract-api = [ "zip" ]
140139
reqwest-client = [ "reqwest", "bytes" ]
141-
process-command-api = [ "shared_child", "os_pipe", "memchr" ]
140+
process-command-api = [ "shared_child", "os_pipe" ]
142141
dialog = [ "rfd" ]
143142
notification = [ "notify-rust" ]
144143
cli = [ "clap" ]

core/tauri/src/api/process/command.rs

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

55
use std::{
66
collections::HashMap,
7-
io::{BufRead, BufReader, Write},
7+
io::{BufReader, Write},
88
path::PathBuf,
99
process::{Command as StdCommand, Stdio},
1010
sync::{Arc, Mutex, RwLock},
@@ -384,7 +384,7 @@ fn spawn_pipe_reader<F: Fn(String) -> CommandEvent + Send + Copy + 'static>(
384384
let mut buf = Vec::new();
385385
loop {
386386
buf.clear();
387-
match read_command_output(&mut reader, &mut buf) {
387+
match tauri_utils::io::read_line(&mut reader, &mut buf) {
388388
Ok(n) => {
389389
if n == 0 {
390390
break;
@@ -407,52 +407,6 @@ fn spawn_pipe_reader<F: Fn(String) -> CommandEvent + Send + Copy + 'static>(
407407
});
408408
}
409409

410-
// adapted from https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line
411-
fn read_command_output<R: BufRead + ?Sized>(
412-
r: &mut R,
413-
buf: &mut Vec<u8>,
414-
) -> std::io::Result<usize> {
415-
let mut read = 0;
416-
loop {
417-
let (done, used) = {
418-
let available = match r.fill_buf() {
419-
Ok(n) => n,
420-
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
421-
Err(e) => return Err(e),
422-
};
423-
match memchr::memchr(b'\n', available) {
424-
Some(i) => {
425-
let end = i + 1;
426-
buf.extend_from_slice(&available[..end]);
427-
(true, end)
428-
}
429-
None => match memchr::memchr(b'\r', available) {
430-
Some(i) => {
431-
let end = i + 1;
432-
buf.extend_from_slice(&available[..end]);
433-
(true, end)
434-
}
435-
None => {
436-
buf.extend_from_slice(available);
437-
(false, available.len())
438-
}
439-
},
440-
}
441-
};
442-
r.consume(used);
443-
read += used;
444-
if done || used == 0 {
445-
if buf.ends_with(&[b'\n']) {
446-
buf.pop();
447-
}
448-
if buf.ends_with(&[b'\r']) {
449-
buf.pop();
450-
}
451-
return Ok(read);
452-
}
453-
}
454-
}
455-
456410
// tests for the commands functions.
457411
#[cfg(test)]
458412
mod test {

examples/api/src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)