Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only consider scala source files when lifting fatal errors #2346

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions backend/src/main/scala/bloop/reporter/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
58 changes: 58 additions & 0 deletions frontend/src/test/scala/bloop/BaseCompileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> args) {
| var second = (java.util.Optional<String>) 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
| ^^^
|${TestUtil.universalPath("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(
Expand Down
Loading