@@ -12,7 +12,7 @@ import java.math.BigInteger
1212import java .security .MessageDigest
1313import java .util .jar .JarFile
1414
15- import cats .effect .{ Concurrent , ConcurrentEffect , Timer }
15+ import cats .effect ._
1616import cats .implicits ._
1717import coursier ._
1818import coursier .cache .{ArtifactError , FileCache }
@@ -22,8 +22,8 @@ import org.scalaexercises.evaluator.{Dependency => EvaluatorDependency}
2222
2323import scala .concurrent .duration ._
2424import scala .language .reflectiveCalls
25- import scala .reflect .internal .util .{AbstractFileClassLoader , BatchSourceFile , Position }
2625import scala .reflect .internal .util .ScalaClassLoader .URLClassLoader
26+ import scala .reflect .internal .util .{AbstractFileClassLoader , BatchSourceFile , Position }
2727import scala .tools .nsc .io .{AbstractFile , VirtualDirectory }
2828import scala .tools .nsc .reporters ._
2929import scala .tools .nsc .{Global , Settings }
@@ -462,28 +462,36 @@ class ${className} extends (() => Any) with java.io.Serializable {
462462 val classPath = getClassPath(this .getClass.getClassLoader)
463463 val currentClassPath = classPath.head
464464
465+ def checkCurrentClassPath : List [String ] = currentClassPath match {
466+ case List (jarFile) if jarFile.endsWith(" .jar" ) =>
467+ val relativeRoot = new File (jarFile).getParentFile
468+ val jarResource : Resource [IO , JarFile ] =
469+ Resource .make(IO (new JarFile (jarFile)))(jar => IO (jar.close()))
470+
471+ val nestedClassPath : IO [String ] =
472+ jarResource
473+ .use(jar => IO (jar.getManifest.getMainAttributes.getValue(" Class-Path" )))
474+ .handleError {
475+ case scala.util.control.NonFatal (e) =>
476+ throw new CompilerException (List (List (e.getMessage)))
477+ }
478+
479+ nestedClassPath.map {
480+ case ncp if ncp eq null => Nil
481+ case ncp =>
482+ ncp
483+ .split(" " )
484+ .map { f =>
485+ new File (relativeRoot, f).getAbsolutePath
486+ }
487+ .toList
488+
489+ }.unsafeRunSync
490+ case _ => Nil
491+ }
492+
465493 // if there's just one thing in the classpath, and it's a jar, assume an executable jar.
466- currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath(0 )
467- .endsWith(" .jar" )) {
468- val jarFile = currentClassPath(0 )
469- val relativeRoot =
470- new File (jarFile).getParentFile()
471- val nestedClassPath =
472- new JarFile (jarFile).getManifest.getMainAttributes
473- .getValue(" Class-Path" )
474- if (nestedClassPath eq null ) {
475- Nil
476- } else {
477- nestedClassPath
478- .split(" " )
479- .map { f =>
480- new File (relativeRoot, f).getAbsolutePath
481- }
482- .toList
483- }
484- } else {
485- Nil
486- }) ::: classPath.tail.flatten
494+ currentClassPath ::: checkCurrentClassPath ::: classPath.tail.flatten
487495 }
488496
489497 lazy val compilerOutputDir = target match {
0 commit comments