From 42d72a66f164f35874449b0e03ee7adfa5cc3297 Mon Sep 17 00:00:00 2001 From: jilen Date: Fri, 24 Jun 2022 13:43:52 +0800 Subject: [PATCH] Fix zinc binary dependencies (#1904) Fix https://github.com/com-lihaoyi/mill/issues/1901 Zinc uses a `MappedFileConverter`, it will converter `jdk` internal classes like `/module/java.base/java.lang.String` associated with a dummpy `rt.jar`. https://github.com/sbt/zinc/blob/57d03412abe3810be5762a8c8e8c55cbf622ed03/internal/zinc-core/src/main/scala/sbt/internal/inc/MappedVirtualFile.scala#L56 ```scala def toVirtualFile(path: Path): VirtualFile = { rootPaths2.find { case (_, rootPath) => path.startsWith(rootPath) } match { case Some((key, rootPath)) => MappedVirtualFile(s"$${$key}/${rootPath.relativize(path)}".replace('\\', '/'), rootPaths) case _ => def isCtSym = path.getFileSystem .provider() .getScheme == "jar" && path.getFileSystem.toString.endsWith("ct.sym") def isJrt = path.getFileSystem.provider().getScheme == "jrt" if (isJrt || path.getFileName.toString == "rt.jar" || isCtSym) DummyVirtualFile("rt.jar", path) else if (allowMachinePath) MappedVirtualFile(s"$path".replace('\\', '/'), rootPaths) else sys.error(s"$path cannot be mapped using the root paths $rootPaths") } } ``` And later the `Incremental` will exclude such binary dependencies. https://github.com/sbt/zinc/blob/57d03412abe3810be5762a8c8e8c55cbf622ed03/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala#L783 ```scala // dependency is some other binary on the classpath. // exclude dependency tracking with rt.jar, for example java.lang.String -> rt.jar. if (!vf.id.endsWith("rt.jar")) { externalLibraryDependency( vf, onBinaryName, sourceFile, context ) } ``` This commit implements the exclusion, when reading the `Incremental` analysis file. Pull request: https://github.com/com-lihaoyi/mill/pull/1904 --- .../worker/src/mill/scalalib/worker/MockedLookup.scala | 7 +++++-- .../worker/src/mill/scalalib/worker/ZincWorkerImpl.scala | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/scalalib/worker/src/mill/scalalib/worker/MockedLookup.scala b/scalalib/worker/src/mill/scalalib/worker/MockedLookup.scala index 6990a6738e6..26dffbf1db3 100644 --- a/scalalib/worker/src/mill/scalalib/worker/MockedLookup.scala +++ b/scalalib/worker/src/mill/scalalib/worker/MockedLookup.scala @@ -13,6 +13,9 @@ case class MockedLookup(am: VirtualFile => Optional[CompileAnalysis]) override def analysis(classpathEntry: VirtualFile): Optional[CompileAnalysis] = am(classpathEntry) - override def definesClass(classpathEntry: VirtualFile): DefinesClass = - Locate.definesClass(classpathEntry) + override def definesClass(classpathEntry: VirtualFile): DefinesClass = { + if (classpathEntry.name.toString != "rt.jar") + Locate.definesClass(classpathEntry) + else (_: String) => false + } } diff --git a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala index 4aff9d9a04a..05b5eb9ae77 100644 --- a/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala +++ b/scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala @@ -474,7 +474,8 @@ class ZincWorkerImpl( val store = FileAnalysisStore.binary(zincFile.toIO) - val converter = PlainVirtualFileConverter.converter + // Fix jdk classes marked as binary dependencies, see https://github.com/com-lihaoyi/mill/pull/1904 + val converter = MappedFileConverter.empty val classpath = (compileClasspath.iterator ++ Some(classesDir)) .map(path => converter.toVirtualFile(path.toNIO)) .toArray