Browse Source

fix(cli): duplicated newlines on child process output (#8042)

Lucas Fernandes Nogueira 1 year ago
parent
commit
be8e5aa307

+ 7 - 0
.changes/fix-cmd-output.md

@@ -0,0 +1,7 @@
+---
+"@tauri-apps/cli": patch:bug
+"tauri-cli": patch:bug
+"tauri-bundler": patch:bug
+---
+
+Fixes duplicated newlines on command outputs.

+ 1 - 1
examples/api/src-tauri/Cargo.lock

@@ -3529,7 +3529,7 @@ checksum = "fd1ba337640d60c3e96bc6f0638a939b9c9a7f2c316a1598c279828b3d1dc8c5"
 
 [[package]]
 name = "tauri"
-version = "1.5.1"
+version = "1.5.2"
 dependencies = [
  "anyhow",
  "base64 0.21.2",

+ 11 - 13
tooling/bundler/src/bundle/common.rs

@@ -8,7 +8,7 @@ use log::debug;
 use std::{
   ffi::OsStr,
   fs::{self, File},
-  io::{self, BufReader, BufWriter},
+  io::{self, BufRead, BufReader, BufWriter},
   path::Path,
   process::{Command, ExitStatus, Output, Stdio},
   sync::{Arc, Mutex},
@@ -165,16 +165,15 @@ impl CommandExt for Command {
     let stdout_lines = Arc::new(Mutex::new(Vec::new()));
     let stdout_lines_ = stdout_lines.clone();
     std::thread::spawn(move || {
-      let mut buf = Vec::new();
+      let mut line = String::new();
       let mut lines = stdout_lines_.lock().unwrap();
       loop {
-        buf.clear();
-        if let Ok(0) = tauri_utils::io::read_line(&mut stdout, &mut buf) {
+        line.clear();
+        if let Ok(0) = stdout.read_line(&mut line) {
           break;
         }
-        debug!(action = "stdout"; "{}", String::from_utf8_lossy(&buf));
-        lines.extend(buf.clone());
-        lines.push(b'\n');
+        debug!(action = "stdout"; "{}", &line[0..line.len() - 1]);
+        lines.extend(line.as_bytes().to_vec());
       }
     });
 
@@ -182,16 +181,15 @@ impl CommandExt for Command {
     let stderr_lines = Arc::new(Mutex::new(Vec::new()));
     let stderr_lines_ = stderr_lines.clone();
     std::thread::spawn(move || {
-      let mut buf = Vec::new();
+      let mut line = String::new();
       let mut lines = stderr_lines_.lock().unwrap();
       loop {
-        buf.clear();
-        if let Ok(0) = tauri_utils::io::read_line(&mut stderr, &mut buf) {
+        line.clear();
+        if let Ok(0) = stderr.read_line(&mut line) {
           break;
         }
-        debug!(action = "stderr"; "{}", String::from_utf8_lossy(&buf));
-        lines.extend(buf.clone());
-        lines.push(b'\n');
+        debug!(action = "stderr"; "{}", &line[0..line.len() - 1]);
+        lines.extend(line.as_bytes().to_vec());
       }
     });
 

+ 0 - 3
tooling/cli/src/interface/rust/desktop.rs

@@ -207,9 +207,6 @@ fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
         break;
       }
       let _ = io_stderr.write_all(&buf);
-      if !buf.ends_with(&[b'\r']) {
-        let _ = io_stderr.write_all(b"\n");
-      }
       lines.push(String::from_utf8_lossy(&buf).into_owned());
     }
   });

+ 11 - 12
tooling/cli/src/lib.rs

@@ -24,6 +24,7 @@ use std::io::{BufReader, Write};
 use std::process::{exit, Command, ExitStatus, Output, Stdio};
 use std::{
   ffi::OsString,
+  io::BufRead,
   sync::{Arc, Mutex},
 };
 
@@ -226,16 +227,15 @@ impl CommandExt for Command {
     let stdout_lines = Arc::new(Mutex::new(Vec::new()));
     let stdout_lines_ = stdout_lines.clone();
     std::thread::spawn(move || {
-      let mut buf = Vec::new();
+      let mut line = String::new();
       let mut lines = stdout_lines_.lock().unwrap();
       loop {
-        buf.clear();
-        if let Ok(0) = tauri_utils::io::read_line(&mut stdout, &mut buf) {
+        line.clear();
+        if let Ok(0) = stdout.read_line(&mut line) {
           break;
         }
-        debug!(action = "stdout"; "{}", String::from_utf8_lossy(&buf));
-        lines.extend(buf.clone());
-        lines.push(b'\n');
+        debug!(action = "stdout"; "{}", &line[0..line.len() - 1]);
+        lines.extend(line.as_bytes().to_vec());
       }
     });
 
@@ -243,16 +243,15 @@ impl CommandExt for Command {
     let stderr_lines = Arc::new(Mutex::new(Vec::new()));
     let stderr_lines_ = stderr_lines.clone();
     std::thread::spawn(move || {
-      let mut buf = Vec::new();
+      let mut line = String::new();
       let mut lines = stderr_lines_.lock().unwrap();
       loop {
-        buf.clear();
-        if let Ok(0) = tauri_utils::io::read_line(&mut stderr, &mut buf) {
+        line.clear();
+        if let Ok(0) = stderr.read_line(&mut line) {
           break;
         }
-        debug!(action = "stderr"; "{}", String::from_utf8_lossy(&buf));
-        lines.extend(buf.clone());
-        lines.push(b'\n');
+        debug!(action = "stderr"; "{}", &line[0..line.len() - 1]);
+        lines.extend(line.as_bytes().to_vec());
       }
     });