Skip to content

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

Open
@atdyer

Description

@atdyer

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

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:

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:

Screen.Recording.2024-08-07.at.12.58.05.PM.mov

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

Screen.Recording.2024-08-07.at.1.00.25.PM.mov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions