Skip to content

Commit caf498d

Browse files
committed
Fix classpath detection when using the REPL
This was broken after I stopped appending `System.getProperty("java.class.path")` to the constructed classpath. Fixed with a hack, but this is still better than looking at java.class.path.
1 parent a6e60d8 commit caf498d

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

staging/src/scala/quoted/staging/QuoteDriver.scala

+26-9
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,32 @@ private class QuoteDriver(appClassloader: ClassLoader) extends Driver {
7474
*/
7575
private def classpathFromClassloader(cl: ClassLoader): String = {
7676
val classpathBuff = List.newBuilder[String]
77-
def collectClassLoaderPaths(cl: ClassLoader): Unit = cl match {
78-
case cl: URLClassLoader =>
79-
collectClassLoaderPaths(cl.getParent)
80-
// Parent classloaders are searched before their child, so the part of
81-
// the classpath coming from the child is added at the _end_ of the
82-
// classpath.
83-
classpathBuff ++=
84-
cl.getURLs.iterator.map(url => Paths.get(url.toURI).toAbsolutePath.toString)
85-
case _ =>
77+
def collectClassLoaderPaths(cl: ClassLoader): Unit = {
78+
if (cl != null) {
79+
cl match {
80+
case cl: URLClassLoader =>
81+
// This is wrong if we're in a subclass of URLClassLoader
82+
// that filters loading classes from its parent ¯\_(ツ)_/¯
83+
collectClassLoaderPaths(cl.getParent)
84+
// Parent classloaders are searched before their child, so the part of
85+
// the classpath coming from the child is added at the _end_ of the
86+
// classpath.
87+
classpathBuff ++=
88+
cl.getURLs.iterator.map(url => Paths.get(url.toURI).toAbsolutePath.toString)
89+
case _ =>
90+
// HACK: We can't just collect the classpath from arbitrary parent
91+
// classloaders since the current classloader might intentionally
92+
// filter loading classes from its parent (for example
93+
// BootFilteredLoader in the sbt launcher does this and we really
94+
// don't want to include the scala-library that sbt depends on
95+
// here), but we do need to look at the parent of the REPL
96+
// classloader, so we special case it. We can't do this using a type
97+
// test since the REPL classloader class itself is normally loaded
98+
// with a different classloader.
99+
if (cl.getClass.getName == classOf[AbstractFileClassLoader].getName)
100+
collectClassLoaderPaths(cl.getParent)
101+
}
102+
}
86103
}
87104
collectClassLoaderPaths(cl)
88105
classpathBuff.result().mkString(java.io.File.pathSeparator)

0 commit comments

Comments
 (0)