@@ -5,6 +5,11 @@ import java.net.URLClassLoader
55import dotty .tools .dotc
66import dotty .tools .dotc .core .Contexts ._
77
8+ import java .nio .file .Paths
9+ import java .io .File
10+ import java .io .File .{ pathSeparator => sep }
11+ import scala .annotation .tailrec
12+
813import scala .tasty .file .TastyConsumer
914
1015object ConsumeTasty {
@@ -18,23 +23,33 @@ object ConsumeTasty {
1823 new TastyFromClass (tastyConsumer)
1924 }
2025
21- val currentClasspath = getCurrentClasspath(getClass.getClassLoader)
22- import java .io .File .{ pathSeparator => sep }
26+ val currentClasspath = classpathFromClassloader(getClass.getClassLoader)
2327 val args = " -from-tasty" :: " -Yretain-trees" :: " -classpath" :: s " $classpath$sep$currentClasspath" :: classes
2428 (new Consume ).process(args.toArray)
2529 }
2630
27- private def getCurrentClasspath (cl : ClassLoader ): String = {
28- val classpath0 = System .getProperty(" java.class.path" )
29- cl match {
30- case cl : URLClassLoader =>
31- // Loads the classes loaded by this class loader
32- // When executing `run` or `test` in sbt the classpath is not in the property java.class.path
33- import java .nio .file .Paths
34- val newClasspath = cl.getURLs.map(url => Paths .get(url.toURI).toString)
35- newClasspath.mkString(" " , java.io.File .pathSeparator, if (classpath0 == " " ) " " else java.io.File .pathSeparator + classpath0)
36- case _ => classpath0
37- }
31+ /** Attempt to recreate a classpath from a classloader.
32+ *
33+ * BEWARE: with exotic enough classloaders, this may not work at all or do
34+ * the wrong thing.
35+ */
36+ private def classpathFromClassloader (cl : ClassLoader ): String = {
37+ @ tailrec
38+ def loop (cl : ClassLoader , suffixClasspath : String ): String =
39+ cl match {
40+ case cl : URLClassLoader =>
41+ val updatedClasspath = cl.getURLs
42+ .map(url => Paths .get(url.toURI).toAbsolutePath.toString)
43+ .mkString(
44+ " " ,
45+ File .pathSeparator,
46+ if (suffixClasspath.isEmpty) " " else File .pathSeparator + suffixClasspath
47+ )
48+ loop(cl.getParent, updatedClasspath)
49+ case _ =>
50+ suffixClasspath
51+ }
52+
53+ loop(cl, " " )
3854 }
39- }
40-
55+ }
0 commit comments