handle fake positions in absoluteSourceMapper

This commit is contained in:
Adrien Piquerez 2020-10-21 15:41:23 +02:00
parent d1ff067d54
commit 2425ca4950
1 changed files with 21 additions and 10 deletions

View File

@ -21,6 +21,7 @@ import org.apache.logging.log4j.core.{ Appender => XAppender }
import org.scalasbt.ipcsocket.Win32SecurityLevel import org.scalasbt.ipcsocket.Win32SecurityLevel
import sbt.Def.{ Initialize, ScopedKey, Setting, SettingsDefinition } import sbt.Def.{ Initialize, ScopedKey, Setting, SettingsDefinition }
import sbt.Keys._ import sbt.Keys._
import sbt.OptionSyntax._
import sbt.Project.{ import sbt.Project.{
inConfig, inConfig,
inScope, inScope,
@ -91,6 +92,7 @@ import xsbti.{ FileConverter, Position }
import scala.collection.immutable.ListMap import scala.collection.immutable.ListMap
import scala.concurrent.duration._ import scala.concurrent.duration._
import scala.util.Try
import scala.util.control.NonFatal import scala.util.control.NonFatal
import scala.xml.NodeSeq import scala.xml.NodeSeq
@ -415,7 +417,7 @@ object Defaults extends BuildCommon {
sourcePositionMappers ++= { sourcePositionMappers ++= {
val fc = fileConverter.value val fc = fileConverter.value
if (reportAbsolutePath.value) { if (reportAbsolutePath.value) {
List(toAbsoluteSourceMapper(fc)) List(toAbsoluteSourceMapper(fc) _)
} else Nil } else Nil
}, },
// The virtual file value cache needs to be global or sbt will run out of direct byte buffer memory. // The virtual file value cache needs to be global or sbt will run out of direct byte buffer memory.
@ -464,13 +466,21 @@ object Defaults extends BuildCommon {
}, },
) )
private[sbt] def toAbsoluteSourceMapper(fc: FileConverter): Position => Option[Position] = { private[sbt] def toAbsoluteSourceMapper(fc: FileConverter)(pos: Position): Option[Position] = {
pos => def isValid(path: String): Boolean = {
val newPath: Optional[String] = pos.sourcePath Try(Paths.get(path)).map(_ => true).getOrElse(false)
.map { id =>
fc.toPath(VirtualFileRef.of(id)).toAbsolutePath.toString
} }
Some(
val newPath: Option[String] = pos
.sourcePath()
.asScala
.filter(isValid)
.map { path =>
fc.toPath(VirtualFileRef.of(path)).toAbsolutePath.toString
}
newPath
.map { path =>
new Position { new Position {
override def line(): Optional[Integer] = pos.line() override def line(): Optional[Integer] = pos.line()
@ -482,11 +492,12 @@ object Defaults extends BuildCommon {
override def pointerSpace(): Optional[String] = pos.pointerSpace() override def pointerSpace(): Optional[String] = pos.pointerSpace()
override def sourcePath(): Optional[String] = newPath override def sourcePath(): Optional[String] = Optional.of(path)
override def sourceFile(): Optional[File] = pos.sourceFile() override def sourceFile(): Optional[File] = pos.sourceFile()
} }
) }
.orElse(Some(pos))
} }
// csrCacheDirectory is scoped to ThisBuild to allow customization. // csrCacheDirectory is scoped to ThisBuild to allow customization.