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

scala-cli -e 'println("hello")' > /dev/null throws java.io.IOException #3481

Open
philwalk opened this issue Feb 6, 2025 · 5 comments
Open
Labels
bug Something isn't working windows Windows-specific issues

Comments

@philwalk
Copy link
Contributor

philwalk commented Feb 6, 2025

Version(s)

# scala-cli --version
Scala CLI version: 1.6.1
Scala version (default): 3.6.3

Describe the bug
Redirecting stdout to /dev/null throws an Exception.

java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6
  coursier.jniutils.WindowsAnsiTerminal.enableAnsiOutput(WindowsAnsiTerminal.java:25)
  scala.cli.ScalaCli$.main0(ScalaCli.scala:317)
  scala.cli.ScalaCli$.main(ScalaCli.scala:124)
  scala.cli.ScalaCli.main(ScalaCli.scala)

To Reproduce
hello.sc:

#!/usr/bin/env -S scala-cli shebang
printf("%s\n", "hello")
./hello.sc
hello
scala-cli -e 'println("hello")'  > /dev/null
Error: java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6
For more details, please see 'C:\opt\ue\.scala-build\stacktraces\1738864818-3992728134502856876.log'

Should not throw an exception.

@philwalk philwalk added the bug Something isn't working label Feb 6, 2025
@philwalk philwalk changed the title scala-cli -e 'println("hello")' > /dev/null throws java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6 scala-cli -e 'println("hello")' > /dev/null throws java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6 Feb 6, 2025
@philwalk
Copy link
Contributor Author

philwalk commented Feb 6, 2025

This may be related. When piping the output rather than redirecting, bloop crashes.
The command line was a typo, but perhaps the output is useful, or maybe it's unrelated.

hello.sc | /dev/null
Compiling project (Scala 3.6.3, JVM (17))
Compiled project (Scala 3.6.3, JVM (17))
[E] Exiting BSP server with Connection reset
Bloop 'bsp' command exited with code 1. Something may be wrong with the current configuration.
Running the clean sub-command to clear the working directory and remove caches might help.
If the error persists, please report the issue as a bug and attach a log with increased verbosity by passing -v -v -v.

@philwalk philwalk changed the title scala-cli -e 'println("hello")' > /dev/null throws java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6 scala-cli -e 'println("hello")' > /dev/null throws java.io.IOException Feb 6, 2025
@Gedochao
Copy link
Contributor

Gedochao commented Feb 10, 2025

@philwalk am I guessing correctly the reproduction is Windows-specific? I can't seem to be able to reproduce on a Mac.
Also, cc @tgodzik for the weird Bloop failure 🤔

@Gedochao Gedochao added windows Windows-specific issues needs reproduction and removed needs reproduction labels Feb 10, 2025
@philwalk
Copy link
Contributor Author

philwalk commented Feb 11, 2025

@philwalk am I guessing correctly the reproduction is Windows-specific? I can't seem to be able to reproduce on a Mac. Also, cc @tgodzik for the weird Bloop failure 🤔

I assume that @tgodzik is notified when he is mentioned like this.
EDITED:

I thought so [that it's Windows-specific] because I didn't see the problem when I did a quick check in a WSL bash session.

This is apparently not a Windows-only problem, although it doesn't manifest in exactly the same way. In Windows Terminal I get this:

# clisrc/hello.sc > /dev/null
Error: java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6
For more details, please see 'C:\opt\ue\.scala-build\stacktraces\1739285187-7626986499591343828.log'

# cat 'C:\opt\ue\.scala-build\stacktraces\1739285187-7626986499591343828.log'
java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6
  coursier.jniutils.WindowsAnsiTerminal.enableAnsiOutput(WindowsAnsiTerminal.java:25)
  scala.cli.ScalaCli$.main0(ScalaCli.scala:317)
  scala.cli.ScalaCli$.main(ScalaCli.scala:124)
  scala.cli.ScalaCli.main(ScalaCli.scala)

See below for the WSL bash session crash and stacktrace.

@philwalk
Copy link
Contributor Author

philwalk commented Feb 11, 2025

Turns out that it also crashes in a Linux bash session, and even with a valid command line:

:/opt/ue$ clisrc/hello.sc > /dev/null
[E] Caught bloop.data.Project$ProjectReadException: Failed to load project from /opt/ue/clisrc/.scala-build/.bloop/clisrc_eac7d69449-afeaabc44b.json
Bloop 'bsp' command exited with code 1. Something may be wrong with the current configuration.
Running the clean sub-command to clear the working directory and remove caches might help.
If the error persists, please report the issue as a bug and attach a log with increased verbosity by passing -v -v -v.
Error: java.lang.RuntimeException: Bloop BSP connection in /home/philwalk/.cache/scalacli/bsp-sockets/proc-340 was unexpectedly closed or bloop didn't start.
For more details, please see '/opt/ue/clisrc/.scala-build/stacktraces/1739284966-4775558018048031860.log'
(base) philwalk@d5:/opt/ue$

Here's the stacktrace:

java.lang.RuntimeException: Bloop BSP connection in /home/philwalk/.cache/scalacli/bsp-sockets/proc-340 was unexpectedly closed or bloop didn't start.
  scala.sys.package$.error(package.scala:27)
  bloop.rifle.internal.Operations$$anon$1.openSocket(Operations.scala:426)
  bloop.rifle.BloopServer$.create$1(BloopServer.scala:128)
  bloop.rifle.BloopServer$.connect(BloopServer.scala:144)
  bloop.rifle.BloopServer$.bsp(BloopServer.scala:167)
  bloop.rifle.BloopServer$.buildServer(BloopServer.scala:186)
  scala.build.compiler.BloopCompilerMaker.$anonfun$1(BloopCompilerMaker.scala:48)
  scala.build.compiler.BloopCompiler.<init>(BloopCompiler.scala:15)
  scala.build.compiler.BloopCompilerMaker.$anonfun$2(BloopCompilerMaker.scala:51)
  scala.util.Try$.apply(Try.scala:217)
  scala.build.compiler.BloopCompilerMaker.create(BloopCompilerMaker.scala:51)
  scala.build.compiler.ScalaCompilerMaker.withCompiler(ScalaCompilerMaker.scala:34)
  scala.build.compiler.ScalaCompilerMaker.withCompiler$(ScalaCompilerMaker.scala:9)
  scala.build.compiler.BloopCompilerMaker.withCompiler(BloopCompilerMaker.scala:14)
  scala.build.Build$.build$$anonfun$3(Build.scala:611)
  scala.build.EitherCps$Helper.apply(EitherCps.scala:19)
  scala.build.Build$.build(Build.scala:596)
  scala.cli.commands.run.Run$.runCommand(Run.scala:334)
  scala.cli.commands.shebang.Shebang$.runCommand(Shebang.scala:33)
  scala.cli.commands.shebang.Shebang$.runCommand(Shebang.scala:26)
  scala.cli.commands.ScalaCommand.run(ScalaCommand.scala:401)
  scala.cli.commands.ScalaCommand.run(ScalaCommand.scala:382)
  caseapp.core.app.CaseApp.main(CaseApp.scala:166)
  scala.cli.commands.ScalaCommand.main(ScalaCommand.scala:367)
  caseapp.core.app.CommandsEntryPoint.main(CommandsEntryPoint.scala:370)
  scala.cli.ScalaCliCommands.main(ScalaCliCommands.scala:125)
  scala.cli.ScalaCli$.main0(ScalaCli.scala:320)
  scala.cli.ScalaCli$.main(ScalaCli.scala:124)
  scala.cli.ScalaCli.main(ScalaCli.scala)

@philwalk
Copy link
Contributor Author

philwalk commented Feb 11, 2025

@tgodzik

More info on Windows/Linux versions of the problem:
This command line fails on Windows in a MSYS64 bash session:

scala -e "println("""hello""")" > /dev/null

And also in a CMD.EXE session, whether CMD.EXE alone, or running in Windows Terminal:

It's not problem when STDOUT is not redirected, or is redirected to a file

C:\Windows\System32>scala-cli.exe -e "println("""hello""")"
Compiling project (Scala 3.6.3, JVM (11))
Compiled project (Scala 3.6.3, JVM (11))
hello

C:\Windows\System32>scala-cli.exe -e "println("""hello""")" > s0
Compiling project (Scala 3.6.3, JVM (11))
Compiled project (Scala 3.6.3, JVM (11))
C:\Windows\System32>type s0
hello

The problem occurs when redirecting to NUL

C:\Windows\System32>scala-cli.exe -e "println("""hello""")" > NUL
Error: java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6
For more details, please see 'C:\Windows\System32\.scala-build\stacktraces\1739296010-9469441151690933143.log'

C:\Windows\System32>type C:\Windows\System32\.scala-build\stacktraces\1739296010-9469441151690933143.log
java.io.IOException: Error enabling ANSI output: GetConsoleMode error 6
  coursier.jniutils.WindowsAnsiTerminal.enableAnsiOutput(WindowsAnsiTerminal.java:25)
  scala.cli.ScalaCli$.main0(ScalaCli.scala:317)
  scala.cli.ScalaCli$.main(ScalaCli.scala:124)
  scala.cli.ScalaCli.main(ScalaCli.scala)

Unable to reproduce the problem in WSL Ubuntu with this particular command line:

(base) philwalk@d5:~$ scala -e "println(\"hello\")"
Compiling project (Scala 3.6.3, JVM (17))
Compiled project (Scala 3.6.3, JVM (17))
hello

However, in WSL there's a bloop crash when running the following short script, unrelated to redirection:

#!/usr/bin/env -S scala-cli shebang
//> using scala 3.4.3
printf("scriptPath: [%s]\n", scriptPath)
sys.props("java.class.path").split(psep).foreach { println }
def psep: String = sys.props("path.separator")
(base) philwalk@d5:/opt/ue$ clisrc/hello.sc
Starting compilation server
[E] Caught bloop.data.Project$ProjectReadException: Failed to load project from /opt/ue/clisrc/.scala-build/.bloop/clisrc_eac7d69449-afeaabc44b.json
Bloop 'bsp' command exited with code 1. Something may be wrong with the current configuration.
Running the clean sub-command to clear the working directory and remove caches might help.
If the error persists, please report the issue as a bug and attach a log with increased verbosity by passing -v -v -v.
Error: java.lang.RuntimeException: Bloop BSP connection in /home/philwalk/.cache/scalacli/bsp-sockets/proc-1864 was unexpectedly closed or bloop didn't start.
For more details, please see '/opt/ue/clisrc/.scala-build/stacktraces/1739296852-7851145895565982827.log'
(base) philwalk@d5:/opt/ue$
(base) philwalk@d5:/opt/ue$ cat  '/opt/ue/clisrc/.scala-build/stacktraces/1739296852-7851145895565982827.log'
java.lang.RuntimeException: Bloop BSP connection in /home/philwalk/.cache/scalacli/bsp-sockets/proc-1864 was unexpectedly closed or bloop didn't start.
  scala.sys.package$.error(package.scala:27)
  bloop.rifle.internal.Operations$$anon$1.openSocket(Operations.scala:426)
  bloop.rifle.BloopServer$.create$1(BloopServer.scala:128)
  bloop.rifle.BloopServer$.connect(BloopServer.scala:144)
  bloop.rifle.BloopServer$.bsp(BloopServer.scala:167)
  bloop.rifle.BloopServer$.buildServer(BloopServer.scala:186)
  scala.build.compiler.BloopCompilerMaker.$anonfun$1(BloopCompilerMaker.scala:48)
  scala.build.compiler.BloopCompiler.<init>(BloopCompiler.scala:15)
  scala.build.compiler.BloopCompilerMaker.$anonfun$2(BloopCompilerMaker.scala:51)
  scala.util.Try$.apply(Try.scala:217)
  scala.build.compiler.BloopCompilerMaker.create(BloopCompilerMaker.scala:51)
  scala.build.compiler.ScalaCompilerMaker.withCompiler(ScalaCompilerMaker.scala:34)
  scala.build.compiler.ScalaCompilerMaker.withCompiler$(ScalaCompilerMaker.scala:9)
  scala.build.compiler.BloopCompilerMaker.withCompiler(BloopCompilerMaker.scala:14)
  scala.build.Build$.build$$anonfun$3(Build.scala:611)
  scala.build.EitherCps$Helper.apply(EitherCps.scala:19)
  scala.build.Build$.build(Build.scala:596)
  scala.cli.commands.run.Run$.runCommand(Run.scala:334)
  scala.cli.commands.shebang.Shebang$.runCommand(Shebang.scala:33)
  scala.cli.commands.shebang.Shebang$.runCommand(Shebang.scala:26)
  scala.cli.commands.ScalaCommand.run(ScalaCommand.scala:401)
  scala.cli.commands.ScalaCommand.run(ScalaCommand.scala:382)
  caseapp.core.app.CaseApp.main(CaseApp.scala:166)
  scala.cli.commands.ScalaCommand.main(ScalaCommand.scala:367)
  caseapp.core.app.CommandsEntryPoint.main(CommandsEntryPoint.scala:370)
  scala.cli.ScalaCliCommands.main(ScalaCliCommands.scala:125)
  scala.cli.ScalaCli$.main0(ScalaCli.scala:320)
  scala.cli.ScalaCli$.main(ScalaCli.scala:124)
  scala.cli.ScalaCli.main(ScalaCli.scala)

This one clears up after doing scala-cli clean clisrc:

(base) philwalk@d5:/opt/ue$ scala-cli clean clisrc
(base) philwalk@d5:/opt/ue$ clisrc/hello.sc
Compiling project (Scala 3.4.3, JVM (17))
Compiled project (Scala 3.4.3, JVM (17))
scriptPath: [clisrc/hello.sc]
/opt/ue/clisrc/.scala-build/clisrc_8b89045a54-a30d5cd5b2/classes/main
/home/philwalk/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.4.3/scala3-library_3-3.4.3.jar
/home/philwalk/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.12/scala-library-2.13.12.jar
(base) philwalk@d5:/opt/ue$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working windows Windows-specific issues
Projects
None yet
Development

No branches or pull requests

2 participants