Skip to content

Commit 2bd8543

Browse files
committed
print details of compilation errors in extra scripts only in verbose mode
1 parent 758c39c commit 2bd8543

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

core/src/main/scala/replpp/scripting/NonForkingScriptRunner.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import replpp.{Config, allPredefFiles}
44

55
import java.nio.file.Files
66
import scala.util.{Failure, Success}
7+
import scala.util.control.NoStackTrace
78

89
/**
910
* Main entrypoint for ScriptingDriver, i.e. it takes commandline arguments and executes a script on the current JVM.
@@ -49,8 +50,7 @@ object NonForkingScriptRunner {
4950
case Success(_) => // no exception, i.e. all is good
5051
if (verboseEnabled) System.err.println(s"script finished successfully")
5152
case Failure(exception) =>
52-
System.err.println(s"error during script execution: ${exception.getMessage}")
53-
throw exception
53+
throw new RuntimeException(s"error during script execution: ${exception.getMessage}") with NoStackTrace
5454
}
5555
}
5656

core/src/main/scala/replpp/scripting/ScriptingDriver.scala

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import java.lang.reflect.Method
1010
import java.net.URLClassLoader
1111
import java.nio.file.{Files, Path, Paths}
1212
import scala.language.unsafeNulls
13-
import scala.util.control.NonFatal
1413
import scala.util.{Failure, Try}
1514

1615
/**
@@ -43,27 +42,34 @@ class ScriptingDriver(compilerArgs: Array[String],
4342
println(s"compiler arguments: ${compilerArgs.mkString(",")}")
4443
}
4544

46-
def compileAndRun(): Try[Unit] = {
45+
def compileAndRun(): Try[Object] = {
4746
assert(!executed, "scripting driver can only be used once, and this instance has already been used.")
4847
executed = true
4948
val inputFiles = (wrappedScript +: predefFiles).filter(Files.exists(_))
50-
try {
51-
new SimpleDriver(
52-
linesBeforeRunBeforeCode = wrappingResult.linesBeforeRunBeforeCode,
53-
linesBeforeScript = wrappingResult.linesBeforeScript
54-
).compile(compilerArgs, inputFiles, verbose) { (ctx, outDir) =>
55-
given Context = ctx
56-
tempFiles += outDir
49+
val driver = new SimpleDriver(
50+
linesBeforeRunBeforeCode = wrappingResult.linesBeforeRunBeforeCode,
51+
linesBeforeScript = wrappingResult.linesBeforeScript
52+
)
53+
val result = driver.compile(compilerArgs, inputFiles, verbose) { (ctx, outDir) =>
54+
given Context = ctx
55+
tempFiles += outDir
5756

58-
val inheritedClasspath = ctx.settings.classpath.value
59-
val classpathEntries = ClassPath.expandPath(inheritedClasspath, expandStar = true).map(Paths.get(_))
60-
val mainMethod = lookupMainMethod(outDir, classpathEntries)
61-
mainMethod.invoke(null, scriptArgs)
62-
}
63-
} catch {
64-
case NonFatal(e) => Failure(e)
65-
} finally {
66-
tempFiles.result().foreach(deleteRecursively)
57+
val inheritedClasspath = ctx.settings.classpath.value
58+
val classpathEntries = ClassPath.expandPath(inheritedClasspath, expandStar = true).map(Paths.get(_))
59+
val mainMethod = lookupMainMethod(outDir, classpathEntries)
60+
mainMethod.invoke(null, scriptArgs)
61+
}
62+
tempFiles.result().foreach(deleteRecursively)
63+
64+
result.recoverWith { case e =>
65+
val msgAddonMaybe = if (verbose) "" else "Re-run with `--verbose` for more details"
66+
Failure(CompilerError(
67+
s"""Error during compilation: ${e.getMessage}
68+
|Please check error output above!
69+
|For given input files: ${inputFiles.mkString(", ")}
70+
|$msgAddonMaybe
71+
|""".stripMargin
72+
))
6773
}
6874
}
6975

core/src/main/scala/replpp/util/SimpleDriver.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package replpp.util
33
import dotty.tools.dotc.Driver
44
import dotty.tools.dotc.core.Contexts
55
import dotty.tools.dotc.core.Contexts.Context
6-
import dotty.tools.dotc.reporting.{ConsoleReporter, Diagnostic, Reporter}
6+
import dotty.tools.dotc.reporting.{Diagnostic, Reporter}
77
import dotty.tools.dotc.util.SourcePosition
88
import dotty.tools.io.{Directory, PlainDirectory}
99
import replpp.scripting.CompilerError
@@ -45,18 +45,19 @@ class SimpleDriver(linesBeforeRunBeforeCode: Int = 0, linesBeforeScript: Int = 0
4545
val outDir = Files.createTempDirectory("scala-repl-pp")
4646

4747
given ctx0: Context = {
48-
val ctx = rootCtx.fresh.setSetting(rootCtx.settings.outputDir, new PlainDirectory(Directory(outDir)))
48+
val ctx = rootCtx.fresh
49+
4950
if (linesBeforeRunBeforeCode != 0 || linesBeforeScript != 0) {
5051
ctx.setReporter(createReporter(linesBeforeRunBeforeCode, linesBeforeScript, rootCtx.reporter))
5152
}
5253

53-
if (verbose) {
54-
ctx.setSetting(rootCtx.settings.help, true)
55-
.setSetting(rootCtx.settings.XshowPhases, true)
56-
.setSetting(rootCtx.settings.Vhelp, true)
57-
.setSetting(rootCtx.settings.Vprofile, true)
58-
.setSetting(rootCtx.settings.explain, true)
59-
} else ctx
54+
ctx.setSetting(rootCtx.settings.outputDir, new PlainDirectory(Directory(outDir)))
55+
.setSetting(rootCtx.settings.XnoEnrichErrorMessages, !verbose)
56+
.setSetting(rootCtx.settings.help, verbose)
57+
.setSetting(rootCtx.settings.XshowPhases, verbose)
58+
.setSetting(rootCtx.settings.Vhelp, verbose)
59+
.setSetting(rootCtx.settings.Vprofile, verbose)
60+
.setSetting(rootCtx.settings.explain, verbose)
6061
}
6162

6263
if (doCompile(newCompiler, toCompile).hasErrors) {

core/src/test/scala/replpp/scripting/ScriptRunnerTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class ScriptRunnerTests extends AnyWordSpec with Matchers {
247247
// TODO: this isn't the case yet: note: if we intercepted the stdout/stderr, we could/should observe that the error is reported in line 1
248248
}
249249

250-
"error in imported file" in {
250+
"error is in imported file" in {
251251
ensureErrors { () =>
252252
val additionalScript = os.temp()
253253
os.write.over(additionalScript,

0 commit comments

Comments
 (0)