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 script runner crashes on a file with no statements #5254

Closed
scabug opened this issue Nov 30, 2011 · 3 comments
Closed

Scala script runner crashes on a file with no statements #5254

scabug opened this issue Nov 30, 2011 · 3 comments

Comments

@scabug
Copy link

scabug commented Nov 30, 2011

When scala script compiler/runner is invoked on an empty file,
or on a file with lines of code commented out, it fails to create the Main class (entry point into the script application),
but still decides to run the thing, so it promptly crashes.

{code:title=Script.scala|borderStyle=solid}
import sys.process._
import java.io.File

// "gzip" #< new File("Script.scala") #> new File("Script.scala.gz")!
// "zip" #< new File("Script.scala") #> new File("Script.zip")!
{code}

{panel}
C:>scala Script.scala
java.lang.ClassNotFoundException: Main (args = , classpath = ...)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:103)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)
at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
{panel}

This doesn't look like the proper way, the compiler should either

  • create an empty Main procedure and run it, providing a valid error level 0 output in case this script was chained somewhere
  • output something like "Nothing to compile", etc....
@scabug
Copy link
Author

scabug commented Nov 30, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5254?orig=1
Reporter: @melezov
Affected Versions: 2.9.1, 2.11.2

@scabug
Copy link
Author

scabug commented Jul 12, 2012

Stas Shevchenko (stasimus) said:
Actually good place to fix is ScriptRunner, while for compiler it is OK to compile nothing, ScriptRunner should detect no classes to run, place for fix is mentioned bellow. In the details - we can implement def hasScriptMain(compiledPath:Directory, scriptMain:String), that will return true if {mainClassName}.class is inside compiled tmp directory. May be there is a better way - for example ask the compiler is scriptMain(settings: Settings) was compiled into class file.

private def withCompiledScript(
settings: GenericRunnerSettings,
scriptFile: String)
(handler: String => Boolean): Boolean =
{
...

def compile: Option[Directory] = {

...        
    if (reporter.hasErrors) None 
    else if (!hasScriptMain(compiledPath, scriptMain(settings))) throw new IOException("Nothing to compile")
    Some(compiledPath)
  }
  ...
}

@scabug
Copy link
Author

scabug commented Aug 26, 2014

@gourlaysama said:
scala/scala#3946

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