os-lib icon indicating copy to clipboard operation
os-lib copied to clipboard

Automatically pump streams with `os.Inherit` on changed `System.out` `System.err` `System.in`

Open lolgab opened this issue 3 weeks ago • 0 comments

In Mill server-client mode the standard streams are changed using System.setOut, etc. methods so they can be propagated from the server to the client and shown to the user's console. Unfortunately, when running processes with os.proc and os.Inherit the changes done with System.setOut, etc. are not honored, since the java.lang.ProcessBuilder doesn't honor the changes in System streams. This issue is about natively support honoring the changes in streams in os.proc without users having to manually redirect the output and the input via pipes. Since there is no way to know if the streams were changed, os-lib would need to add an API to allow users to set the original streams, like:

os.proc.setOriginalSystemOut(System.out)
os.proc.setOriginalSystemErr(System.err)
os.proc.setOriginalSystemIn(System.in)

// or
os.proc.setOriginalStreams()
// which would do the same things but on the background

So when a process is executed and System.in ne os.proc.originalSystemIn && stdin == os.Inherit it would pump the data to the System.in using os.Pipe (as we currently do in Mill), since it assumes it is not the original System.in. The same thing would happen with System.out and System.err.

To work around this problem, Mill implemented some helper methods, but they are bespoke and are not what users would want to write. In fact, many plugins and users' builds in the wild just use os.proc with os.Inherit since it's the easiest way, and then they see broken output in server-client mode.

lolgab avatar Jun 26 '24 11:06 lolgab