Skip to content

Fix #7852: avoid reading stale .tasty files from jars #7918

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 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 21 additions & 14 deletions compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -780,22 +780,29 @@ class ClassfileParser(
Array.empty
case Some(jar: ZipArchive) => // We are in a jar
val cl = new URLClassLoader(Array(jar.jpath.toUri.toURL), /*parent =*/ null)
val path = classfile.path.stripSuffix(".class") + ".tasty"
val stream = cl.getResourceAsStream(path)
if (stream != null) {
val tastyOutStream = new ByteArrayOutputStream()
val buffer = new Array[Byte](1024)
var read = stream.read(buffer, 0, buffer.length)
while (read != -1) {
tastyOutStream.write(buffer, 0, read)
read = stream.read(buffer, 0, buffer.length)
try {
val path = classfile.path.stripSuffix(".class") + ".tasty"
val stream = cl.getResourceAsStream(path)
if (stream != null) {
val tastyOutStream = new ByteArrayOutputStream()
val buffer = new Array[Byte](1024)
var read = stream.read(buffer, 0, buffer.length)
while (read != -1) {
tastyOutStream.write(buffer, 0, read)
read = stream.read(buffer, 0, buffer.length)
}
tastyOutStream.flush()
tastyOutStream.toByteArray
}
else {
ctx.error(s"Could not find $path in $jar")
Array.empty
}
tastyOutStream.flush()
tastyOutStream.toByteArray
}
else {
ctx.error(s"Could not find $path in $jar")
Array.empty
finally {
// If we don't close the classloader, spooky things happen (see
// scripted test source-dependencies/export-jars2).
cl.close()
}
case _ =>
val plainFile = new PlainFile(io.File(classfile.jpath).changeExtension("tasty"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A
4 changes: 4 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/export-jars2/b/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class B {
val x = new A
}

10 changes: 10 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/export-jars2/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
lazy val a = Project("a", file("a"))
.settings(
exportJars := true
)

lazy val b = Project("b", file("b"))
.dependsOn(a)
.settings(
exportJars := true
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

class A
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

class B {
val x = new A
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sbt._
import Keys._

object DottyInjectedPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements

override val projectSettings = Seq(
scalaVersion := sys.props("plugin.scalaVersion"),
scalacOptions += "-language:Scala2Compat"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
7 changes: 7 additions & 0 deletions sbt-dotty/sbt-test/source-dependencies/export-jars2/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
> compile
$ copy-file changes/A1.scala a/A.scala
$ copy-file changes/B1.scala b/B.scala
# This used to fail with "Tasty UUID (...) file did not correspond the tasty UUID (...) declared in the classfile"
# because we were somehow reading the .tasty file from the previous compilation run, even though it does not exist
# on disk anymore (#7852)
> compile