Skip to content

Commit fa15a07

Browse files
author
Blaine Freestone
committed
remove sourceroot flag from analysis_store file content (it's not deterministic)
1 parent 62a1b67 commit fa15a07

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sbt.internal.inc
2+
3+
import xsbti.compile.{CompileOptions, MiniSetup}
4+
import java.nio.file.{Path, Paths}
5+
6+
/**
7+
* Zinc's MiniSetup contains scalacOptions which include the --sourceroot flag that references
8+
* absolute sandbox paths. These paths are non-deterministic across builds because the sandbox
9+
* directory changes (e.g., __sandbox/4/_main vs __sandbox/8/_main), making the analysis files
10+
* non-deterministic.
11+
*
12+
* This class filters out the --sourceroot option from the scalacOptions to ensure deterministic
13+
* analysis files.
14+
*
15+
* TODO: Consider if there's a better way to handle this upstream in Zinc
16+
*/
17+
object FilteredSetup {
18+
private val sourcerootFlag = "--sourceroot"
19+
20+
def getFilteredSetup(setup: MiniSetup): MiniSetup = {
21+
val options = setup.options()
22+
23+
// Filter out the --sourceroot option and its value
24+
val filteredScalacOptions = {
25+
val originalOptions = options.scalacOptions()
26+
val filtered = scala.collection.mutable.ArrayBuffer[String]()
27+
28+
var i = 0
29+
while (i < originalOptions.length) {
30+
val option = originalOptions(i)
31+
if (option == sourcerootFlag) {
32+
// Skip both the flag and its value (next argument)
33+
i += 2
34+
} else {
35+
filtered += option
36+
i += 1
37+
}
38+
}
39+
40+
filtered.toArray
41+
}
42+
43+
// Create new CompileOptions with filtered scalac options
44+
val newOptions = options.withScalacOptions(filteredScalacOptions)
45+
46+
// Create new MiniSetup with filtered options
47+
setup.withOptions(newOptions)
48+
}
49+
}

src/main/scala/higherkindness/rules_scala/workers/zinc/compile/ZincRunner.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import net.sourceforge.argparse4j.impl.Arguments as Arg
1818
import net.sourceforge.argparse4j.inf.{ArgumentParserException, Namespace}
1919
import sbt.internal.inc.classpath.ClassLoaderCache
2020
import 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}
2222
import scala.jdk.CollectionConverters.*
2323
import scala.util.Try
2424
import scala.util.control.NonFatal
@@ -336,14 +336,17 @@ object ZincRunner extends WorkerMain[ZincRunnerWorkerConfig] {
336336
)
337337
}
338338

339+
// Filter out non-deterministic --sourceroot paths from the setup
340+
val filteredSetup = FilteredSetup.getFilteredSetup(compileResult.setup)
341+
339342
val analysisStoreText = AnalysisUtil.getAnalysisStore(
340343
new File(pathString.substring(0, pathString.length() - 3) + ".text.gz"),
341344
true,
342345
readWriteMappers,
343346
)
344347

345-
analysisStoreText.set(AnalysisContents.create(resultAnalysis, compileResult.setup))
346-
analysisStore.set(AnalysisContents.create(resultAnalysis, compileResult.setup))
348+
analysisStoreText.set(AnalysisContents.create(resultAnalysis, filteredSetup))
349+
analysisStore.set(AnalysisContents.create(resultAnalysis, filteredSetup))
347350

348351
// create used deps
349352
val usedDeps =

0 commit comments

Comments
 (0)