Skip to content

Commit f287cd1

Browse files
authored
bugfix: Print info diagnostics (#2990)
Fixes #2530
1 parent 6cfd71a commit f287cd1

File tree

2 files changed

+123
-45
lines changed

2 files changed

+123
-45
lines changed

modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -162,50 +162,45 @@ object ConsoleBloopBuildClient {
162162
path: Either[String, os.Path],
163163
diag: bsp4j.Diagnostic
164164
): Unit = {
165-
val isWarningOrErrorOrHint = diag.getSeverity == bsp4j.DiagnosticSeverity.ERROR ||
166-
diag.getSeverity == bsp4j.DiagnosticSeverity.WARNING ||
167-
diag.getSeverity == bsp4j.DiagnosticSeverity.HINT
168-
if (isWarningOrErrorOrHint) {
169-
val prefix = diagnosticPrefix(diag.getSeverity)
170-
171-
val line = (diag.getRange.getStart.getLine + 1).toString + ":"
172-
val col = (diag.getRange.getStart.getCharacter + 1).toString
173-
val msgIt = diag.getMessage.linesIterator
174-
175-
val path0 = path match {
176-
case Left(source) => source
177-
case Right(p) if p.startsWith(Os.pwd) =>
178-
"." + File.separator + p.relativeTo(Os.pwd).toString
179-
case Right(p) => p.toString
180-
}
181-
logger.error(s"$prefix$path0:$line$col")
182-
for (line <- msgIt)
183-
logger.error(prefix + line)
184-
val codeOpt = {
185-
val lineOpt =
186-
if (diag.getRange.getStart.getLine == diag.getRange.getEnd.getLine)
187-
Option(diag.getRange.getStart.getLine)
188-
else None
189-
for {
190-
line <- lineOpt
191-
p <- path.toOption
192-
lines = os.read.lines(p)
193-
line <- if (line < lines.length) Some(lines(line)) else None
194-
} yield line
195-
}
196-
for (code <- codeOpt)
197-
code.linesIterator.map(prefix + _).foreach(logger.error(_))
198-
val canPrintUnderline = diag.getRange.getStart.getLine == diag.getRange.getEnd.getLine &&
199-
diag.getRange.getStart.getCharacter != null &&
200-
diag.getRange.getEnd.getCharacter != null &&
201-
codeOpt.nonEmpty
202-
if (canPrintUnderline) {
203-
val len =
204-
math.max(1, diag.getRange.getEnd.getCharacter - diag.getRange.getStart.getCharacter)
205-
logger.error(
206-
prefix + " " * diag.getRange.getStart.getCharacter + "^" * len
207-
)
208-
}
165+
val prefix = diagnosticPrefix(diag.getSeverity)
166+
167+
val line = (diag.getRange.getStart.getLine + 1).toString + ":"
168+
val col = (diag.getRange.getStart.getCharacter + 1).toString
169+
val msgIt = diag.getMessage.linesIterator
170+
171+
val path0 = path match {
172+
case Left(source) => source
173+
case Right(p) if p.startsWith(Os.pwd) =>
174+
"." + File.separator + p.relativeTo(Os.pwd).toString
175+
case Right(p) => p.toString
176+
}
177+
logger.error(s"$prefix$path0:$line$col")
178+
for (line <- msgIt)
179+
logger.error(prefix + line)
180+
val codeOpt = {
181+
val lineOpt =
182+
if (diag.getRange.getStart.getLine == diag.getRange.getEnd.getLine)
183+
Option(diag.getRange.getStart.getLine)
184+
else None
185+
for {
186+
line <- lineOpt
187+
p <- path.toOption
188+
lines = os.read.lines(p)
189+
line <- if (line < lines.length) Some(lines(line)) else None
190+
} yield line
191+
}
192+
for (code <- codeOpt)
193+
code.linesIterator.map(prefix + _).foreach(logger.error(_))
194+
val canPrintUnderline = diag.getRange.getStart.getLine == diag.getRange.getEnd.getLine &&
195+
diag.getRange.getStart.getCharacter != null &&
196+
diag.getRange.getEnd.getCharacter != null &&
197+
codeOpt.nonEmpty
198+
if (canPrintUnderline) {
199+
val len =
200+
math.max(1, diag.getRange.getEnd.getCharacter - diag.getRange.getStart.getCharacter)
201+
logger.error(
202+
prefix + " " * diag.getRange.getStart.getCharacter + "^" * len
203+
)
209204
}
210205
}
211206

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,86 @@
11
package scala.cli.integration
22

3-
class CompileTests213 extends CompileTestDefinitions with Test213
3+
import scala.util.Properties
4+
5+
class CompileTests213 extends CompileTestDefinitions with Test213 {
6+
7+
test("test-macro-output") {
8+
val triple = "\"\"\""
9+
TestInputs(
10+
os.rel / "Main.scala" ->
11+
s"""|//> using scala ${Constants.scala213}
12+
|//> using dep org.scala-lang:scala-reflect:${Constants.scala213}
13+
|package example
14+
|import scala.reflect.macros.blackbox
15+
|import scala.language.experimental.macros
16+
|
17+
|object Scala2Example {
18+
| def macroMethod[A](a: A): String =
19+
| macro Scala2Example.macroMethodImpl[A]
20+
|
21+
| def macroMethodImpl[A: c.WeakTypeTag](
22+
| c: blackbox.Context
23+
| )(a: c.Expr[A]): c.Expr[String] = {
24+
| import c.universe._
25+
| val output = s$triple$${show(a.tree)}
26+
| |$${showCode(a.tree)}
27+
| |$${showRaw(a.tree)}
28+
| |$${weakTypeTag[A]}
29+
| |$${weakTypeOf[A]}
30+
| |$${showRaw(weakTypeOf[A])}$triple.stripMargin
31+
| c.echo(c.enclosingPosition, output)
32+
| c.warning(c.enclosingPosition, "example error message")
33+
| c.abort(c.enclosingPosition, "example error message")
34+
| }
35+
|}
36+
|""".stripMargin,
37+
os.rel / "Test.test.scala" ->
38+
"""|//> using test.dep org.scalameta::munit::1.0.0
39+
|package example
40+
|
41+
|class Tests extends munit.FunSuite {
42+
| test("macro works OK") {
43+
| Scala2Example.macroMethod(1 -> "test")
44+
| }
45+
|}""".stripMargin
46+
).fromRoot { root =>
47+
val result = os.proc(TestUtil.cli, "test", ".").call(
48+
cwd = root,
49+
check = false,
50+
// stdout = ProcessOutput.Readlines{ str => stringBuffer.append(str)},
51+
mergeErrIntoOut = true
52+
)
53+
val separator = if (Properties.isWin) "\\" else "/"
54+
55+
val expectedOutput =
56+
s"""|Compiling project (Scala ${Constants.scala213}, JVM (17))
57+
|Compiled project (Scala ${Constants.scala213}, JVM (17))
58+
|Compiling project (test, Scala ${Constants.scala213}, JVM (17))
59+
|[info] .${separator}Test.test.scala:6:5
60+
|[info] scala.Predef.ArrowAssoc[Int](1).->[String]("test")
61+
|[info] scala.Predef.ArrowAssoc[Int](1).->[String]("test")
62+
|[info] Apply(TypeApply(Select(Apply(TypeApply(Select(Select(Ident(scala), scala.Predef), TermName("ArrowAssoc")), List(TypeTree())), List(Literal(Constant(1)))), TermName("$$minus$$greater")), List(TypeTree())), List(Literal(Constant("test"))))
63+
|[info] WeakTypeTag[(Int, String)]
64+
|[info] (Int, String)
65+
|[info] TypeRef(ThisType(scala), scala.Tuple2, List(TypeRef(ThisType(scala), scala.Int, List()), TypeRef(ThisType(java.lang), java.lang.String, List())))
66+
|[info] Scala2Example.macroMethod(1 -> "test")
67+
|[info] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
|[error] .${separator}Test.test.scala:6:5
69+
|[error] example error message
70+
|[error] Scala2Example.macroMethod(1 -> "test")
71+
|[error] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
|Error compiling project (test, Scala ${Constants.scala213}, JVM (17))
73+
|Compilation failed
74+
|""".stripMargin
75+
76+
assertNoDiff(
77+
result.toString.trim().linesIterator.filterNot { str =>
78+
// these lines are not stable and can easily change
79+
val shouldNotContain = Set("Starting compilation server", "hint", "Download", "Result of")
80+
shouldNotContain.exists(str.contains)
81+
}.mkString("\n"),
82+
expectedOutput
83+
)
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)