tauri-apps / tauri-apps/plugins-workspace

[shell] Flushed data from spawned process is not sent to JS unless it ends in a newline

Open
#1,632 3 comments 0 reactions 0 assignees View on GitHub
plugin: shell type: bug
Dominant language
Rust
Stars
1.8k
Forks
602
Avg merge
4d 14h
Merged PRs (30d)
9

Description

I am communicating with a spawned process via `stdin` and `stdout` using something like the following:

```ts
const cmd = Command.create('example-program');
cmd.stdout.on('data', (data) => console.log(data));
cmd.stderr.on('data', (data) => console.log(data));

const proc = await cmd.spawn();
proc.write("This is a message to the spawned process");
```

The program being spawned listens for input from `stdin` and writes some output to `stdout` in response. The issue I'm running into is that while the the output is flushed correctly by the spawned program, it is not terminated with a newline character. As a result, the JS above receives all output _except_ for the last line, which it will receive as the first line of text in responsed to the subsequent `proc.write()` call.

To demonstrate, the following Rust code is a simple echo program:

```rust
use std::io;
use std::io::Write;

fn main() {
io::stdout().write_all(b"Type something: ").expect("Error writing to stdout");
io::stdout().flush().expect("Error flushing stdout");
loop {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).expect("Error reading from stdin");
io::stdout().write_all(b"You wrote: ").expect("Error writing to stdout");
io::stdout().write_all(buffer.as_bytes()).expect("Error writing to stdout");
io::stdout().write_all(b"Type something: ").expect("Error writing to stdout");
io::stdout().flush().expect("Error flushing stdout");
}
}

```

When executed from the command line, it behaves as follows:

https://github.com/user-attachments/assets/919918f2-dc99-4972-b849-1645939b3c0d

When attached to Tauri as a sidecar using the above code, the output in the console is as follows:

https://github.com/user-attachments/assets/dc0aa8dd-27f3-4e12-ae77-f2708d0084e2

Note specifically that the string "Type something:" is not written to the console until a newline character has been written to stdout.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.