Skip to content

Commit b461e7b

Browse files
committed
Avoid "too general" compilation errors, prefer more specific ones instead
1 parent bb49e04 commit b461e7b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

compiler/src/test/scala-3/play/twirl/compiler/test/Helper.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ object Helper {
9292
val reporter = driver.compile()
9393

9494
if (reporter.hasErrors) {
95-
val error = reporter.allErrors.sortBy(_.pos.point).head
95+
val errorsSortedAll = reporter.allErrors.sortBy(_.pos.point)
96+
val errorsSortedWithoutZeroPos =
97+
errorsSortedAll.filterNot(e => mapper.mapLine(e.pos.line + 1) == 0 && mapper.mapPosition(e.pos.point) == 0)
98+
// We filter out any errors that are mapped to line 0 and column 0, because since Scalameta 4.14.1 it's likely
99+
// those are very general errors. However, fall back to all errors in case after filtering no errors are left.
100+
val error = (if (errorsSortedWithoutZeroPos.isEmpty) errorsSortedAll else errorsSortedWithoutZeroPos).head
96101
val message = error.msg
97102
val pos = error.pos
98103
throw CompilationError(message.toString, mapper.mapLine(pos.line + 1), mapper.mapPosition(pos.point))

compiler/src/test/scala/play/twirl/compiler/test/CompilerSpec.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,23 @@ class CompilerSpec extends AnyWordSpec with Matchers {
200200
}
201201

202202
"fail compilation for error.scala.html" in {
203+
val msg = if (BuildInfo.scalaVersion.startsWith("3.")) "Not found: world" else "not found: value world"
203204
val helper = newCompilerHelper
204205
the[CompilationError] thrownBy helper.compile[(() => Html)]("error.scala.html", "html.error") must have(
205206
Symbol("line")(5),
206-
// Symbol("column")(12) TODO: need fix https://github.com/playframework/twirl/issues/571 to back
207+
Symbol("message")(msg),
207208
Symbol("column")(463)
208209
)
209210
}
210211

211212
"fail compilation for errorInTemplateArgs.scala.html" in {
212213
val helper = newCompilerHelper
214+
val msg =
215+
if (BuildInfo.scalaVersion.startsWith("3.")) "':' expected, but ')' found" else "':' expected but ')' found."
213216
the[CompilationError] thrownBy helper
214217
.compile[(() => Html)]("errorInTemplateArgs.scala.html", "html.errorInTemplateArgs") must have(
215218
Symbol("line")(5),
216-
// Symbol("column")(6) TODO: need fix https://github.com/playframework/twirl/issues/571 to back
219+
Symbol("message")(msg),
217220
Symbol("column")(458)
218221
)
219222
}

0 commit comments

Comments
 (0)