Skip to content

Commit

Permalink
Fix ScalaModule#console by properly inheriting streams (#3500)
Browse files Browse the repository at this point in the history
`SystemStreamswithStreams(SystemStreams.original)` didn't quite do the
right thing because it continued to use the `PumpedProcess*put`s for
subprocesses rather than directly inheriting the streams. This fixes it.

Tested manually via `./mill dist.launcher && (cd
example/scalalib/basic/1-simple &&
../../../../out/dist/launcher.dest/run -i console)`, which previously
would print `Unable to create system terminal` and not allow keyboard
navigation in the REPL, and with this PR it no longer warns and keyboard
navigation works correctly

Fixes #3491
  • Loading branch information
lihaoyi authored Sep 10, 2024
1 parent 4ed83d9 commit 5badefe
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main/api/src/mill/api/SystemStreams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,32 @@ object SystemStreams {
val out = System.out
val err = System.err
try {
// If we are setting a stream back to its original value, make sure we reset
// `os.Inherit` to `os.InheritRaw` for that stream. This direct inheritance
// ensures that interactive applications involving console IO work, as the
// presence of a `PumpedProcess` would cause most interactive CLIs (e.g.
// scala console, REPL, etc.) to misbehave
val inheritIn =
if (systemStreams.in eq original.in) os.InheritRaw
else new PumpedProcessInput

val inheritOut =
if (systemStreams.out eq original.out) os.InheritRaw
else new PumpedProcessOutput(systemStreams.out)

val inheritErr =
if (systemStreams.err eq original.err) os.InheritRaw
else new PumpedProcessOutput(systemStreams.err)

System.setIn(systemStreams.in)
System.setOut(systemStreams.out)
System.setErr(systemStreams.err)
Console.withIn(systemStreams.in) {
Console.withOut(systemStreams.out) {
Console.withErr(systemStreams.err) {
os.Inherit.in.withValue(new PumpedProcessInput) {
os.Inherit.out.withValue(new PumpedProcessOutput(System.out)) {
os.Inherit.err.withValue(new PumpedProcessOutput(System.err)) {
os.Inherit.in.withValue(inheritIn) {
os.Inherit.out.withValue(inheritOut) {
os.Inherit.err.withValue(inheritErr) {
t
}
}
Expand Down

0 comments on commit 5badefe

Please sign in to comment.