From 40752ff67a853a1f61bb99e4756f6e2b617da0e3 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Fri, 9 Oct 2020 10:03:25 -0700 Subject: [PATCH] Remove ansi control characters from piped output When piping an sbt task to a file, the expectation is that sbt will not write ansi control characters and colors unless the users specifies that with, e.g. -D.sbt.color=true. With sbt 1.4.0, all output bytes are routed through the progress output processor which tries to ensure that progress lines are not interleaved with log lines. The issue was that the hasProgress flag was being set to true for the server process even when the formatEnabledInEnv flag was set to false. This caused each log line to have a leading clear screen before cursor ansi control code written which would appear in the output file. --- .../src/main/scala/sbt/internal/util/ProgressState.scala | 4 ++-- .../src/main/scala/sbt/internal/util/Terminal.scala | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/util-logging/src/main/scala/sbt/internal/util/ProgressState.scala b/internal/util-logging/src/main/scala/sbt/internal/util/ProgressState.scala index 329cd6ad8..e04c07fab 100644 --- a/internal/util-logging/src/main/scala/sbt/internal/util/ProgressState.scala +++ b/internal/util-logging/src/main/scala/sbt/internal/util/ProgressState.scala @@ -91,9 +91,9 @@ private[sbt] final class ProgressState( printStream: PrintStream, hasProgress: Boolean ): Unit = { - val canClearPrompt = currentLineBytes.get.isEmpty - addBytes(terminal, bytes) if (hasProgress) { + val canClearPrompt = currentLineBytes.get.isEmpty + addBytes(terminal, bytes) val toWrite = new ArrayBuffer[Byte] terminal.prompt match { case a: Prompt.AskUser if a.render.nonEmpty && canClearPrompt => diff --git a/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala b/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala index 839e41617..e34467002 100644 --- a/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala +++ b/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala @@ -331,7 +331,7 @@ object Terminal { private[sbt] def withStreams[T](isServer: Boolean)(f: => T): T = // In ci environments, don't touch the io streams unless run with -Dsbt.io.virtual=true if (System.getProperty("sbt.io.virtual", "") == "true" || (logFormatEnabled.getOrElse(true) && !isCI)) { - hasProgress.set(isServer) + hasProgress.set(isServer && formatEnabledInEnv) consoleTerminalHolder.set(newConsoleTerminal()) activeTerminal.set(consoleTerminalHolder.get) try withOut(withIn(f))