Skip to content

Commit

Permalink
Improvement: compiler access error reporting
Browse files Browse the repository at this point in the history
Added:
1. printing stacktrace
2. add offset/range marker to text
  • Loading branch information
kasiaMarek authored and tgodzik committed Jul 14, 2023
1 parent 3161d13 commit 2b00208
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ case class Report(
def fullText: String =
error match {
case Some(error) =>
s"""|$text
|Error message: ${error.getMessage()}
|Error: $error
s"""|$error
|$text
|
|error stacktrace:
|${error.getStackTrace().mkString("\n\t")}
|""".stripMargin
case None => text
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import scala.meta.internal.jdk.CollectionConverters._
import scala.meta.internal.metals.CompilerOffsetParams
import scala.meta.internal.metals.CompilerRangeParams
import scala.meta.internal.pc.CompletionItemData
import scala.meta.internal.pc.RangeOffset
import scala.meta.pc.OffsetParams
import scala.meta.pc.RangeParams
import scala.meta.pc.VirtualFileParams

import com.google.gson.Gson
import com.google.gson.JsonElement
Expand Down Expand Up @@ -338,4 +340,35 @@ trait CommonMtagsEnrichments {
}

}

implicit class XtensionVirtualFileParams(params: VirtualFileParams) {
def printed(marker: String = "@@"): String = {
def textWithPosMarker(markerPos: Int, text: String) =
if (markerPos < 0) marker ++ text
else if (markerPos >= text.length()) text ++ marker
else {
val (head, tail) = text.splitAt(markerPos)
head ++ marker ++ tail
}
params match {
case r: RangeOffset =>
val withStartMarker = textWithPosMarker(r.start, r.text())
val withMarkers =
textWithPosMarker(r.end + marker.length(), withStartMarker)
s"""|range: ${r.start} - ${r.end}
|uri: ${r.uri()}
|text:
|$withMarkers""".stripMargin
case o: OffsetParams =>
s"""|offset: ${o.offset()}
|uri: ${o.uri()}
|text:
|${textWithPosMarker(o.offset(), o.text())}""".stripMargin
case v =>
s"""|uri: ${v.uri()}
|text:
|${v.text()}""".stripMargin
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import scala.util.control.NonFatal

import scala.meta.internal.metals.Report
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.mtags.CommonMtagsEnrichments._
import scala.meta.pc.CancelToken
import scala.meta.pc.OffsetParams
import scala.meta.pc.PresentationCompilerConfig
import scala.meta.pc.VirtualFileParams

Expand Down Expand Up @@ -193,47 +193,27 @@ abstract class CompilerAccess[Reporter, Compiler](
e: Throwable,
params: Option[VirtualFileParams]
): Unit = {
def virtualFileParamsToString(v: VirtualFileParams) =
s"""|file uri: ${v.uri()}
|file text:
|${v.text()}
|""".stripMargin
val stacktrace = Thread.currentThread().getStackTrace().mkString("\n")
val error = CompilerThrowable.trimStackTrace(e)
val paramsText =
params match {
case None => ""
case Some(r: RangeOffset) =>
s"""|range ${r.start} - ${r.end}
|${virtualFileParamsToString(r)}
|""".stripMargin
case Some(o: OffsetParams) =>
s"""|offset ${o.offset()}
|${virtualFileParamsToString(o)}
|""".stripMargin
case Some(v) => virtualFileParamsToString(v)
}
val pathToFull =
rc.unsanitized.create(
Report(
"compiler-error",
s"""|An error occurred in the presentation compiler:
|context stacktrace:
|${stacktrace}
|action params:
|$paramsText
|""".stripMargin,
error = Some(error)
)
val report =
Report(
"compiler-error",
s"""|occurred in the presentation compiler.
|
|action parameters:
|${params.map(_.printed()).getOrElse("<NONE>")}
|""".stripMargin,
error
)
pathToFull match {
val pathToReport =
rc.unsanitized.create(report)
pathToReport match {
case Some(path) =>
logger.log(
Level.SEVERE,
s"A severe compiler error occurred, full details of the error can be found in the error report $path"
)
case _ =>
logger.log(Level.SEVERE, e.getMessage, e)
logger.log(Level.SEVERE, error.getMessage, error)
}
shutdownCurrentCompiler()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ object CompilerThrowable {
)
@tailrec def loop(ex: Throwable): Unit = {
isVisited.add(ex)
val stacktrace = ex.getStackTrace.takeWhile(
!_.getClassName.contains("ScalaPresentationCompiler")
)
val fullStacktrace = ex.getStackTrace
val stacktrace = {
val i = fullStacktrace.indexWhere(
_.getClassName.contains("ScalaPresentationCompiler")
)
val hi = if (i < 0) fullStacktrace.length else i + 1
fullStacktrace.slice(0, hi)
}
ex.setStackTrace(stacktrace)
// avoid infinite loop when traversing exceptions cyclic dependencies between causes.
if (e.getCause != null && !isVisited.contains(e.getCause)) {
Expand Down

0 comments on commit 2b00208

Please sign in to comment.