Skip to content

[sbt-dotty] Use sbt loader as parent of scala instance loader #10541

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ object Build {
scalaLibrary,
dottyLibrary,
dottyCompiler,
allJars
allJars,
appConfiguration.value
)
},
// sbt-dotty defines `scalaInstance in doc` so we need to override it manually
Expand Down
122 changes: 0 additions & 122 deletions sbt-bridge/src/xsbt/CompilerClassLoader.java

This file was deleted.

9 changes: 1 addition & 8 deletions sbt-bridge/src/xsbt/CompilerInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,9 @@

public final class CompilerInterface {
public CachedCompiler newCompiler(String[] options, Output output, Logger initialLog, Reporter initialDelegate) {
// The classloader that sbt uses to load the compiler bridge is broken
// (see CompilerClassLoader#fixBridgeLoader for details). To workaround
// this we construct our own ClassLoader and then run the following code
// with it:
// new CachedCompilerImpl(options, output)

try {
ClassLoader bridgeLoader = this.getClass().getClassLoader();
ClassLoader fixedLoader = CompilerClassLoader.fixBridgeLoader(bridgeLoader);
Class<?> cciClass = fixedLoader.loadClass("xsbt.CachedCompilerImpl");
Class<?> cciClass = bridgeLoader.loadClass("xsbt.CachedCompilerImpl");
return (CachedCompiler) cciClass.getConstructors()[0].newInstance(options, output);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
Expand Down
47 changes: 43 additions & 4 deletions sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import sbt.librarymanagement.{
VersionNumber
}
import sbt.internal.inc.ScalaInstance
import sbt.internal.inc.classpath.ClassLoaderCache
import xsbti.compile._
import xsbti.AppConfiguration
import java.net.URLClassLoader
import java.util.Optional
import java.util.{Enumeration, Collections}
import java.net.URL
import scala.util.Properties.isJavaAtLeast


object DottyPlugin extends AutoPlugin {
object autoImport {
val isDotty = settingKey[Boolean]("Is this project compiled with Dotty?")
Expand Down Expand Up @@ -521,15 +526,34 @@ object DottyPlugin extends AutoPlugin {
scalaLibraryJar,
dottyLibraryJar,
compilerJar,
allJars
allJars,
appConfiguration.value
)
}

// Adapted from private mkScalaInstance in sbt
def makeScalaInstance(
state: State, dottyVersion: String, scalaLibrary: File, dottyLibrary: File, compiler: File, all: Seq[File]
state: State, dottyVersion: String, scalaLibrary: File, dottyLibrary: File, compiler: File, all: Seq[File], appConfiguration: AppConfiguration
): ScalaInstance = {
val libraryLoader = state.classLoaderCache(List(dottyLibrary, scalaLibrary))
/**
* The compiler bridge must load the xsbti classes from the sbt
* classloader, and similarly the Scala repl must load the sbt provided
* jline terminal. To do so we add the `appConfiguration` loader in
* the parent hierarchy of the scala 3 instance loader.
*
* The [[TopClassLoader]] ensures that the xsbti and jline classes
* only are loaded from the sbt loader. That is necessary because
* the sbt class loader contains the Scala 2.12 library and compiler
* bridge.
*/
val topLoader = new TopClassLoader(appConfiguration.provider.loader)

val libraryJars = Array(dottyLibrary, scalaLibrary)
val libraryLoader = state.classLoaderCache.cachedCustomClassloader(
libraryJars.toList,
() => new URLClassLoader(libraryJars.map(_.toURI.toURL), topLoader)
)

class DottyLoader
extends URLClassLoader(all.map(_.toURI.toURL).toArray, libraryLoader)
val fullLoader = state.classLoaderCache.cachedCustomClassloader(
Expand All @@ -540,10 +564,25 @@ object DottyPlugin extends AutoPlugin {
dottyVersion,
fullLoader,
libraryLoader,
Array(dottyLibrary, scalaLibrary),
libraryJars,
compiler,
all.toArray,
None)
}
}

private class TopClassLoader(sbtLoader: ClassLoader) extends ClassLoader(null) {
private val sharedPrefixes = List(
"xsbti.",
"org.jline."
)

override protected def loadClass(name: String, resolve: Boolean): Class[_] = {
if (sharedPrefixes.exists(name.startsWith(_))) {
val c = sbtLoader.loadClass(name)
if (resolve) resolveClass(c)
c
}
else super.loadClass(name, resolve)
}
}