From 7696f6ca622f9416e4a1c650d8035aa5ac166f1f Mon Sep 17 00:00:00 2001 From: Friendseeker <66892505+Friendseeker@users.noreply.github.com> Date: Fri, 27 Sep 2024 11:55:44 -0700 Subject: [PATCH] Generate AbstractZincFile during -sourcepath workflow In certain codepath, unit.source.file is a PlainFile, which causes a MatchError. This PR converts PlainFile to AbstractZincFile, avoiding the MatchError. --- .../src/main/scala/xsbt/CallbackGlobal.scala | 12 +++++++++++- .../src/main/java/xsbti/AnalysisCallback3.java | 5 +++++ .../main/scala/sbt/internal/inc/Incremental.scala | 2 ++ .../src/test/scala/sbt/internal/inc/IncHandler.scala | 5 ++++- .../src/main/scala/xsbti/TestCallback.scala | 4 ++++ .../sourcepath-virtualfile/changes/B.scala | 3 +++ .../sourcepath-virtualfile/incOptions.properties | 1 + .../sourcepath-virtualfile/src/main/scala/A.scala | 1 + .../sourcepath-virtualfile/src/main/scala/B.scala | 3 +++ .../source-dependencies/sourcepath-virtualfile/test | 6 ++++++ 10 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/changes/B.scala create mode 100644 zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/incOptions.properties create mode 100644 zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/A.scala create mode 100644 zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/B.scala create mode 100644 zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/test diff --git a/internal/compiler-bridge/src/main/scala/xsbt/CallbackGlobal.scala b/internal/compiler-bridge/src/main/scala/xsbt/CallbackGlobal.scala index 0b3663ece0..4c6f53d9d5 100644 --- a/internal/compiler-bridge/src/main/scala/xsbt/CallbackGlobal.scala +++ b/internal/compiler-bridge/src/main/scala/xsbt/CallbackGlobal.scala @@ -11,7 +11,7 @@ package xsbt -import xsbti.{ AnalysisCallback, Severity } +import xsbti.{ AnalysisCallback, AnalysisCallback3, Severity } import xsbti.compile._ import scala.tools.nsc._ @@ -20,6 +20,7 @@ import java.nio.file.{ Files, Path } import scala.reflect.io.PlainFile import scala.reflect.ReflectAccess._ +import scala.reflect.internal.util.BatchSourceFile /** Defines the interface of the incremental compiler hiding implementation details. */ sealed abstract class CallbackGlobal( @@ -78,6 +79,15 @@ sealed class ZincCompiler(settings: Settings, dreporter: DelegatingReporter, out extends CallbackGlobal(settings, dreporter, output) with ZincGlobalCompat { + override def getSourceFile(f: AbstractFile): BatchSourceFile = { + val file = (f, callback) match { + case (plainFile: PlainFile, callback3: AnalysisCallback3) => + AbstractZincFile(callback3.toVirtualFile(plainFile.file.toPath)) + case _ => f + } + super.getSourceFile(file) + } + final class ZincRun(compileProgress: CompileProgress) extends Run { override def informUnitStarting(phase: Phase, unit: CompilationUnit): Unit = { compileProgress.startUnit(phase.name, unit.source.path) diff --git a/internal/compiler-interface/src/main/java/xsbti/AnalysisCallback3.java b/internal/compiler-interface/src/main/java/xsbti/AnalysisCallback3.java index 649ad752eb..b91afd0284 100644 --- a/internal/compiler-interface/src/main/java/xsbti/AnalysisCallback3.java +++ b/internal/compiler-interface/src/main/java/xsbti/AnalysisCallback3.java @@ -10,12 +10,17 @@ */ package xsbti; + import xsbti.compile.analysis.ReadSourceInfos; +import java.nio.file.Path; + /** * Extension to {@link AnalysisCallback2}. * Similar to {@link AnalysisCallback2}, it serves as compatibility layer for Scala compilers. */ public interface AnalysisCallback3 extends AnalysisCallback2 { + VirtualFile toVirtualFile(Path path); + ReadSourceInfos getSourceInfos(); } diff --git a/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala b/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala index 5035187c53..72ec426a91 100644 --- a/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala +++ b/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala @@ -674,6 +674,8 @@ private final class AnalysisCallback( () } + override def toVirtualFile(path: Path): VirtualFile = converter.toVirtualFile(path) + override def problem2( category: String, pos: Position, diff --git a/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala b/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala index 7a76d0cf8c..4d9cba150f 100644 --- a/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala +++ b/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala @@ -795,7 +795,10 @@ case class ProjectStructure( } val scalacOptions: List[String] = Option(map.get("scalac.options")).toList - .flatMap(_.toString.split(" +").toList) ++ + .flatMap(_.toString.split(" +").toList.map(_.replace( + "[basedir]", + baseDirectory.toAbsolutePath.toString + ))) ++ // for now assume export pipelining for all pipelining subprojects (if (incOptions.pipelining) List("-Ypickle-java", "-Ypickle-write", earlyOutput.toString) else Nil) diff --git a/internal/zinc-testing/src/main/scala/xsbti/TestCallback.scala b/internal/zinc-testing/src/main/scala/xsbti/TestCallback.scala index 9426b8c6bc..484407c2ff 100644 --- a/internal/zinc-testing/src/main/scala/xsbti/TestCallback.scala +++ b/internal/zinc-testing/src/main/scala/xsbti/TestCallback.scala @@ -154,6 +154,10 @@ class TestCallback extends AnalysisCallback3 { override def getPickleJarPair = Optional.empty() + override def toVirtualFile(path: Path): VirtualFile = { + throw new UnsupportedOperationException("This method should not be called in tests") + } + override def getSourceInfos: ReadSourceInfos = new TestSourceInfos } diff --git a/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/changes/B.scala b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/changes/B.scala new file mode 100644 index 0000000000..3826c673f1 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/changes/B.scala @@ -0,0 +1,3 @@ +class B { + def f: A = new A() +} diff --git a/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/incOptions.properties b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/incOptions.properties new file mode 100644 index 0000000000..6b07ac31db --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/incOptions.properties @@ -0,0 +1 @@ +scalac.options = -sourcepath [basedir]/src/main/scala diff --git a/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/A.scala b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/A.scala new file mode 100644 index 0000000000..83d15dc739 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/A.scala @@ -0,0 +1 @@ +class A diff --git a/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/B.scala b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/B.scala new file mode 100644 index 0000000000..a97bc1e396 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/src/main/scala/B.scala @@ -0,0 +1,3 @@ +class B { + def f = new A() +} diff --git a/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/test b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/test new file mode 100644 index 0000000000..45605ea4a8 --- /dev/null +++ b/zinc/src/sbt-test/source-dependencies/sourcepath-virtualfile/test @@ -0,0 +1,6 @@ +> compile + +$ copy-file changes/B.scala src/main/scala/B.scala +$ touch src/main/scala/A.scala + +> compile