Merge pull request #6008 from adpi2/fake-positions

Fix #5994: sourceMappers handles fake positions
This commit is contained in:
eugene yokota 2020-10-26 12:53:43 -04:00 committed by GitHub
commit cf78d18d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 11 deletions

View File

@ -157,4 +157,4 @@ for:
test_script:
# The server tests often fail in CI when run together so just run a single test to ensure
# that the thin client works on windows
- sbt "-Dsbt.io.virtual=false" "scripted actions/* classloader-cache/* nio/* watch/*" "serverTestProj/testOnly testpkg.ClientTest"
- sbt "-Dsbt.io.virtual=false" "scripted actions/* reporter/source-mapper classloader-cache/* nio/* watch/*" "serverTestProj/testOnly testpkg.ClientTest"

View File

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

View File

@ -0,0 +1,46 @@
import java.util.Optional
import xsbti.Position
val assertAbsolutePathConversion = taskKey[Unit]("checks source mappers convert to absolute path")
val assertHandleFakePos = taskKey[Unit]("checks source mappers handle fake position")
assertAbsolutePathConversion := {
val converter = fileConverter.value
val source = (Compile/sources).value.head
val position = newPosition(converter.toVirtualFile(source.toPath).id, source)
val mappedPos = sourcePositionMappers.value
.foldLeft(Option(position)) {
case (pos, mapper) => pos.flatMap(mapper)
}
assert {
mappedPos.get.sourcePath.asScala.contains(source.getAbsolutePath)
}
}
assertHandleFakePos := {
val position = newPosition("<macro>", new File("<macro>"))
val mappedPos = sourcePositionMappers.value
.foldLeft(Option(position)) {
case (pos, mapper) => pos.flatMap(mapper)
}
assert {
mappedPos.get.sourcePath.asScala.get.contains("<macro>")
}
}
def newPosition(path: String, file: File): Position = new Position {
override def line(): Optional[Integer] = Optional.empty()
override def lineContent() = ""
override def offset(): Optional[Integer] = Optional.empty()
override def pointer(): Optional[Integer] = Optional.empty()
override def pointerSpace(): Optional[String] = Optional.empty()
override def sourcePath(): Optional[String] = Optional.of(path)
override def sourceFile(): Optional[File] = Optional.of(file)
}

View File

@ -0,0 +1,2 @@
> assertAbsolutePathConversion
> assertHandleFakePos