@@ -5,6 +5,11 @@ import java.net.URLClassLoader
5
5
import dotty .tools .dotc
6
6
import dotty .tools .dotc .core .Contexts ._
7
7
8
+ import java .nio .file .Paths
9
+ import java .io .File
10
+ import java .io .File .{ pathSeparator => sep }
11
+ import scala .annotation .tailrec
12
+
8
13
import scala .tasty .file .TastyConsumer
9
14
10
15
object ConsumeTasty {
@@ -18,23 +23,33 @@ object ConsumeTasty {
18
23
new TastyFromClass (tastyConsumer)
19
24
}
20
25
21
- val currentClasspath = getCurrentClasspath(getClass.getClassLoader)
22
- import java .io .File .{ pathSeparator => sep }
26
+ val currentClasspath = classpathFromClassloader(getClass.getClassLoader)
23
27
val args = " -from-tasty" :: " -Yretain-trees" :: " -classpath" :: s " $classpath$sep$currentClasspath" :: classes
24
28
(new Consume ).process(args.toArray)
25
29
}
26
30
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, " " )
38
54
}
39
- }
40
-
55
+ }
0 commit comments