Skip to content

Commit 6d06661

Browse files
committed
fix exit codes
1 parent 2c87994 commit 6d06661

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,40 @@ object MainGenericRunner {
118118
@sharable val scalaOption = raw"""@.*""".r
119119
@sharable val colorOption = raw"""-color:.*""".r
120120
@tailrec
121-
def process(args: List[String], settings: Settings): Settings = args match
121+
def processArgs(args: List[String], settings: Settings): Settings = args match
122122
case Nil =>
123123
settings
124124
case "-run" :: fqName :: tail =>
125-
process(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetToRun(fqName))
125+
processArgs(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetToRun(fqName))
126126
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
127127
val (tailargs, newEntries) = processClasspath(cp, tail)
128-
process(tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
128+
processArgs(tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
129129
case ("-version" | "--version") :: _ =>
130130
settings.copy(
131131
executeMode = ExecuteMode.Repl,
132132
residualArgs = List("-version")
133133
)
134134
case ("-v" | "-verbose" | "--verbose") :: tail =>
135-
process(
135+
processArgs(
136136
tail,
137137
settings.copy(
138138
verbose = true,
139139
residualArgs = settings.residualArgs :+ "-verbose"
140140
)
141141
)
142142
case "-save" :: tail =>
143-
process(tail, settings.withSave)
143+
processArgs(tail, settings.withSave)
144144
case "-nosave" :: tail =>
145-
process(tail, settings.noSave)
145+
processArgs(tail, settings.noSave)
146146
case "-with-compiler" :: tail =>
147-
process(tail, settings.withCompiler)
147+
processArgs(tail, settings.withCompiler)
148148
case (o @ javaOption(striped)) :: tail =>
149-
process(tail, settings.withJavaArgs(striped).withScalaArgs(o))
149+
processArgs(tail, settings.withJavaArgs(striped).withScalaArgs(o))
150150
case (o @ scalaOption(_*)) :: tail =>
151151
val remainingArgs = (CommandLineParser.expandArg(o) ++ tail).toList
152-
process(remainingArgs, settings)
152+
processArgs(remainingArgs, settings)
153153
case (o @ colorOption(_*)) :: tail =>
154-
process(tail, settings.withScalaArgs(o))
154+
processArgs(tail, settings.withScalaArgs(o))
155155
case "-e" :: expression :: tail =>
156156
val mainSource = s"@main def main(args: String *): Unit =\n ${expression}"
157157
settings
@@ -169,13 +169,13 @@ object MainGenericRunner {
169169
.withScriptArgs(tail*)
170170
else
171171
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
172-
process(tail, newSettings.withResidualArgs(arg))
173-
end process
172+
processArgs(tail, newSettings.withResidualArgs(arg))
173+
end processArgs
174174

175-
def main(args: Array[String]): Unit =
175+
def process(args: Array[String]): Boolean =
176176
val scalaOpts = envOrNone("SCALA_OPTS").toArray.flatMap(_.split(" ")).filter(_.nonEmpty)
177177
val allArgs = scalaOpts ++ args
178-
val settings = process(allArgs.toList, Settings())
178+
val settings = processArgs(allArgs.toList, Settings())
179179
if settings.exitCode != 0 then System.exit(settings.exitCode)
180180

181181
def removeCompiler(cp: Array[String]) =
@@ -185,12 +185,13 @@ object MainGenericRunner {
185185
else
186186
cp
187187

188-
def run(settings: Settings): Unit = settings.executeMode match
188+
def run(settings: Settings): Option[Throwable] = settings.executeMode match
189189
case ExecuteMode.Repl =>
190190
val properArgs =
191191
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
192192
++ settings.residualArgs
193193
repl.Main.main(properArgs.toArray)
194+
None
194195

195196
case ExecuteMode.PossibleRun =>
196197
val newClasspath = (settings.classPath :+ ".").flatMap(_.split(classpathSeparator).filter(_.nonEmpty)).map(File(_).toURI.toURL)
@@ -207,10 +208,11 @@ object MainGenericRunner {
207208
case None =>
208209
settings.withExecuteMode(ExecuteMode.Repl)
209210
run(newSettings)
211+
210212
case ExecuteMode.Run =>
211213
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
212214
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
213-
val res = ObjectRunner.runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
215+
ObjectRunner.runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
214216
case ex: ClassNotFoundException if ex.getMessage == settings.targetToRun =>
215217
val file = settings.targetToRun
216218
Jar(file).mainClass match
@@ -220,7 +222,7 @@ object MainGenericRunner {
220222
Some(IllegalArgumentException(s"No main class defined in manifest in jar: $file"))
221223
case ex => Some(ex)
222224
}
223-
errorFn("", res)
225+
224226
case ExecuteMode.Script =>
225227
val targetScript = Paths.get(settings.targetScript).toFile
226228
val targetJar = settings.targetScript.replaceAll("[.][^\\/]*$", "")+".jar"
@@ -232,11 +234,10 @@ object MainGenericRunner {
232234
sys.props("script.path") = targetScript.toPath.toAbsolutePath.normalize.toString
233235
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
234236
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
235-
val res = if mainClass.nonEmpty then
237+
if mainClass.nonEmpty then
236238
ObjectRunner.runAndCatch(newClasspath :+ File(targetJar).toURI.toURL, mainClass, settings.scriptArgs)
237239
else
238240
Some(IllegalArgumentException(s"No main class defined in manifest in jar: $precompiledJar"))
239-
errorFn("", res)
240241

241242
else
242243
val properArgs =
@@ -247,6 +248,8 @@ object MainGenericRunner {
247248
++ List("-script", settings.targetScript)
248249
++ settings.scriptArgs
249250
scripting.Main.main(properArgs.toArray)
251+
None
252+
250253
case ExecuteMode.Expression =>
251254
val cp = settings.classPath match {
252255
case Nil => ""
@@ -265,12 +268,16 @@ object MainGenericRunner {
265268
else
266269
run(settings.withExecuteMode(ExecuteMode.Repl))
267270

268-
run(settings)
271+
run(settings) match
272+
case e @ Some(ex) => errorFn("", e)
273+
case _ => true
269274

270-
271-
def errorFn(str: String, e: Option[Throwable] = None, isFailure: Boolean = true): Boolean = {
275+
def errorFn(str: String, e: Option[Throwable] = None): Boolean =
272276
if (str.nonEmpty) Console.err.println(str)
273277
e.foreach(_.printStackTrace())
274-
!isFailure
275-
}
278+
false
279+
280+
def main(args: Array[String]): Unit =
281+
if (!process(args)) System.exit(1)
282+
276283
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Util.*
1212
class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Driver:
1313
override def sourcesRequired: Boolean = false
1414

15-
def compileAndRun(classpath: List[String] = Nil): Unit =
15+
def compileAndRun(classpath: List[String] = Nil): Option[Throwable] =
1616
val outDir = Files.createTempDirectory("scala3-expression")
1717
outDir.toFile.deleteOnExit()
1818

@@ -26,20 +26,21 @@ class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Dri
2626

2727
val output = ctx.settings.outputDir.value
2828
if ctx.reporter.hasErrors then
29-
throw StringDriverException("Errors encountered during compilation")
29+
Some(StringDriverException("Errors encountered during compilation"))
3030

3131
try
3232
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}"
3333
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) }
3434
sys.props("java.class.path") = classpathEntries.map(_.toString).mkString(pathsep)
3535
val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scalaSource)
3636
mainMethod.invoke(null, Array.empty[String])
37+
None
3738
catch
3839
case e: java.lang.reflect.InvocationTargetException =>
3940
throw e.getCause
4041
finally
4142
deleteFile(outDir.toFile)
42-
case None =>
43+
case None => None
4344
end compileAndRun
4445

4546
end StringDriver

0 commit comments

Comments
 (0)