diff --git a/compiler/src/dotty/tools/dotc/fromtasty/TASTYRun.scala b/compiler/src/dotty/tools/dotc/fromtasty/TASTYRun.scala index 2d2d41ae50ed..1c8ba2359623 100644 --- a/compiler/src/dotty/tools/dotc/fromtasty/TASTYRun.scala +++ b/compiler/src/dotty/tools/dotc/fromtasty/TASTYRun.scala @@ -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 = { @@ -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 _ => diff --git a/compiler/src/dotty/tools/io/JarArchive.scala b/compiler/src/dotty/tools/io/JarArchive.scala index f35493299cd6..80af5ef390b5 100644 --- a/compiler/src/dotty/tools/io/JarArchive.scala +++ b/compiler/src/dotty/tools/io/JarArchive.scala @@ -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 { diff --git a/sbt-test/sbt-dotty/tasty-inspector-jars/build.sbt b/sbt-test/sbt-dotty/tasty-inspector-jars/build.sbt new file mode 100644 index 000000000000..59dd85290bf0 --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-jars/build.sbt @@ -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) diff --git a/sbt-test/sbt-dotty/tasty-inspector-jars/inspector/src/main/scala/main.scala b/sbt-test/sbt-dotty/tasty-inspector-jars/inspector/src/main/scala/main.scala new file mode 100644 index 000000000000..b27e9a59c66a --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-jars/inspector/src/main/scala/main.scala @@ -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) diff --git a/sbt-test/sbt-dotty/tasty-inspector-jars/lib/src/main/scala/inpackage.scala b/sbt-test/sbt-dotty/tasty-inspector-jars/lib/src/main/scala/inpackage.scala new file mode 100644 index 000000000000..82a15907dfad --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-jars/lib/src/main/scala/inpackage.scala @@ -0,0 +1,3 @@ +package pkg + +class A diff --git a/sbt-test/sbt-dotty/tasty-inspector-jars/lib/src/main/scala/toplevel.scala b/sbt-test/sbt-dotty/tasty-inspector-jars/lib/src/main/scala/toplevel.scala new file mode 100644 index 000000000000..337fa441878a --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-jars/lib/src/main/scala/toplevel.scala @@ -0,0 +1,3 @@ +// NOTE: no package, this is top-level! + +class A diff --git a/sbt-test/sbt-dotty/tasty-inspector-jars/test b/sbt-test/sbt-dotty/tasty-inspector-jars/test new file mode 100644 index 000000000000..8fa783e886e2 --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-jars/test @@ -0,0 +1 @@ +> inspector/runTest