Skip to content

Fixed classpath bug #8004

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

Merged
merged 1 commit into from
Jan 17, 2020
Merged
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
45 changes: 30 additions & 15 deletions compiler/src/dotty/tools/dotc/consumetasty/ConsumeTasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import java.net.URLClassLoader
import dotty.tools.dotc
import dotty.tools.dotc.core.Contexts._

import java.nio.file.Paths
import java.io.File
import java.io.File.{ pathSeparator => sep }
import scala.annotation.tailrec

import scala.tasty.file.TastyConsumer

object ConsumeTasty {
Expand All @@ -18,23 +23,33 @@ object ConsumeTasty {
new TastyFromClass(tastyConsumer)
}

val currentClasspath = getCurrentClasspath(getClass.getClassLoader)
import java.io.File.{ pathSeparator => sep }
val currentClasspath = classpathFromClassloader(getClass.getClassLoader)
val args = "-from-tasty" :: "-Yretain-trees" :: "-classpath" :: s"$classpath$sep$currentClasspath" :: classes
(new Consume).process(args.toArray)
}

private def getCurrentClasspath(cl: ClassLoader): String = {
val classpath0 = System.getProperty("java.class.path")
cl match {
case cl: URLClassLoader =>
// Loads the classes loaded by this class loader
// When executing `run` or `test` in sbt the classpath is not in the property java.class.path
import java.nio.file.Paths
val newClasspath = cl.getURLs.map(url => Paths.get(url.toURI).toString)
newClasspath.mkString("", java.io.File.pathSeparator, if (classpath0 == "") "" else java.io.File.pathSeparator + classpath0)
case _ => classpath0
}
/** Attempt to recreate a classpath from a classloader.
*
* BEWARE: with exotic enough classloaders, this may not work at all or do
* the wrong thing.
*/
private def classpathFromClassloader(cl: ClassLoader): String = {
@tailrec
def loop(cl: ClassLoader, suffixClasspath: String): String =
cl match {
case cl: URLClassLoader =>
val updatedClasspath = cl.getURLs
.map(url => Paths.get(url.toURI).toAbsolutePath.toString)
.mkString(
"",
File.pathSeparator,
if (suffixClasspath.isEmpty) "" else File.pathSeparator + suffixClasspath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current version in #7953 has been updated. You should take the current version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Nicolas. I'm not sure what you mean. I looked at #7953, which is now merged into master. ConsumeTasty.scala in master still looks like the old way of getting the class path, not @smarter's new way. What change is requested? Thx.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we take this as a sign to refactor into one method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I would leave it for a future PR. I have a bigger refactoring coming that will address that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #8020 for this refactoring.

)
loop(cl.getParent, updatedClasspath)
case _ =>
suffixClasspath
}

loop(cl, "")
}
}

}