Skip to content

Commit 479fb8c

Browse files
committed
add exit code tests, fix exit code for tastyprinter flag, improve temp file creation for expression for better error messages
1 parent 01c1855 commit 479fb8c

File tree

11 files changed

+183
-18
lines changed

11 files changed

+183
-18
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,22 @@ object TastyPrinter:
3737
for arg <- args do
3838
if arg == "-color:never" then () // skip
3939
else if arg.startsWith("-") then println(s"bad option '$arg' was ignored")
40-
else if arg.endsWith(".tasty") then {
40+
else if arg.endsWith(".tasty") then
4141
val path = Paths.get(arg)
42-
if Files.exists(path) then printTasty(arg, Files.readAllBytes(path).nn)
43-
else println("File not found: " + arg)
44-
}
45-
else if arg.endsWith(".jar") then {
42+
if Files.exists(path) then
43+
printTasty(arg, Files.readAllBytes(path).nn)
44+
else
45+
println("File not found: " + arg)
46+
System.exit(1)
47+
else if arg.endsWith(".jar") then
4648
val jar = JarArchive.open(Path(arg), create = false)
4749
try
4850
for file <- jar.iterator() if file.name.endsWith(".tasty") do
4951
printTasty(s"$arg ${file.path}", file.toByteArray)
5052
finally jar.close()
51-
52-
}
53-
else println(s"Not a '.tasty' or '.jar' file: $arg")
53+
else
54+
println(s"Not a '.tasty' or '.jar' file: $arg")
55+
System.exit(1)
5456

5557
if printLastLine then
5658
println(line)

compiler/src/dotty/tools/io/Path.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Path private[io] (val jpath: JPath) {
170170
// returns the filename without the extension.
171171
def stripExtension: String = name stripSuffix ("." + extension)
172172
// returns the Path with the extension.
173-
def addExtension(ext: String): Path = new Path(jpath.resolveSibling(name + ext))
173+
def addExtension(ext: String): Path = new Path(jpath.resolveSibling(name + "." + ext))
174174
// changes the existing extension out for a new one, or adds it
175175
// if the current path has none.
176176
def changeExtension(ext: String): Path =

compiler/src/dotty/tools/scripting/Main.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ object Main:
4343
// write a standalone jar to the script parent directory
4444
writeJarfile(outDir, scriptFile, scriptArgs, classpathEntries, mainClass)
4545
invokeFlag
46-
} match
47-
case Some(ex) =>
48-
println(ex.getMessage)
49-
sys.exit(1)
50-
case _ =>
46+
}.map {
47+
case ScriptingException(msg) => println(msg)
48+
case ex => ex.printStackTrace
49+
}.foreach(_ => System.exit(1))
5150

5251
private def writeJarfile(outDir: Path, scriptFile: File, scriptArgs:Array[String],
5352
classpathEntries:Seq[Path], mainClassName: String): Unit =

compiler/src/dotty/tools/scripting/StringDriver.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dotty.tools.dotc.Driver
88
import dotty.tools.dotc.core.Contexts, Contexts.{ Context, ctx }
99
import dotty.tools.io.{ PlainDirectory, Directory, ClassPath }
1010
import Util.*
11+
import dotty.tools.dotc.util.SourceFile
1112

1213
class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Driver:
1314
override def sourcesRequired: Boolean = false
@@ -18,11 +19,12 @@ class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Dri
1819

1920
setup(compilerArgs, initCtx.fresh) match
2021
case Some((toCompile, rootCtx)) =>
21-
given Context = rootCtx.fresh.setSetting(rootCtx.settings.outputDir,
22-
new PlainDirectory(Directory(outDir)))
22+
given Context = rootCtx.fresh.setSetting(rootCtx.settings.outputDir, new PlainDirectory(Directory(outDir)))
2323

2424
val compiler = newCompiler
25-
compiler.newRun.compileFromStrings(List(scalaSource))
25+
26+
val source = SourceFile.virtual("expression", scalaSource)
27+
compiler.newRun.compileSources(List(source))
2628

2729
val output = ctx.settings.outputDir.value
2830
if ctx.reporter.hasErrors then
@@ -39,7 +41,7 @@ class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Dri
3941
case Left(ex) => Some(ex)
4042
catch
4143
case e: java.lang.reflect.InvocationTargetException =>
42-
throw e.getCause
44+
Some(e.getCause)
4345
finally
4446
deleteFile(outDir.toFile)
4547
case None => None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def compileError = prin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def positiveTest = println("Hello World!")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def runtimeError = throw RuntimeException()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def main = prin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def scriptPositive = println("Hello world!")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def scriptRuntimeError = throw RuntimeException()

0 commit comments

Comments
 (0)