Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scalaInstanceFromBloop in boostrapped server #774

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/main/scala/bloop/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object Compiler {

object NotOk {
def unapply(result: Result): Option[Result] = result match {
case f @ (Failed(_, _, _) | Cancelled(_, _) | Blocked(_)) => Some(f)
case f @ (Failed(_, _, _) | Cancelled(_, _) | Blocked(_) | GlobalError(_)) => Some(f)
case _ => None
}
}
Expand Down
50 changes: 40 additions & 10 deletions backend/src/main/scala/bloop/ScalaInstance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bloop

import java.io.File
import java.net.URLClassLoader
import java.nio.file.{Files, Path, Paths}
import java.nio.file.{Files, Path, Paths, StandardCopyOption}
import java.nio.file.attribute.{BasicFileAttributes, FileTime}
import java.util.Properties

Expand Down Expand Up @@ -159,23 +159,53 @@ object ScalaInstance {
* domain.
*/
def scalaInstanceFromBloop(logger: Logger): Option[ScalaInstance] = {
def findLocationForClazz(clazz: Class[_]): Option[Path] = {
try Some(Paths.get(clazz.getProtectionDomain.getCodeSource.getLocation.toURI))
catch { case NonFatal(_) => None }
lazy val tempDirectory = Files.createTempDirectory("bloop-scala-instance")
implicit val filter = DebugFilter.Compilation
def findLocationForClazz(clazz: Class[_], jarName: String): Option[Path] = {
try {
val expectedPath = clazz.getProtectionDomain.getCodeSource.getLocation.toURI
logger.debug(s"${clazz} detected in ${expectedPath}")

try Some(Paths.get(expectedPath))
catch {
case t: java.nio.file.FileSystemNotFoundException =>
// When bloop is bootstrapped by coursier, jars are available from resources instead
logger.debug(
s"Load jar from resource because scheme '${expectedPath.getScheme}' has no file system provider"
)

val fromResourceStream =
clazz.getResourceAsStream("/" + expectedPath.getSchemeSpecificPart)
if (fromResourceStream == null) None
else {
val outPath = tempDirectory.resolve(jarName)
logger.debug(s"${clazz} detected in resource, dumping to ${outPath}...")
Files.copy(fromResourceStream, outPath)
Some(outPath)
}
}
} catch {
case NonFatal(t) =>
logger.debug("Unexpected error when creting Bloop's classloader")
logger.trace(t)
None
}
}

if (cachedBloopScalaInstance != null) {
cachedBloopScalaInstance
} else {
logger.debug("Creating a scala instance from Bloop's classloader...")
val instance = {
for {
scalaLibraryJar <- findLocationForClazz(scala.Predef.getClass)
scalaReflectJar <- findLocationForClazz(classOf[scala.reflect.api.Trees])
scalaCompilerJar <- findLocationForClazz(scala.tools.nsc.Main.getClass)
scalaXmlJar <- findLocationForClazz(classOf[scala.xml.Node])
jlineJar <- findLocationForClazz(classOf[jline.console.ConsoleReader])
libraryJar <- findLocationForClazz(scala.Predef.getClass, "scala-library.jar")
reflectJar <- findLocationForClazz(classOf[scala.reflect.api.Trees], "scala-reflect.jar")
compilerJar <- findLocationForClazz(scala.tools.nsc.Main.getClass, "scala-compiler.jar")
xmlJar <- findLocationForClazz(classOf[scala.xml.Node], "scala-xml.jar")
jlineJar <- findLocationForClazz(classOf[jline.console.ConsoleReader], "jline.jar")
} yield {
val jars = List(scalaLibraryJar, scalaReflectJar, scalaCompilerJar, scalaXmlJar, jlineJar)
logger.debug(s"Created Bloop scala instance for ${BloopScalaInfo.scalaVersion}")
val jars = List(libraryJar, reflectJar, compilerJar, xmlJar, jlineJar)
ScalaInstance(
BloopScalaInfo.scalaOrganization,
ScalacCompilerName,
Expand Down
1 change: 0 additions & 1 deletion frontend/src/test/scala/bloop/tasks/CompileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import bloop.cli.Commands
import bloop.config.Config
import bloop.engine.tasks.Tasks
import bloop.engine.{Feedback, Run, State}
import bloop.exec.JavaEnv
import bloop.logging.{Logger, RecordingLogger}
import bloop.tasks.TestUtil.{
RootProject,
Expand Down