From afbceb062fa4e6e777603abe548df28f27384995 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Tue, 3 Nov 2020 08:42:04 -0800 Subject: [PATCH] Only create console terminal if process has console Intellij import was broken in sbt 1.4.2 because we increased the scenarios in which virtual io is used. The problem wasn't actually virtual io but that we create a console terminal with jline3 and that could cause issues reading input from System.in. This can be fixed by only creating an instance of ConsoleTerminal if a console is actually available for the sbt process. When hasConsole is false, the consoleTerminalHolder will point to the SimpleTerminal whose inputStream method just reads directly from System.in. After making this change, I verified that intellij import worked again on windows. We also don't want to make a console terminal if it is a dumb terminal because jline 3 ends up having problems with the ConsoleTerminal. --- .../src/main/scala/sbt/internal/util/Terminal.scala | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 d4adac080..9d9d0cc2e 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 @@ -307,7 +307,9 @@ object Terminal { } } private[sbt] lazy val isAnsiSupported: Boolean = logFormatEnabled.getOrElse(useColorDefault) - private[this] val isDumbTerminal = "dumb" == System.getenv("TERM") + + private[this] val isDumb = "dumb" == System.getenv("TERM") + private[this] def isDumbTerminal = isDumb || System.getProperty("jline.terminal", "") == "none" private[this] val hasConsole = Option(java.lang.System.console).isDefined private[this] def useColorDefault: Boolean = { // This approximates that both stdin and stdio are connected, @@ -336,7 +338,7 @@ object Terminal { // In ci environments, don't touch the io streams unless run with -Dsbt.io.virtual=true if (System.getProperty("sbt.io.virtual", "") == "true" || !isCI) { hasProgress.set(isServer && isAnsiSupported) - consoleTerminalHolder.set(newConsoleTerminal()) + if (hasConsole && !isDumbTerminal) consoleTerminalHolder.set(newConsoleTerminal()) activeTerminal.set(consoleTerminalHolder.get) try withOut(withIn(f)) finally { @@ -744,7 +746,7 @@ object Terminal { private[sbt] def reset(): Unit = { jline.TerminalFactory.reset() console.close() - consoleTerminalHolder.set(newConsoleTerminal()) + if (hasConsole && !isDumbTerminal) consoleTerminalHolder.set(newConsoleTerminal()) } // translate explicit class names to type in order to support @@ -760,7 +762,7 @@ object Terminal { case "jline.WindowsTerminal" => "windows" case "jline.AnsiWindowsTerminal" => "windows" case "jline.UnsupportedTerminal" => "none" - case null if isDumbTerminal => "none" + case null if isDumb => "none" case x => x } if (newValue != null) {