Skip to content

Commit 8082d38

Browse files
committed
Merge pull request scala#3946 from gourlaysama/wip/t5254
SI-5254 running an empty scala script should succeed
2 parents 0729b29 + 9519eb0 commit 8082d38

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

src/compiler/scala/tools/nsc/ScriptRunner.scala

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package scala
77
package tools.nsc
88

9-
import io.{ Directory, File, Path }
9+
import io.{ AbstractFile, Directory, File, Path }
1010
import java.io.IOException
1111
import scala.tools.nsc.reporters.{Reporter,ConsoleReporter}
1212
import util.Exceptional.unwrap
@@ -111,6 +111,12 @@ class ScriptRunner extends HasCompileSocket {
111111
else None
112112
}
113113

114+
def hasClassToRun(d: Directory): Boolean = {
115+
import util.ClassPath.{ DefaultJavaContext => ctx }
116+
val cp = ctx.newClassPath(AbstractFile.getDirectory(d))
117+
cp.findClass(mainClass).isDefined
118+
}
119+
114120
/* The script runner calls sys.exit to communicate a return value, but this must
115121
* not take place until there are no non-daemon threads running. Tickets #1955, #2006.
116122
*/
@@ -124,24 +130,30 @@ class ScriptRunner extends HasCompileSocket {
124130

125131
compile match {
126132
case Some(compiledPath) =>
127-
try io.Jar.create(jarFile, compiledPath, mainClass)
128-
catch { case _: Exception => jarFile.delete() }
129-
130-
if (jarOK) {
131-
compiledPath.deleteRecursively()
132-
handler(jarFile.toAbsolute.path)
133+
if (!hasClassToRun(compiledPath)) {
134+
// it compiled ok, but there is nothing to run;
135+
// running an empty script should succeed
136+
true
137+
} else {
138+
try io.Jar.create(jarFile, compiledPath, mainClass)
139+
catch { case _: Exception => jarFile.delete() }
140+
141+
if (jarOK) {
142+
compiledPath.deleteRecursively()
143+
handler(jarFile.toAbsolute.path)
144+
}
145+
// jar failed; run directly from the class files
146+
else handler(compiledPath.path)
133147
}
134-
// jar failed; run directly from the class files
135-
else handler(compiledPath.path)
136148
case _ => false
137149
}
138150
}
139151

140152
if (jarOK) handler(jarFile.toAbsolute.path) // pre-compiled jar is current
141153
else recompile() // jar old - recompile the script.
142154
}
143-
// don't use a cache jar at all--just use the class files
144-
else compile exists (cp => handler(cp.path))
155+
// don't use a cache jar at all--just use the class files, if they exist
156+
else compile exists (cp => !hasClassToRun(cp) || handler(cp.path))
145157
}
146158
}
147159

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package scala.tools.nsc
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
8+
@RunWith(classOf[JUnit4])
9+
class ScriptRunnerTest {
10+
@Test
11+
def testEmptyScriptSucceeds: Unit = {
12+
val s = new GenericRunnerSettings(s => ())
13+
s.nc.value = true
14+
s.usejavacp.value = true
15+
16+
// scala -nc -e ''
17+
assertTrue(ScriptRunner.runCommand(s, "", Nil))
18+
19+
// scala -nc -save -e ''
20+
s.save.value = true
21+
assertTrue(ScriptRunner.runCommand(s, "", Nil))
22+
}
23+
}

0 commit comments

Comments
 (0)