Skip to content

scala must not exit with 0 if the program threw an uncaught exception #15022

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

Closed
sevaseva opened this issue Apr 25, 2022 · 2 comments · Fixed by #15604
Closed

scala must not exit with 0 if the program threw an uncaught exception #15022

sevaseva opened this issue Apr 25, 2022 · 2 comments · Fixed by #15604

Comments

@sevaseva
Copy link

Compiler version

3.1.0, 3.1.1, 3.1.2 as well as the tip of the main branch at the time of writing this.

On the other hand, this isn't affecting scala 2.13.4 (verified) and many/all other 2.x versions.

Minimized code

object Main {
    def main(args: Array[String]) = {
    	throw new RuntimeException()
   }
}

More concretely, in bash shell after building scala into scala3-3.2.0-RC1-bin-SNAPSHOT (with bin and lib in there):

scala3-3.2.0-RC1-bin-SNAPSHOT$ export SCALA_HOME=$(pwd)

scala3-3.2.0-RC1-bin-SNAPSHOT$ cat > a.scala << EOF
object Main {
    def main(args: Array[String]) = {
    	throw new RuntimeException()
   }
}
EOF

scala3-3.2.0-RC1-bin-SNAPSHOT$ cat a.scala
object Main {
    def main(args: Array[String]) = {
    	throw new RuntimeException()
   }
}

scala3-3.2.0-RC1-bin-SNAPSHOT$ bin/scalac a.scala

scala3-3.2.0-RC1-bin-SNAPSHOT$ bin/scala Main
java.lang.RuntimeException
	at Main$.main(a.scala:3)
	at Main.main(a.scala)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at dotty.tools.runner.RichClassLoader$.run$extension$$anonfun$1(ScalaClassLoader.scala:36)
	at dotty.tools.runner.ScalaClassLoader$.asContext(ScalaClassLoader.scala:80)
	at dotty.tools.runner.RichClassLoader$.dotty$tools$runner$RichClassLoader$$$asContext$extension(ScalaClassLoader.scala:18)
	at dotty.tools.runner.RichClassLoader$.run$extension(ScalaClassLoader.scala:36)
	at dotty.tools.runner.CommonRunner.run(ObjectRunner.scala:23)
	at dotty.tools.runner.CommonRunner.run$(ObjectRunner.scala:13)
	at dotty.tools.runner.ObjectRunner$.run(ObjectRunner.scala:48)
	at dotty.tools.runner.CommonRunner.runAndCatch(ObjectRunner.scala:30)
	at dotty.tools.runner.CommonRunner.runAndCatch$(ObjectRunner.scala:13)
	at dotty.tools.runner.ObjectRunner$.runAndCatch(ObjectRunner.scala:48)
	at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:212)
	at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:267)
	at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)

scala3-3.2.0-RC1-bin-SNAPSHOT$ echo $?
0

Output

That last output of 0 is the issue.

Expectation

The last command echo $? must print 1 or some other non-zero.
scala 2.13.4 does that.

@sevaseva sevaseva added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 25, 2022
@sevaseva
Copy link
Author

sevaseva commented Apr 25, 2022

After applying the one-line change https://github.com/lampepfl/dotty/compare/main...sevaseva:uncaught-exception-exit-1?expand=1 , scala does exit with 1 (and echo $? prints 1) as expected for the particular program mentioned in the issue. That's just a demo to help demonstrate the issue.

@som-snytt
Copy link
Contributor

It would be a small nice thing to have a dedicated exception maybe in scala.runtime for now.

@Kordyjan Kordyjan added compat:scala2 area:tooling and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 25, 2022
@rochala rochala self-assigned this May 5, 2022
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants