From 603a23c295d800372111f517267062a05cafd652 Mon Sep 17 00:00:00 2001 From: Mason Edmison Date: Sun, 2 Jun 2024 21:03:12 -0500 Subject: [PATCH 1/2] fix: Only consider scala source files when lifting fatal errors --- .../main/scala/bloop/reporter/Reporter.scala | 10 +++- .../bloop/reporter/BspProjectReporter.scala | 2 +- .../test/scala/bloop/BaseCompileSpec.scala | 58 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/backend/src/main/scala/bloop/reporter/Reporter.scala b/backend/src/main/scala/bloop/reporter/Reporter.scala index 6e0bb5c3e4..1351285f65 100644 --- a/backend/src/main/scala/bloop/reporter/Reporter.scala +++ b/backend/src/main/scala/bloop/reporter/Reporter.scala @@ -75,11 +75,15 @@ abstract class Reporter( } protected def liftFatalWarning(problem: Problem): Problem = { - val isFatalWarning = hasFatalWarningsEnabled && problem.severity == Severity.Warn + lazy val sourceFile = InterfaceUtil.toOption(problem.position.sourceFile()) + // Only fatal warnings within scala source files are + // promoted to errors + val isFatalWarning = + hasFatalWarningsEnabled && problem.severity == Severity.Warn && + sourceFile.exists(_.getCanonicalPath().split('.').last == "scala") if (!isFatalWarning) problem else { - InterfaceUtil - .toOption(problem.position.sourceFile()) + sourceFile .foreach(f => sourceFilesWithFatalWarnings.put(f, true)) problem.copy(severity = Severity.Error) diff --git a/frontend/src/main/scala/bloop/reporter/BspProjectReporter.scala b/frontend/src/main/scala/bloop/reporter/BspProjectReporter.scala index dd01baa3a8..39fd1e0048 100644 --- a/frontend/src/main/scala/bloop/reporter/BspProjectReporter.scala +++ b/frontend/src/main/scala/bloop/reporter/BspProjectReporter.scala @@ -130,7 +130,7 @@ final class BspProjectReporter( * 2. There is no way to know if an incremental cycle will be the last one in * `reportEndIncrementalCycle`. We work around this limitation with this approach, so that * when the thunk is run from `reportStartIncrementalCycle` we know a new cycle is coming - * and when it's run from `reportEndIncrementalCompilation` we know it's the last cycle. + * and when it's run from `reportEndIncrementalCycle` we know it's the last cycle. */ private def processEndPreviousCycle( inputs: CycleInputs, diff --git a/frontend/src/test/scala/bloop/BaseCompileSpec.scala b/frontend/src/test/scala/bloop/BaseCompileSpec.scala index 6d823353c2..b741310e17 100644 --- a/frontend/src/test/scala/bloop/BaseCompileSpec.scala +++ b/frontend/src/test/scala/bloop/BaseCompileSpec.scala @@ -1163,6 +1163,64 @@ abstract class BaseCompileSpec extends bloop.testing.BaseSuite { } } + test("scalac -Xfatal-warnings should not set java fatal warnings as errors") { + TestUtil.withinWorkspace { workspace => + object Sources { + val `Main.scala` = + """/Main.scala + |object Main extends App { + | println("Hello, World!") + |} + """.stripMargin + val `Service.java` = + """/Service.java + |package com.example; + |import java.util.List; + |public class Service { + | String aaa = 123; + | public void create(List args) { + | var second = (java.util.Optional) args.get(2); + | System.out.println(second); + | } + |} + """.stripMargin + } + + val logger = new RecordingLogger(ansiCodesSupported = false) + val sources = List(Sources.`Main.scala`, Sources.`Service.java`) + val scalacOptions = List("-Xfatal-warnings") + val javacOptions = List("-Xlint:unchecked") + val `A` = TestProject( + workspace, + "a", + sources, + scalacOptions = scalacOptions, + javacOptions = javacOptions + ) + val projects = List(`A`) + val state = loadState(workspace, projects, logger) + val compiledState = state.compile(`A`) + assertExitStatus(compiledState, ExitStatus.CompilationError) + + val targetService = TestUtil.universalPath("a/src/Service.java") + assertNoDiff( + logger.renderErrors(exceptContaining = "Failed to compile"), + s"""[E1] ${targetService}:4 + | incompatible types: int cannot be converted to java.lang.String + | L4: 123 + | ^^^ + |a/src/Service.java: L4 [E1], L6 [E2] + |""".stripMargin + ) + assertDiagnosticsResult( + compiledState.getLastResultFor(`A`), + errors = 1, + warnings = 1, + expectFatalWarnings = false + ) + } + } + test("detect Scala syntactic errors") { TestUtil.withinWorkspace { workspace => val sources = List( From a8c6f9a14ea1221e9a770a4607424bebe5955f28 Mon Sep 17 00:00:00 2001 From: Mason Lazalier Edmison <36715397+masonedmison@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:59:53 -0500 Subject: [PATCH 2/2] Use universal path in test assertion Co-authored-by: Tomasz Godzik --- frontend/src/test/scala/bloop/BaseCompileSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/test/scala/bloop/BaseCompileSpec.scala b/frontend/src/test/scala/bloop/BaseCompileSpec.scala index b741310e17..7dba45821a 100644 --- a/frontend/src/test/scala/bloop/BaseCompileSpec.scala +++ b/frontend/src/test/scala/bloop/BaseCompileSpec.scala @@ -1209,7 +1209,7 @@ abstract class BaseCompileSpec extends bloop.testing.BaseSuite { | incompatible types: int cannot be converted to java.lang.String | L4: 123 | ^^^ - |a/src/Service.java: L4 [E1], L6 [E2] + |${TestUtil.universalPath("a/src/Service.java")}: L4 [E1], L6 [E2] |""".stripMargin ) assertDiagnosticsResult(