Skip to content

Commit 6e4c93f

Browse files
committed
bugfix: Treat -Werror the same as -Xfatal-warnings
Context scala/scala3#20041
1 parent 871b812 commit 6e4c93f

File tree

3 files changed

+170
-166
lines changed

3 files changed

+170
-166
lines changed

backend/src/main/scala/bloop/Compiler.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ object Compiler {
249249
}
250250
}
251251

252+
private def isFatalWarningOpt(opt: String) = opt == "-Xfatal-warnings" || opt == "-Werror"
253+
252254
def compile(
253255
compileInputs: CompileInputs,
254256
isBestEffortMode: Boolean,
@@ -295,7 +297,7 @@ object Compiler {
295297
}
296298

297299
val isFatalWarningsEnabled: Boolean =
298-
compileInputs.scalacOptions.exists(_ == "-Xfatal-warnings")
300+
compileInputs.scalacOptions.exists(isFatalWarningOpt)
299301
def getInputs(compilers: Compilers): Inputs = {
300302
val options = getCompilationOptions(compileInputs, logger, newClassesDir)
301303
val setup = getSetup(compileInputs)
@@ -916,7 +918,7 @@ object Compiler {
916918
inputs.scalaInstance.version
917919
)
918920

919-
val optionsWithoutFatalWarnings = scalacOptions.filter(_ != "-Xfatal-warnings")
921+
val optionsWithoutFatalWarnings = scalacOptions.filterNot(isFatalWarningOpt)
920922
val areFatalWarningsEnabled = scalacOptions.length != optionsWithoutFatalWarnings.length
921923

922924
// Enable fatal warnings in the reporter if they are enabled in the build

frontend/src/test/scala/bloop/BaseCompileSpec.scala

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,52 +1231,53 @@ abstract class BaseCompileSpec extends bloop.testing.BaseSuite {
12311231
}
12321232
}
12331233

1234-
test("support -Xfatal-warnings internal implementation") {
1235-
TestUtil.withinWorkspace { workspace =>
1236-
object Sources {
1237-
val `Foo.scala` =
1238-
"""/Foo.scala
1239-
|import Predef.assert
1240-
|class Foo
1234+
for (fatalOpt <- List("-Xfatal-warnings", "-Werror"))
1235+
test(s"support $fatalOpt internal implementation") {
1236+
TestUtil.withinWorkspace { workspace =>
1237+
object Sources {
1238+
val `Foo.scala` =
1239+
"""/Foo.scala
1240+
|import Predef.assert
1241+
|class Foo
12411242
""".stripMargin
1242-
val `Bar.scala` =
1243-
"""/Bar.scala
1244-
|class Bar
1243+
val `Bar.scala` =
1244+
"""/Bar.scala
1245+
|class Bar
12451246
""".stripMargin
1246-
val `Baz.scala` =
1247-
"""/Baz.scala
1248-
|class Baz
1247+
val `Baz.scala` =
1248+
"""/Baz.scala
1249+
|class Baz
12491250
""".stripMargin
1250-
}
1251+
}
12511252

1252-
val logger = new RecordingLogger(ansiCodesSupported = false)
1253-
val sources = List(Sources.`Bar.scala`, Sources.`Foo.scala`, Sources.`Baz.scala`)
1254-
val options = List("-Ywarn-unused", "-Xfatal-warnings")
1255-
val `A` = TestProject(workspace, "a", sources, scalacOptions = options)
1256-
val projects = List(`A`)
1257-
val state = loadState(workspace, projects, logger)
1258-
val compiledState = state.compile(`A`)
1259-
assertExitStatus(compiledState, ExitStatus.CompilationError)
1260-
// Despite error, compilation of project should be valid
1261-
assertValidCompilationState(compiledState, projects)
1253+
val logger = new RecordingLogger(ansiCodesSupported = false)
1254+
val sources = List(Sources.`Bar.scala`, Sources.`Foo.scala`, Sources.`Baz.scala`)
1255+
val options = List("-Ywarn-unused", fatalOpt)
1256+
val `A` = TestProject(workspace, "a", sources, scalacOptions = options)
1257+
val projects = List(`A`)
1258+
val state = loadState(workspace, projects, logger)
1259+
val compiledState = state.compile(`A`)
1260+
assertExitStatus(compiledState, ExitStatus.CompilationError)
1261+
// Despite error, compilation of project should be valid
1262+
assertValidCompilationState(compiledState, projects)
12621263

1263-
val targetFoo = TestUtil.universalPath("a/src/Foo.scala")
1264-
assertNoDiff(
1265-
logger.renderErrors(exceptContaining = "Failed to compile"),
1266-
s"""|[E1] ${targetFoo}:1:15
1267-
| Unused import
1268-
| L1: import Predef.assert
1269-
| ^^^^^^^
1270-
|""".stripMargin
1271-
)
1272-
assertDiagnosticsResult(
1273-
compiledState.getLastResultFor(`A`),
1274-
errors = 0,
1275-
warnings = 1,
1276-
expectFatalWarnings = true
1277-
)
1264+
val targetFoo = TestUtil.universalPath("a/src/Foo.scala")
1265+
assertNoDiff(
1266+
logger.renderErrors(exceptContaining = "Failed to compile"),
1267+
s"""|[E1] ${targetFoo}:1:15
1268+
| Unused import
1269+
| L1: import Predef.assert
1270+
| ^^^^^^^
1271+
|""".stripMargin
1272+
)
1273+
assertDiagnosticsResult(
1274+
compiledState.getLastResultFor(`A`),
1275+
errors = 0,
1276+
warnings = 1,
1277+
expectFatalWarnings = true
1278+
)
1279+
}
12781280
}
1279-
}
12801281

12811282
test("scalac -Xfatal-warnings should not set java fatal warnings as errors") {
12821283
TestUtil.withinWorkspace { workspace =>

frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala

Lines changed: 125 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,144 +1109,145 @@ class BspCompileSpec(
11091109
}
11101110
}
11111111

1112-
test("support -Xfatal-warnings internal implementation") {
1113-
TestUtil.withinWorkspace { workspace =>
1114-
object Sources {
1115-
val `Foo.scala` =
1116-
"""/Foo.scala
1117-
|import Predef.assert
1118-
|class Foo
1112+
for (fatalOpt <- List("-Xfatal-warnings", "-Werror"))
1113+
test(s"support $fatalOpt internal implementation") {
1114+
TestUtil.withinWorkspace { workspace =>
1115+
object Sources {
1116+
val `Foo.scala` =
1117+
"""/Foo.scala
1118+
|import Predef.assert
1119+
|class Foo
11191120
""".stripMargin
1120-
val `Bar.scala` =
1121-
"""/Bar.scala
1122-
|class Bar
1121+
val `Bar.scala` =
1122+
"""/Bar.scala
1123+
|class Bar
11231124
""".stripMargin
1124-
val `Baz.scala` =
1125-
"""/Baz.scala
1126-
|class Baz
1125+
val `Baz.scala` =
1126+
"""/Baz.scala
1127+
|class Baz
11271128
""".stripMargin
1128-
val `Foo2.scala` =
1129-
"""/Foo.scala
1130-
|class Foo
1129+
val `Foo2.scala` =
1130+
"""/Foo.scala
1131+
|class Foo
11311132
""".stripMargin
1132-
val `Foo3.scala` =
1133-
"""/Foo.scala
1134-
|import Predef.assert
1135-
|import Predef.Manifest
1136-
|class Foo
1133+
val `Foo3.scala` =
1134+
"""/Foo.scala
1135+
|import Predef.assert
1136+
|import Predef.Manifest
1137+
|class Foo
11371138
""".stripMargin
1138-
val `Buzz.scala` =
1139-
"""/Buzz.scala
1140-
|class Buzz
1139+
val `Buzz.scala` =
1140+
"""/Buzz.scala
1141+
|class Buzz
11411142
""".stripMargin
1142-
val `Buzz2.scala` =
1143-
"""/Buzz.scala
1144-
|import Predef.assert
1145-
|class Buzz
1143+
val `Buzz2.scala` =
1144+
"""/Buzz.scala
1145+
|import Predef.assert
1146+
|class Buzz
11461147
""".stripMargin
1147-
}
1148+
}
11481149

1149-
val bspLogger = new RecordingLogger(ansiCodesSupported = false)
1150-
val sourcesA = List(Sources.`Bar.scala`, Sources.`Foo.scala`, Sources.`Baz.scala`)
1151-
val sourcesB = List(Sources.`Buzz.scala`)
1152-
val options = List("-Ywarn-unused", "-Xfatal-warnings")
1153-
val `A` = TestProject(workspace, "a", sourcesA, scalacOptions = options)
1154-
val `B` = TestProject(workspace, "b", sourcesB, List(`A`), scalacOptions = options)
1155-
val projects = List(`A`, `B`)
1156-
loadBspState(workspace, projects, bspLogger) { state =>
1157-
val compiledState = state.compile(`B`)
1158-
assertExitStatus(compiledState, ExitStatus.CompilationError)
1159-
assertValidCompilationState(compiledState, projects)
1160-
assertNoDiff(
1161-
compiledState.lastDiagnostics(`A`),
1162-
"""|#1: task start 1
1163-
| -> Msg: Compiling a (3 Scala sources)
1164-
| -> Data kind: compile-task
1165-
|#1: a/src/Foo.scala
1166-
| -> List(Diagnostic(Range(Position(0,0),Position(0,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1167-
| -> reset = true
1168-
|#1: task finish 1
1169-
| -> errors 1, warnings 0
1170-
| -> Msg: Compiled 'a'
1171-
| -> Data kind: compile-report
1172-
|""".stripMargin
1173-
)
1150+
val bspLogger = new RecordingLogger(ansiCodesSupported = false)
1151+
val sourcesA = List(Sources.`Bar.scala`, Sources.`Foo.scala`, Sources.`Baz.scala`)
1152+
val sourcesB = List(Sources.`Buzz.scala`)
1153+
val options = List("-Ywarn-unused", fatalOpt)
1154+
val `A` = TestProject(workspace, "a", sourcesA, scalacOptions = options)
1155+
val `B` = TestProject(workspace, "b", sourcesB, List(`A`), scalacOptions = options)
1156+
val projects = List(`A`, `B`)
1157+
loadBspState(workspace, projects, bspLogger) { state =>
1158+
val compiledState = state.compile(`B`)
1159+
assertExitStatus(compiledState, ExitStatus.CompilationError)
1160+
assertValidCompilationState(compiledState, projects)
1161+
assertNoDiff(
1162+
compiledState.lastDiagnostics(`A`),
1163+
"""|#1: task start 1
1164+
| -> Msg: Compiling a (3 Scala sources)
1165+
| -> Data kind: compile-task
1166+
|#1: a/src/Foo.scala
1167+
| -> List(Diagnostic(Range(Position(0,0),Position(0,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1168+
| -> reset = true
1169+
|#1: task finish 1
1170+
| -> errors 1, warnings 0
1171+
| -> Msg: Compiled 'a'
1172+
| -> Data kind: compile-report
1173+
|""".stripMargin
1174+
)
11741175

1175-
assertNoDiff(
1176-
compiledState.lastDiagnostics(`B`),
1177-
"""|#1: task start 2
1178-
| -> Msg: Compiling b (1 Scala source)
1179-
| -> Data kind: compile-task
1180-
|#1: task finish 2
1181-
| -> errors 0, warnings 0
1182-
| -> Msg: Compiled 'b'
1183-
| -> Data kind: compile-report
1184-
|""".stripMargin
1185-
)
1176+
assertNoDiff(
1177+
compiledState.lastDiagnostics(`B`),
1178+
"""|#1: task start 2
1179+
| -> Msg: Compiling b (1 Scala source)
1180+
| -> Data kind: compile-task
1181+
|#1: task finish 2
1182+
| -> errors 0, warnings 0
1183+
| -> Msg: Compiled 'b'
1184+
| -> Data kind: compile-report
1185+
|""".stripMargin
1186+
)
11861187

1187-
writeFile(`A`.srcFor("/Foo.scala"), Sources.`Foo2.scala`)
1188-
val secondCompiledState = compiledState.compile(`B`)
1189-
assertExitStatus(secondCompiledState, ExitStatus.Ok)
1190-
assertValidCompilationState(secondCompiledState, projects)
1191-
assertNoDiff(
1192-
secondCompiledState.lastDiagnostics(`A`),
1193-
"""|#2: task start 3
1194-
| -> Msg: Compiling a (1 Scala source)
1195-
| -> Data kind: compile-task
1196-
|#2: a/src/Foo.scala
1197-
| -> List()
1198-
| -> reset = true
1199-
|#2: task finish 3
1200-
| -> errors 0, warnings 0
1201-
| -> Msg: Compiled 'a'
1202-
| -> Data kind: compile-report
1203-
|""".stripMargin
1204-
)
1188+
writeFile(`A`.srcFor("/Foo.scala"), Sources.`Foo2.scala`)
1189+
val secondCompiledState = compiledState.compile(`B`)
1190+
assertExitStatus(secondCompiledState, ExitStatus.Ok)
1191+
assertValidCompilationState(secondCompiledState, projects)
1192+
assertNoDiff(
1193+
secondCompiledState.lastDiagnostics(`A`),
1194+
"""|#2: task start 3
1195+
| -> Msg: Compiling a (1 Scala source)
1196+
| -> Data kind: compile-task
1197+
|#2: a/src/Foo.scala
1198+
| -> List()
1199+
| -> reset = true
1200+
|#2: task finish 3
1201+
| -> errors 0, warnings 0
1202+
| -> Msg: Compiled 'a'
1203+
| -> Data kind: compile-report
1204+
|""".stripMargin
1205+
)
12051206

1206-
assertNoDiff(
1207-
compiledState.lastDiagnostics(`B`),
1208-
"" // no-op
1209-
)
1207+
assertNoDiff(
1208+
compiledState.lastDiagnostics(`B`),
1209+
"" // no-op
1210+
)
12101211

1211-
writeFile(`A`.srcFor("/Foo.scala"), Sources.`Foo3.scala`)
1212-
writeFile(`B`.srcFor("/Buzz.scala"), Sources.`Buzz2.scala`)
1213-
val thirdCompiledState = secondCompiledState.compile(`B`)
1214-
assertExitStatus(thirdCompiledState, ExitStatus.CompilationError)
1215-
assertValidCompilationState(thirdCompiledState, projects)
1216-
assertNoDiff(
1217-
thirdCompiledState.lastDiagnostics(`A`),
1218-
"""|#3: task start 4
1219-
| -> Msg: Compiling a (1 Scala source)
1220-
| -> Data kind: compile-task
1221-
|#3: a/src/Foo.scala
1222-
| -> List(Diagnostic(Range(Position(0,0),Position(0,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1223-
| -> reset = true
1224-
|#3: a/src/Foo.scala
1225-
| -> List(Diagnostic(Range(Position(1,0),Position(1,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1226-
| -> reset = false
1227-
|#3: task finish 4
1228-
| -> errors 2, warnings 0
1229-
| -> Msg: Compiled 'a'
1230-
| -> Data kind: compile-report
1231-
|""".stripMargin
1232-
)
1212+
writeFile(`A`.srcFor("/Foo.scala"), Sources.`Foo3.scala`)
1213+
writeFile(`B`.srcFor("/Buzz.scala"), Sources.`Buzz2.scala`)
1214+
val thirdCompiledState = secondCompiledState.compile(`B`)
1215+
assertExitStatus(thirdCompiledState, ExitStatus.CompilationError)
1216+
assertValidCompilationState(thirdCompiledState, projects)
1217+
assertNoDiff(
1218+
thirdCompiledState.lastDiagnostics(`A`),
1219+
"""|#3: task start 4
1220+
| -> Msg: Compiling a (1 Scala source)
1221+
| -> Data kind: compile-task
1222+
|#3: a/src/Foo.scala
1223+
| -> List(Diagnostic(Range(Position(0,0),Position(0,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1224+
| -> reset = true
1225+
|#3: a/src/Foo.scala
1226+
| -> List(Diagnostic(Range(Position(1,0),Position(1,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1227+
| -> reset = false
1228+
|#3: task finish 4
1229+
| -> errors 2, warnings 0
1230+
| -> Msg: Compiled 'a'
1231+
| -> Data kind: compile-report
1232+
|""".stripMargin
1233+
)
12331234

1234-
assertNoDiff(
1235-
compiledState.lastDiagnostics(`B`),
1236-
"""|#3: task start 5
1237-
| -> Msg: Compiling b (1 Scala source)
1238-
| -> Data kind: compile-task
1239-
|#3: b/src/Buzz.scala
1240-
| -> List(Diagnostic(Range(Position(0,0),Position(0,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1241-
| -> reset = true
1242-
|#3: task finish 5
1243-
| -> errors 1, warnings 0
1244-
| -> Msg: Compiled 'b'
1245-
| -> Data kind: compile-report
1246-
|""".stripMargin
1247-
)
1235+
assertNoDiff(
1236+
compiledState.lastDiagnostics(`B`),
1237+
"""|#3: task start 5
1238+
| -> Msg: Compiling b (1 Scala source)
1239+
| -> Data kind: compile-task
1240+
|#3: b/src/Buzz.scala
1241+
| -> List(Diagnostic(Range(Position(0,0),Position(0,7)),Some(Error),Some(_),Some(_),Unused import,None,None,Some({"actions":[]})))
1242+
| -> reset = true
1243+
|#3: task finish 5
1244+
| -> errors 1, warnings 0
1245+
| -> Msg: Compiled 'b'
1246+
| -> Data kind: compile-report
1247+
|""".stripMargin
1248+
)
1249+
}
12481250
}
12491251
}
1250-
}
12511252

12521253
}

0 commit comments

Comments
 (0)