Skip to content

Commit

Permalink
Migrate partially to bsp v2.0
Browse files Browse the repository at this point in the history
Start the migration to bsp 2.0. There's not an official release yet but
we're going to depend on a milestone to ease the migration of the
clients and their testing via Bloop.

The following migration only misses support for `test`, `run` and a few
other endpoints that we will implement in a future PR.
  • Loading branch information
jvican committed Nov 15, 2018
1 parent ae5e387 commit 2ba5537
Show file tree
Hide file tree
Showing 18 changed files with 701 additions and 213 deletions.
54 changes: 35 additions & 19 deletions backend/src/main/scala/bloop/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,43 @@ object Compiler {
// We don't need nanosecond granularity, we're happy with milliseconds
def elapsed: Long = ((System.nanoTime() - start).toDouble / 1e6).toLong

import ch.epfl.scala.bsp
import scala.util.{Success, Failure}
val logger = compileInputs.reporter.logger
val reporter = compileInputs.reporter
reporter.reportStartCompilation()

val previousAnalysis = InterfaceUtil.toOption(compileInputs.previousResult.analysis())
BloopZincCompiler.compile(inputs, compileInputs.mode, logger).materialize.map {
case Success(result) =>
// Report warnings that occurred in previous compilation cycles
previousAnalysis.foreach { previous =>
warningsFromPreviousRuns(previous, result.analysis()).foreach { p =>
// Note that buffered warnings are not added back to the current analysis on purpose
compileInputs.reporter.log(p)
BloopZincCompiler
.compile(inputs, compileInputs.mode, reporter.logger)
.materialize
.map {
case Success(result) =>
// Report warnings that occurred in previous compilation cycles
previousAnalysis.foreach { previous =>
warningsFromPreviousRuns(previous, result.analysis()).foreach { p =>
// Note that buffered warnings are not added back to the current analysis on purpose
compileInputs.reporter.log(p)
}
}
}

val res = PreviousResult.of(Optional.of(result.analysis()), Optional.of(result.setup()))
Result.Success(compileInputs.reporter, res, elapsed)
case Failure(f: StopPipelining) => Result.Blocked(f.failedProjectNames)
case Failure(f: xsbti.CompileFailed) => Result.Failed(f.problems().toList, None, elapsed)
case Failure(_: xsbti.CompileCancelled) => Result.Cancelled(elapsed)
case Failure(t: Throwable) =>
t.printStackTrace()
Result.Failed(Nil, Some(t), elapsed)
}

// Report end of compilation only after we have reported all warnings from previous runs
reporter.reportEndCompilation(bsp.StatusCode.Ok)
val res = PreviousResult.of(Optional.of(result.analysis()), Optional.of(result.setup()))
Result.Success(compileInputs.reporter, res, elapsed)
case Failure(_: xsbti.CompileCancelled) =>
reporter.reportEndCompilation(bsp.StatusCode.Cancelled)
Result.Cancelled(elapsed)
case Failure(cause) =>
val result = cause match {
case f: StopPipelining => Result.Blocked(f.failedProjectNames)
case f: xsbti.CompileFailed => Result.Failed(f.problems().toList, None, elapsed)
case t: Throwable =>
t.printStackTrace()
Result.Failed(Nil, Some(t), elapsed)
}

reporter.reportEndCompilation(bsp.StatusCode.Error)
result
}
}
}
16 changes: 14 additions & 2 deletions backend/src/main/scala/bloop/io/AbsolutePath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bloop.io

import java.io.File
import java.net.URI
import java.nio.file.{Files, Path, Paths => NioPaths}
import java.nio.file.{FileSystems, Files, Path, PathMatcher, Paths => NioPaths}

final class AbsolutePath private (val underlying: Path) extends AnyVal {
def syntax: String = toString
Expand All @@ -22,8 +22,17 @@ final class AbsolutePath private (val underlying: Path) extends AnyVal {
def isFile: Boolean = Files.isRegularFile(underlying)
def isDirectory: Boolean = Files.isDirectory(underlying)
def readAllBytes: Array[Byte] = Files.readAllBytes(underlying)
def toFile: File = underlying.toFile()
def toFile: File = underlying.toFile
def toBspUri: URI = underlying.toUri
def toBspSourceUri: URI = {
val uri = toBspUri
if (exists) uri
else {
if (AbsolutePath.sourceFileNameMatcher.matches(underlying.getFileName)) uri
// If path doesn't exist and its name doesn't look like a file, assume it's a dir
else new java.net.URI(uri.toString + "/")
}
}
}

object AbsolutePath {
Expand All @@ -38,4 +47,7 @@ object AbsolutePath {
// Necessary to test wrong paths in tests...
private[bloop] def completelyUnsafe(path: String): AbsolutePath =
new AbsolutePath(NioPaths.get(path))

private[io] val sourceFileNameMatcher: PathMatcher =
FileSystems.getDefault.getPathMatcher("glob:*.{scala, java}")
}
7 changes: 7 additions & 0 deletions backend/src/main/scala/bloop/reporter/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import bloop.logging.Logger
import xsbti.compile.CompileAnalysis
import xsbti.{Position, Severity}

import ch.epfl.scala.bsp

import scala.collection.mutable

/**
Expand Down Expand Up @@ -59,6 +61,9 @@ abstract class Reporter(

private def hasWarnings(problems: Seq[Problem]): Boolean =
problems.exists(_.severity == Severity.Warn)

def reportStartCompilation(): Unit
def reportEndCompilation(code: bsp.StatusCode): Unit
}

final class LogReporter(
Expand Down Expand Up @@ -89,6 +94,8 @@ final class LogReporter(
}
}

override def reportStartCompilation(): Unit = ()
override def reportEndCompilation(code: bsp.StatusCode): Unit = ()
}

object Reporter {
Expand Down
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ val backend = project
buildInfoKeys := BloopBackendInfoKeys,
buildInfoObject := "BloopScalaInfo",
libraryDependencies ++= List(
Dependencies.bsp,
Dependencies.zinc,
Dependencies.nailgun,
Dependencies.scalazCore,
Expand Down Expand Up @@ -125,7 +126,8 @@ lazy val frontend: Project = project
.enablePlugins(BuildInfoPlugin)
.settings(testSettings, assemblySettings, releaseSettings, integrationTestSettings)
.settings(
name := s"bloop-frontend",
name := "bloop-frontend",
bloopName := "bloop",
mainClass in Compile in run := Some("bloop.Cli"),
buildInfoPackage := "bloop.internal.build",
buildInfoKeys := bloopInfoKeys(nativeBridge, jsBridge06, jsBridge10),
Expand All @@ -137,7 +139,6 @@ lazy val frontend: Project = project
parallelExecution in test := false,
libraryDependencies ++= List(
Dependencies.scalazCore,
Dependencies.bsp,
Dependencies.monix,
Dependencies.caseApp,
Dependencies.nuprocess,
Expand Down
Loading

0 comments on commit 2ba5537

Please sign in to comment.