Skip to content

Instantly share code, notes, and snippets.

@litan
Last active August 13, 2025 07:05
Show Gist options
  • Select an option

  • Save litan/c73faa88154ee842b680e9417b8e754b to your computer and use it in GitHub Desktop.

Select an option

Save litan/c73faa88154ee842b680e9417b8e754b to your computer and use it in GitHub Desktop.
Trigger Scala compiler issue
import scala.tools.nsc.{Global, Settings}
import scala.reflect.internal.util.BatchSourceFile
import scala.tools.nsc.io.VirtualDirectory
object CompilerExample {
val virtualDirectory = new VirtualDirectory("(memory)", None)
val settings = new Settings()
settings.usejavacp.value = true
settings.outputDirs.setSingleOutput(virtualDirectory)
val global = new Global(settings)
def main(args: Array[String]): Unit = {
val codeWithNoError =
"""
class Hello1 {
def hello(): Unit = {
println("Hi there!")
}
}
"""
val codeWithBenignError =
"""
class Hello2 {
def hello(): Unit = {
printlnx("Hi there!")
}
}
"""
val codeWithDeadlyError =
"""
class Hello3 {
def pic = 20
def makePic(s: Int = {
pic
}
}
"""
val code2WithNoError =
"""
class Hello4 {
def hello(): Unit = {
val x = Seq(1, 2, 3)
println(x)
}
}
"""
println("Doing first compilation")
// compileCode(codeWithNoError)
// compileCode(codeWithBenignError)
compileCode(codeWithDeadlyError)
println("\n\nDoing second compilation")
compileCode(code2WithNoError)
}
def compileCode(code: String): Unit = {
val run = new global.Run
val sourceFile = new BatchSourceFile("scripteditor", code)
global.reporter.reset()
run.compileSources(List(sourceFile))
if (global.reporter.hasErrors) {
println("Compilation failed!")
} else {
println("Compilation successful!")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment