@@ -18,7 +18,7 @@ import net.sourceforge.argparse4j.impl.Arguments as Arg
1818import net .sourceforge .argparse4j .inf .{ArgumentParserException , Namespace }
1919import sbt .internal .inc .classpath .ClassLoaderCache
2020import sbt .internal .inc .caching .ClasspathCache
21- import sbt .internal .inc .{Analysis , AnalyzingCompiler , CompileFailed , FilteredInfos , FilteredRelations , IncrementalCompilerImpl , Locate , PlainVirtualFile , PlainVirtualFileConverter , ZincUtil }
21+ import sbt .internal .inc .{Analysis , AnalyzingCompiler , CompileFailed , FilteredInfos , FilteredRelations , FilteredSetup , IncrementalCompilerImpl , Locate , PlainVirtualFile , PlainVirtualFileConverter , ZincUtil }
2222import scala .jdk .CollectionConverters .*
2323import scala .util .Try
2424import scala .util .control .NonFatal
@@ -212,17 +212,31 @@ object ZincRunner extends WorkerMain[ZincRunnerWorkerConfig] {
212212 isWorker,
213213 )
214214
215+ // Check if we should include the -sourceroot flag in the compiler options
216+ // We only include it for Scala 3 and later versions, as it is not supported
217+ // in Scala 2.x versions.
218+ // We include this so that the TASTy file generated by the Scala compiler
219+ // will be deterministic across machines and directories Bazel uses for
220+ // multiplexed sandbox execution.
221+ val shouldIncludeSourceRoot = ! scalaInstance.actualVersion.startsWith(" 0." ) &&
222+ scalaInstance.actualVersion.startsWith(" 3" )
223+
224+ val scalacOptions =
225+ workRequest.plugins.view.map(p => s " -Xplugin: $p" ).toArray ++
226+ workRequest.compilerOptions ++
227+ workRequest.compilerOptionsReferencingPaths.toArray ++
228+ (if (shouldIncludeSourceRoot)
229+ Array (" -sourceroot" , task.workDir.toAbsolutePath().toString)
230+ else
231+ Array .empty[String ])
232+
215233 val compileOptions =
216234 CompileOptions .create
217235 .withSources(sources.view.map(source => PlainVirtualFile (source.toAbsolutePath().normalize())).toArray)
218236 .withClasspath((classesOutputDir +: deps.view.map(_.classpath)).map(path => PlainVirtualFile (path)).toArray)
219237 .withClassesDirectory(classesOutputDir)
220238 .withJavacOptions(workRequest.javaCompilerOptions)
221- .withScalacOptions(
222- workRequest.plugins.view.map(p => s " -Xplugin: $p" ).toArray ++
223- workRequest.compilerOptions ++
224- workRequest.compilerOptionsReferencingPaths.toArray,
225- )
239+ .withScalacOptions(scalacOptions)
226240
227241 val compilers = {
228242 val scalaCompiler = ZincUtil
@@ -335,14 +349,17 @@ object ZincRunner extends WorkerMain[ZincRunnerWorkerConfig] {
335349 )
336350 }
337351
352+ // Filter out non-deterministic --sourceroot paths from the setup
353+ val filteredSetup = FilteredSetup .getFilteredSetup(compileResult.setup)
354+
338355 val analysisStoreText = AnalysisUtil .getAnalysisStore(
339356 new File (pathString.substring(0 , pathString.length() - 3 ) + " .text.gz" ),
340357 true ,
341358 readWriteMappers,
342359 )
343360
344- analysisStoreText.set(AnalysisContents .create(resultAnalysis, compileResult.setup ))
345- analysisStore.set(AnalysisContents .create(resultAnalysis, compileResult.setup ))
361+ analysisStoreText.set(AnalysisContents .create(resultAnalysis, filteredSetup ))
362+ analysisStore.set(AnalysisContents .create(resultAnalysis, filteredSetup ))
346363
347364 // create used deps
348365 val usedDeps =
0 commit comments