From 5b179395ec0b7c6c767ae3944e5556df0e802e8f Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Sat, 27 Nov 2021 23:42:20 -0500 Subject: [PATCH] Fixes fake position handling, take 2 Fixes #6720 Ref #5994 Problem ------- Sometimes the compiler returns a fake position such as ``. This causes this causes InvalidPathException on Windows if we try to convert it into NIO path. Solution -------- Looks for less-than sign in the VirtualFileRef and skip those. Since BSP requires the diagnostic info to be associated with files, we probably can't do much. --- .../internal/server/BuildServerReporter.scala | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/main/src/main/scala/sbt/internal/server/BuildServerReporter.scala b/main/src/main/scala/sbt/internal/server/BuildServerReporter.scala index 41a35ee8e..dba366578 100644 --- a/main/src/main/scala/sbt/internal/server/BuildServerReporter.scala +++ b/main/src/main/scala/sbt/internal/server/BuildServerReporter.scala @@ -80,11 +80,17 @@ final class BuildServerReporterImpl( private lazy val exchange = StandardMain.exchange private val problemsByFile = mutable.Map[Path, Vector[Diagnostic]]() + // sometimes the compiler returns a fake position such as + // on Windows, this causes InvalidPathException (see #5994 and #6720) + private def toSafePath(ref: VirtualFileRef): Option[Path] = + if (ref.id().contains("<")) None + else Some(converter.toPath(ref)) + override def sendSuccessReport(analysis: CompileAnalysis): Unit = { for { (source, infos) <- analysis.readSourceInfos.getAllSourceInfos.asScala + filePath <- toSafePath(source) } { - val filePath = converter.toPath(source) val diagnostics = infos.getReportedProblems.toSeq.flatMap(toDiagnostic) val params = PublishDiagnosticsParams( textDocument = TextDocumentIdentifier(filePath.toUri), @@ -98,8 +104,10 @@ final class BuildServerReporterImpl( } override def sendFailureReport(sources: Array[VirtualFile]): Unit = { - for (source <- sources) { - val filePath = converter.toPath(source) + for { + source <- sources + filePath <- toSafePath(source) + } { val diagnostics = problemsByFile.getOrElse(filePath, Vector()) val params = PublishDiagnosticsParams( textDocument = TextDocumentIdentifier(filePath.toUri), @@ -116,8 +124,8 @@ final class BuildServerReporterImpl( for { id <- problem.position.sourcePath.toOption diagnostic <- toDiagnostic(problem) + filePath <- toSafePath(VirtualFileRef.of(id)) } { - val filePath = converter.toPath(VirtualFileRef.of(id)) problemsByFile(filePath) = problemsByFile.getOrElse(filePath, Vector()) :+ diagnostic val params = PublishDiagnosticsParams( TextDocumentIdentifier(filePath.toUri),