diff --git a/modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala b/modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala index 7055e29e80..6580c7737b 100644 --- a/modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala +++ b/modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala @@ -4,7 +4,7 @@ import ch.epfl.scala.bsp4j import java.io.File import java.net.URI -import java.nio.file.Paths +import java.nio.file.{NoSuchFileException, Paths} import scala.build.errors.Severity import scala.build.internal.WrapperParams @@ -185,12 +185,19 @@ object ConsoleBloopBuildClient { for { line <- lineOpt p <- path.toOption - lines = os.read.lines(p) - line <- if (line < lines.length) Some(lines(line)) else None + lines = + try + os.read.lines(p) + catch + case e: NoSuchFileException => + logger.message(s"File not found: $p") + logger.error(e.getMessage) + Nil + line <- lines.lift(line) } yield line } for (code <- codeOpt) - code.linesIterator.map(prefix + _).foreach(logger.error(_)) + code.linesIterator.map(prefix + _).foreach(logger.error) val canPrintUnderline = diag.getRange.getStart.getLine == diag.getRange.getEnd.getLine && diag.getRange.getStart.getCharacter != null && diag.getRange.getEnd.getCharacter != null && diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunScalacCompatTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunScalacCompatTestDefinitions.scala index 19255b9e53..0d07245781 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunScalacCompatTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunScalacCompatTestDefinitions.scala @@ -606,4 +606,40 @@ trait RunScalacCompatTestDefinitions { expect(r.out.trim() == macroSettings.mkString(", ")) } } + + if (actualScalaVersion.startsWith("3")) + test("-Ysafe-init-global doesnt crash the CLI when a dependency produces a warning") { + val (org, name, version) = ("hello-world", "test-safe-init-global-works", "0.0.1-SNAPSHOT") + val (depDir, mainDir) = ("dep", "main") + val (depPackage, depClass, depMethod) = ("hello", "DepMain", "x") + val expectedMessage = "Hello, world!" + TestInputs( + os.rel / depDir / "Main.scala" -> + s"""//> using publish.organization $org + |//> using publish.name $name + |//> using publish.version $version + |package $depPackage + | + |object $depClass { + | val $depMethod: Int = y + | val y: Int = $depMethod + |} + |""".stripMargin, + os.rel / mainDir / "Main.scala" -> + s"""//> using option -Ysafe-init-global + |//> using dep $org::$name:$version + | + |import $depPackage.$depClass + | + |object Main extends App { + | val a = $depClass.$depMethod + | println("$expectedMessage") + |} + |""".stripMargin + ).fromRoot { root => + os.proc(TestUtil.cli, "--power", "publish", "local", depDir, extraOptions).call(cwd = root) + val res = os.proc(TestUtil.cli, "run", mainDir, extraOptions).call(cwd = root) + expect(res.out.trim() == expectedMessage) + } + } }