From a03f795a4d1a89c5bd324b2c923be12940789782 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Tue, 10 Oct 2017 11:39:01 +0200 Subject: [PATCH 1/2] Fix #3279 When using the repl through "sbt console", we used to use the class loader sbt give us. But we need a class loader that has the output directory of the compiler on the classpath --- compiler/src/dotty/tools/repl/Rendering.scala | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/compiler/src/dotty/tools/repl/Rendering.scala b/compiler/src/dotty/tools/repl/Rendering.scala index 1f7167845b4e..15125320ef1e 100644 --- a/compiler/src/dotty/tools/repl/Rendering.scala +++ b/compiler/src/dotty/tools/repl/Rendering.scala @@ -21,24 +21,24 @@ import dotc.core.StdNames.str * `Rendering` is no longer valid. */ private[repl] class Rendering(compiler: ReplCompiler, - private var currentClassLoader: Option[ClassLoader] = None) { + parentClassLoader: Option[ClassLoader] = None) { - private def classLoader()(implicit ctx: Context) = - currentClassLoader.getOrElse { - import java.net.{URL, URLClassLoader} + private[this] var myClassLoader: ClassLoader = _ - /** the compiler's classpath, as URL's */ - val compilerClasspath: Seq[URL] = ctx.platform.classPath(ctx).asURLs + /** Class loader used to load compiled code */ + private[this] def classLoader()(implicit ctx: Context) = + if (myClassLoader != null) myClassLoader + else { + val parent = parentClassLoader.getOrElse { + // the compiler's classpath, as URL's + val compilerClasspath = ctx.platform.classPath(ctx).asURLs + new java.net.URLClassLoader(compilerClasspath.toArray, classOf[ReplDriver].getClassLoader) + } - def parent = new URLClassLoader(compilerClasspath.toArray, - classOf[ReplDriver].getClassLoader) - - val newClsLoader = new AbstractFileClassLoader(compiler.directory, - currentClassLoader.getOrElse(parent)) - - Thread.currentThread.setContextClassLoader(newClsLoader) - currentClassLoader = Some(newClsLoader) - newClsLoader + myClassLoader = new AbstractFileClassLoader(compiler.directory, parent) + // Set the current Java "context" class loader to this rendering class loader + Thread.currentThread.setContextClassLoader(myClassLoader) + myClassLoader } /** Load the value of the symbol using reflection From 061fdb1a57a6543412b404ff77527bcccc2ee39e Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Tue, 10 Oct 2017 13:18:31 +0200 Subject: [PATCH 2/2] Add test case We need to support "initialCommands" in the repl, otherwise this tests nothing... --- sbt-dotty/sbt-test/sbt-dotty/example-project/test | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sbt-dotty/sbt-test/sbt-dotty/example-project/test b/sbt-dotty/sbt-test/sbt-dotty/example-project/test index 62ea636c177f..885d666a4c01 100644 --- a/sbt-dotty/sbt-test/sbt-dotty/example-project/test +++ b/sbt-dotty/sbt-test/sbt-dotty/example-project/test @@ -1 +1,3 @@ > run +> 'set initialCommands := "1 + 1" ' +> console