Skip to content

Commit

Permalink
include the build target in the report name
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 5, 2023
1 parent 3a2b778 commit b778bc7
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 49 deletions.
24 changes: 13 additions & 11 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,12 @@ class Compilers(
): Option[PresentationCompiler] = {
val pc = JavaPresentationCompiler()
Some(
configure(pc, search).newInstance(
targetUri,
classpath.toAbsoluteClasspath.map(_.toNIO).toSeq.asJava,
log.asJava,
)
configure(pc, search)
.newInstance(
targetUri,
classpath.toAbsoluteClasspath.map(_.toNIO).toSeq.asJava,
log.asJava,
)
)
}

Expand Down Expand Up @@ -1097,7 +1098,7 @@ class Compilers(
classpath,
search,
target.scalac.getTarget.getUri,
)
).withBuildTargetName(target.displayName)
}

def newCompiler(
Expand All @@ -1116,11 +1117,12 @@ class Compilers(
}

val filteredOptions = plugins.filterSupportedOptions(options)
configure(pc, search).newInstance(
name,
classpath.asJava,
(log ++ filteredOptions).asJava,
)
configure(pc, search)
.newInstance(
name,
classpath.asJava,
(log ++ filteredOptions).asJava,
)
}

private def toDebugCompletionType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ class MetalsLspService(

implicit val reports: StdReportContext = new StdReportContext(
folder.toNIO,
_.flatMap { uri =>
for {
filePath <- uri.toAbsolutePathSafe
buildTargetId <- buildTargets.inverseSources(filePath)
name <- buildTargets
.scalaTarget(buildTargetId)
.map(_.displayName)
.orElse(buildTargets.javaTarget(buildTargetId).map(_.displayName))
} yield name
},
ReportLevel.fromString(MetalsServerConfig.default.loglevel),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,6 @@ final class Doctor(
private def getErrorReports(): List[ErrorReportInfo] = {
def decode(text: String) =
text.replace(StdReportContext.WORKSPACE_STR, workspace.toString())
def getBuildTarget(lines: List[String]) =
for {
filePath <- lines.collectFirst {
case line if line.startsWith("file:") =>
decode(line.trim()).toAbsolutePath
}
buildTargetId <- buildTargets.inverseSources(filePath)
name <- buildTargets
.scalaTarget(buildTargetId)
.map(_.displayName)
.orElse(buildTargets.javaTarget(buildTargetId).map(_.displayName))
} yield name
def getSummary(lines: List[String]) = {
val reversed = lines.reverse
val index = reversed.indexWhere(_.startsWith(Report.summaryTitle))
Expand All @@ -269,11 +257,12 @@ final class Doctor(
rc.getReports().map { case TimestampedFile(file, timestamp) =>
val optLines =
Try(Files.readAllLines(file.toPath).asScala.toList).toOption
val (name, buildTarget) = ReportFileName.getReportNameAndBuildTarget(file)
ErrorReportInfo(
ReportFileName.getReportName(file),
name,
timestamp,
file.toPath.toUri().toString(),
optLines.flatMap(getBuildTarget(_)),
buildTarget,
optLines.map(getSummary(_)).getOrElse(""),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class MetalsLanguageServer(
metalsService.underlying = service

folderPathsWithScala.foreach(folder =>
new StdReportContext(folder.toNIO).cleanUpOldReports()
new StdReportContext(folder.toNIO, _ => None).cleanUpOldReports()
)

service.initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class PresentationCompiler {
*
*/
public CompletableFuture<List<Node>> semanticTokens(VirtualFileParams params) {

return CompletableFuture.completedFuture(Collections.emptyList());
}

Expand Down Expand Up @@ -208,6 +208,13 @@ public PresentationCompiler withReportsLoggerLevel(String level) {
return this;
};

/**
* Set build target name.
*/
public PresentationCompiler withBuildTargetName(String buildTargetName) {
return this;
};

/**
* Provide a SymbolSearch to extract docstrings, java parameter names and Scala
* default parameter values.
Expand Down Expand Up @@ -252,7 +259,7 @@ public abstract PresentationCompiler withScheduledExecutorService(
* better-monadic-for.
*/
public abstract PresentationCompiler newInstance(String buildTargetIdentifier, List<Path> classpath,
List<String> options);
List<String> options);

// =============================
// Intentionally missing methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,32 @@ trait Reporter {
def deleteAll(): Unit
}

class StdReportContext(workspace: Path, level: ReportLevel = ReportLevel.Info)
extends ReportContext {
class StdReportContext(
workspace: Path,
resolveBuildTarget: Option[String] => Option[String],
level: ReportLevel = ReportLevel.Info
) extends ReportContext {
val reportsDir: Path = workspace.resolve(StdReportContext.reportsDir)

val unsanitized =
new StdReporter(
workspace,
StdReportContext.reportsDir.resolve("metals-full"),
resolveBuildTarget,
level
)
val incognito =
new StdReporter(
workspace,
StdReportContext.reportsDir.resolve("metals"),
resolveBuildTarget,
level
)
val bloop =
new StdReporter(
workspace,
StdReportContext.reportsDir.resolve("bloop"),
resolveBuildTarget,
level
)

Expand All @@ -71,8 +77,12 @@ class StdReportContext(workspace: Path, level: ReportLevel = ReportLevel.Info)
}
}

class StdReporter(workspace: Path, pathToReports: Path, level: ReportLevel)
extends Reporter {
class StdReporter(
workspace: Path,
pathToReports: Path,
resolveBuildTarget: Option[String] => Option[String],
level: ReportLevel
) extends Reporter {
private lazy val maybeReportsDir: Path = workspace.resolve(pathToReports)
private lazy val reportsDir = maybeReportsDir.createDirectories()
private val limitedFilesManager =
Expand Down Expand Up @@ -114,7 +124,7 @@ class StdReporter(workspace: Path, pathToReports: Path, level: ReportLevel)
val sanitizedId = report.id.map(sanitize)
if (sanitizedId.isDefined && reported.contains(sanitizedId.get)) None
else {
val path = reportPath(report.name)
val path = reportPath(report)
path.getParent.createDirectories()
sanitizedId.foreach(reported += _)
path.writeText(sanitize(report.fullText(withIdAndSummary = true)))
Expand All @@ -130,10 +140,12 @@ class StdReporter(workspace: Path, pathToReports: Path, level: ReportLevel)
.getOrElse(textAfterWokspaceReplace)
}

private def reportPath(name: String): Path = {
private def reportPath(report: Report): Path = {
val date = TimeFormatter.getDate()
val time = TimeFormatter.getTime()
val filename = s"r_${name}_${time}.md"
val buildTargetPart =
resolveBuildTarget(report.path).map(":" ++ _).getOrElse("")
val filename = s"r_${report.name}${buildTargetPart}_${time}.md"
reportsDir.resolve(date).resolve(filename)
}

Expand Down Expand Up @@ -278,11 +290,15 @@ object ReportLevel {
}

object ReportFileName {
val pattern: Regex = "r_(.*)_".r

def getReportName(file: File): String =
pattern
.findPrefixMatchOf(file.getName())
.map(_.group(1))
.getOrElse(file.getName())
val pattern: Regex = "r_([^:]*)(:.*)?_".r

def getReportNameAndBuildTarget(file: File): (String, Option[String]) =
pattern.findPrefixMatchOf(file.getName()) match {
case None => (file.getName(), None)
case Some(foundMatch) =>
(
foundMatch.group(1),
Option(foundMatch.group(2)).map(_.stripPrefix(":"))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import org.eclipse.lsp4j.TextEdit

case class ScalaPresentationCompiler(
buildTargetIdentifier: String = "",
buildTargetName: Option[String] = None,
classpath: Seq[Path] = Nil,
options: List[String] = Nil,
search: SymbolSearch = EmptySymbolSearch,
Expand All @@ -69,9 +70,14 @@ case class ScalaPresentationCompiler(

implicit val reportContex: ReportContext =
folderPath
.map(new StdReportContext(_, reportsLevel))
.map(new StdReportContext(_, _ => buildTargetName, reportsLevel))
.getOrElse(EmptyReportContext)

override def withBuildTargetName(
buildTargetName: String
): ScalaPresentationCompiler =
copy(buildTargetName = Some(buildTargetName))

override def withReportsLoggerLevel(level: String): PresentationCompiler =
copy(reportsLevel = ReportLevel.fromString(level))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.eclipse.{lsp4j as l}

case class ScalaPresentationCompiler(
buildTargetIdentifier: String = "",
buildTargetName: Option[String] = None,
classpath: Seq[Path] = Nil,
options: List[String] = Nil,
search: SymbolSearch = EmptySymbolSearch,
Expand All @@ -41,17 +42,20 @@ case class ScalaPresentationCompiler(
reportsLevel: ReportLevel = ReportLevel.Info,
) extends PresentationCompiler:

def this() = this("", Nil, Nil)
def this() = this("", None, Nil, Nil)

val scalaVersion = BuildInfo.scalaCompilerVersion

private val forbiddenOptions = Set("-print-lines", "-print-tasty")
private val forbiddenDoubleOptions = Set("-release")
given ReportContext =
folderPath
.map(StdReportContext(_, reportsLevel))
.map(StdReportContext(_, _ => buildTargetName, reportsLevel))
.getOrElse(EmptyReportContext)

override def withBuildTargetName(buildTargetName: String) =
copy(buildTargetName = Some(buildTargetName))

override def withReportsLoggerLevel(level: String): PresentationCompiler =
copy(reportsLevel = ReportLevel.fromString(level))

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/src/main/scala/tests/TestingServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ final case class TestingServer(
lazy val fullServer = languageServer.getOldMetalsLanguageServer
def server = fullServer.folderServices.head

implicit val reports: ReportContext = new StdReportContext(workspace.toNIO)
implicit val reports: ReportContext =
new StdReportContext(workspace.toNIO, _ => None)

private lazy val trees = new Trees(
buffers,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/src/main/scala/tests/TreeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ object TreeUtils {
() => UserConfiguration(fallbackScalaVersion = scalaVersion),
buildTargets,
)
implicit val reports = new StdReportContext(Paths.get(".").toAbsolutePath)
implicit val reports =
new StdReportContext(Paths.get(".").toAbsolutePath, _ => None)
val trees =
new Trees(buffers, selector)
(buffers, trees)
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/src/test/scala/tests/ReportsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import scala.meta.io.AbsolutePath

class ReportsSuite extends BaseSuite {
val workspace: AbsolutePath = AbsolutePath(Paths.get("."))
val reportsProvider = new StdReportContext(workspace.toNIO)
val reportsProvider =
new StdReportContext(workspace.toNIO, _ => Some("buildTarget"))
val folderReportsZippper: FolderReportsZippper =
FolderReportsZippper(exampleBuildTargetsInfo, reportsProvider)

Expand Down Expand Up @@ -107,7 +108,8 @@ class ReportsSuite extends BaseSuite {
Report("test_error_again", None, exampleText(), "Test error", testId)
)
assert(none1.isEmpty)
val newReportsProvider = new StdReportContext(workspace.toNIO)
val newReportsProvider =
new StdReportContext(workspace.toNIO, _ => Some("buildTarget"))
val none2 = newReportsProvider.incognito.create(
Report("test_error_again", None, exampleText(), "Test error", testId)
)
Expand Down

0 comments on commit b778bc7

Please sign in to comment.