Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jline3 console detection is fragile in Windows cmd.exe, cygwin, msys, and mingw environments. #12405

Closed
philwalk opened this issue May 10, 2021 · 2 comments

Comments

@philwalk
Copy link
Contributor

philwalk commented May 10, 2021

Compiler version

sbt:scala3> print scalaVersion
scala3-sbt-bridge / scalaVersion
3.0.0-RC3
scala3-library / scalaVersion
3.0.0-RC3
scala3-compiler / scalaVersion
3.0.0-RC3
scala3-interfaces / scalaVersion
3.0.0-RC3
tasty-core / scalaVersion
3.0.0-RC3
scalaVersion
3.0.0-RC3
sbt:scala3>

Minimized code

#!/opt/scala3/bin/scala
import org.jline.utils.OSUtils._
def main(args: Array[String]) =
  printf("IS_WINDOWS: %s\n", IS_WINDOWS)
  printf("IS_CYGWIN:  %s\n", IS_CYGWIN)
  printf("IS_MINGW:   %s\n", IS_MINGW)
  printf("IS_MSYSTEM: %s\n", IS_MSYSTEM)
  printf("IS_CONEMU:  %s\n", IS_CONEMU)
  printf("IS_OSX:     %s\n", IS_OSX)
  printf("TTY_COMMAND:     %s\n", TTY_COMMAND)
  printf("STTY_COMMAND:    %s\n", STTY_COMMAND)
  printf("STTY_F_OPTION:   %s\n", STTY_F_OPTION)
  printf("INFOCMP_COMMAND: %s\n", INFOCMP_COMMAND)

Output

The output depends on environment variables, but in Windows, the jline3 code for identifying the current console environment is fragile. When launching the scala REPL, if jline3 misidentifies the environment, the result is not good, so it might make sense to take defensive measures to protect the REPL experience from random accidents.

Some examples:
with console CMD.EXE, and PWD=, jline3 mis-identifies Cygwin.

C:\opt\ue>.\scala3.bat /opt/ue/ssrc/showJline3view.sc
-- Deprecation Warning: C:\opt\ue\ssrc\showJline3view.sc:8:28 ------------------
8 |  printf("IS_MINGW:   %s\n",IS_MINGW)
  |                            ^^^^^^^^
  |value IS_MINGW in object OSUtils is deprecated since : see corresponding Javadoc for more information.
1 warning found
IS_WINDOWS: true
IS_CYGWIN:  true
IS_MINGW:   false
IS_MSYSTEM: false
IS_CONEMU:  false
IS_OSX:     false
TTY_COMMAND:     tty.exe
STTY_COMMAND:    stty.exe
STTY_F_OPTION:   null
INFOCMP_COMMAND: infocmp.exe

A REPL session when PWD is defined and starts with a slash:

set PWD=/
.\scala3.bat
C:\opt\ue>.\scala3.bat
[90m~←[0m[K                                                                                                                                                                                        [?2004h[34mscala> [0m

The session looks bad, and the arrow keys are not functional.
A workaround is to unset PWD immediately prior to starting the jvm.

FYI, here is my scala3.bat file:

@echo off
set SCALA_HOME="c:/opt/scala3"
set CP="%SCALA_HOME%/lib/scala-library-2.13.5.jar;%SCALA_HOME%/lib/scala3-library_3-3.0.1-RC1-bin-SNAPSHOT.jar;%SCALA_HOME%/lib/scala-asm-9.1.0-scala-1.jar;%SCALA_HOME%/lib/compiler-interface-1.3.5.jar;%SCALA_HOME%/lib/scala3-interfaces-3.0.1-RC1-bin-SNAPSHOT.jar;%SCALA_HOME%/lib/scala3-compiler_3-3.0.1-RC1-bin-SNAPSHOT.jar;%SCALA_HOME%/lib/tasty-core_3-3.0.1-RC1-bin-SNAPSHOT.jar;%SCALA_HOME%/lib/scala3-staging_3-3.0.1-RC1-bin-SNAPSHOT.jar;%SCALA_HOME%/lib/scala3-tasty-inspector_3-3.0.1-RC1-bin-SNAPSHOT.jar;%SCALA_HOME%/lib/jline-reader-3.19.0.jar;%SCALA_HOME%/lib/jline-terminal-3.19.0.jar;%SCALA_HOME%/lib/jline-terminal-jna-3.19.0.jar;%SCALA_HOME%/lib/jna-5.3.1.jar"
if [%1]==[] goto repl
C:/opt/jdk/bin/java.exe -Xmx768m -Xms768m -classpath %CP% -Dscala.usejavacp=true dotty.tools.scripting.Main -color:never -deprecation -script %*
goto :eof
:repl
C:/opt/jdk/bin/java.exe -Xmx768m -Xms768m -classpath %CP% -Dscala.usejavacp=true dotty.tools.repl.Main %*

I stumbled onto this problem accidentally, and PWD would not typically be set in a CMD.EXE session.

A bigger issue is that I have the same bad REPL experience in a CYGWIN bash session with console mintty. I have to fake a non-cygwin environment to have a good and functional REPL experience. Perhaps something is unusual about my system, and I do have somewhat similar problems with sbt as well (I have to fake a non-cygwin environment).

I'm testing other environments, see detailed summary below.

Expectation

Scala REPL sessions should not be sensitive to fragile console detection. I'll post updates as I learn more about the problem.

@smarter
Copy link
Member

smarter commented May 10, 2021

Could you report this against jline directly? https://github.com/jline/jline3/issues, it would be nicer if this could be fixed upstream so we don't have to add our own workarounds.

@philwalk
Copy link
Contributor Author

philwalk commented May 10, 2021

Could you report this against jline directly? https://github.com/jline/jline3/issues, it would be nicer if this could be fixed upstream so we don't have to add our own workarounds.

I am reporting this to jline, although I think there might also be problems in how scala initializes it, or in how we setup our REPL session. Even when jline3 correctly identifies a cygwin session, things are broken.

I any event, I'd like to submit a PR for a workaround to allow a functioning cygwin experience.

The jline error report: jline/jline3#669

WojciechMazur added a commit that referenced this issue Dec 19, 2024
…27.1 (was 3.27.0) (#22205)

as per the https://github.com/jline/jline3 readme
 
and as per discussion and linked items on #22201 

fixes #22201

note that as far as I can tell, the stuff I removed from
libexec/common-shared is dead code

@philwalk dunno if you're still around but judging from #12405 you might
be a good reviewer here

note that I believe we _don't_ need to also port
scala/scala#10889 here, since we are already
using separate JLine JARs rather than the all-in-one JAR

I've chosen not to upgrade all the way to JLine 3.28.0 at the moment, as
it is quite new (2 days ago) and doesn't appear to have any fixes that
might be critical.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants