From 275dc3fa7493201344604e94eb1b052c272544e8 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Thu, 15 Aug 2024 17:53:32 +0200 Subject: [PATCH] bugfix: Catch possible syntax errors Previously, if we tried to calculate semanticdb for a file with parsing errors we would have an exception thrown. Now, we just return an empty TextDocument and not reprot that to the user as it will already be reproted as a diagnostic. --- .../internal/pc/SemanticdbTextDocumentProvider.scala | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/SemanticdbTextDocumentProvider.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/SemanticdbTextDocumentProvider.scala index 721e008bc67..219a85369aa 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/SemanticdbTextDocumentProvider.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/SemanticdbTextDocumentProvider.scala @@ -11,6 +11,8 @@ import scala.meta.internal.mtags.MtagsEnrichments._ import scala.meta.internal.semanticdb.scalac.SemanticdbConfig import scala.meta.internal.{semanticdb => s} import scala.meta.io.AbsolutePath +import scala.meta.parsers.ParseException +import scala.meta.tokenizers.TokenizeException class SemanticdbTextDocumentProvider( val compiler: MetalsGlobal, @@ -51,7 +53,13 @@ class SemanticdbTextDocumentProvider( } // we recalculate md5, since there seems to be issue with newlines sometimes val document = - unit.toTextDocument(explicitDialect).withMd5(MD5.compute(code)) + try { + unit.toTextDocument(explicitDialect).withMd5(MD5.compute(code)) + } catch { + case _: TokenizeException | _: ParseException => + s.TextDocument.defaultInstance + } + compiler.workspace .flatMap { workspacePath => scala.util.Try(workspacePath.relativize(filePath.toNIO)).toOption