Skip to content

Fix inspectTastyFilesInJar in TastyInspector #13254

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 3 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/fromtasty/TASTYRun.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fromtasty

import io.{JarArchive, AbstractFile, Path}
import core.Contexts._
import java.io.File

class TASTYRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
override def compile(files: List[AbstractFile]): Unit = {
Expand All @@ -17,9 +18,10 @@ class TASTYRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
val classNames = files.flatMap { file =>
file.extension match
case "jar" =>
JarArchive.open(Path(file.path), create = false).iterator()
.filter(e => e.extension == "tasty" && !fromTastyIgnoreList(e.name))
.map(e => e.name.stripSuffix(".tasty").replace("/", "."))
JarArchive.open(Path(file.path), create = false).allFileNames()
.map(_.stripPrefix(File.separator)) // change paths from absolute to relative
.filter(e => Path.extension(e) == "tasty" && !fromTastyIgnoreList(e))
.map(e => e.stripSuffix(".tasty").replace(File.separator, "."))
.toList
case "tasty" => TastyFileUtil.getClassName(file)
case _ =>
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/io/JarArchive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import scala.jdk.CollectionConverters._
*/
class JarArchive private (root: Directory) extends PlainDirectory(root) {
def close(): Unit = jpath.getFileSystem().close()
def allFileNames(): Iterator[String] =
java.nio.file.Files.walk(jpath).iterator().asScala.map(_.toString)
}

object JarArchive {
Expand Down
24 changes: 24 additions & 0 deletions sbt-test/sbt-dotty/tasty-inspector-jars/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
lazy val dottyVersion = sys.props("plugin.scalaVersion")

lazy val lib = project
.in(file("lib"))
.settings(
scalaVersion := dottyVersion
)

val jarDest = file("target") / "app.jar"

val runTest = Def.taskKey[Unit]("run tests")

lazy val inspector = project
.in(file("inspector"))
.settings(
scalaVersion := dottyVersion,
libraryDependencies += "org.scala-lang" %% "scala3-tasty-inspector" % scalaVersion.value,
runTest :=
Def.sequential(
Def.task(IO.copyFile((lib/Compile/packageBin).value, jarDest)),
(Compile/run).toTask(" " + jarDest.getAbsolutePath)
).value
)
.dependsOn(lib)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted.Quotes
import scala.quoted.quotes
import scala.tasty.inspector as ins

class MyInspector extends ins.Inspector:
def inspect(using Quotes)(tastys: List[ins.Tasty[quotes.type]]): Unit =
val sources = tastys.map(_.ast.pos.sourceFile.toString).toSet
val expectedSources = Set("lib/src/main/scala/toplevel.scala", "lib/src/main/scala/inpackage.scala")
assert(sources == expectedSources, s"expected $expectedSources tasty files but get: $sources")

@main def main(args: String*): Unit =
ins.TastyInspector.inspectTastyFilesInJar(args.head)(new MyInspector)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pkg

class A
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// NOTE: no package, this is top-level!

class A
1 change: 1 addition & 0 deletions sbt-test/sbt-dotty/tasty-inspector-jars/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> inspector/runTest