I have a very simple .NET5 Linux program (same behaviour on Core 3.1) which runs Nano and waits for it to terminate. If I redirect only one output stream then this code works as expected. Nano loads and I can interact exactly how I would if it was launched via Bash.
Now if I redirect stderr and stdout a number of odd things happen:
- The nano window does not fill the entire terminal
- Escape sequences appear on screen ^X as an example
- I can use the menus but I have to press the escape sequence then hit enter
As I understood it stdout and stderr are different streams, why are they causing these issues? If I run:
nano 2>> stderr.txt
I can see that nano produces no stderr at all and everything works as expected.
I updated my code to only redirect stderr and as expected everything works. So this issue is something to do with redirecting both output streams. Code has been run under WSL1, WSL2 and a real machine.
Code that works:
static async Task Main(string[] args)
{
var proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "/bin/nano";
proc.StartInfo.Arguments = "hello";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = true; // only redirect stdout
proc.StartInfo.RedirectStandardInput = false;
proc.Start();
var t1 = proc.StandardOutput.BaseStream.CopyToAsync(Console.OpenStandardOutput());
proc.WaitForExit();
await t1;
}
Code that doesn't:
static async Task Main(string[] args)
{
var proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "/bin/nano";
proc.StartInfo.Arguments = "hello";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = false;
proc.Start();
var t1 = proc.StandardOutput.BaseStream.CopyToAsync(Console.OpenStandardOutput());
var t2 = proc.StandardError.BaseStream.CopyToAsync(Console.OpenStandardError());
proc.WaitForExit();
await t1;
await t2;
}
I'm using nano as a good example here. The actual program works in a very similar way but produces stderr on output.