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.
This commit is contained in:
Ethan Atkins 2020-11-03 08:42:04 -08:00
parent 7e1384608e
commit afbceb062f
1 changed files with 6 additions and 4 deletions

View File

@ -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) {