forked from higherkindness/rules_scala
-
Notifications
You must be signed in to change notification settings - Fork 5
Fix tasty file non-determinism with sourceroot argument in multiplex worker and analysis_store.gz changes
#98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
blainefreestone
merged 2 commits into
lucid-master
from
bfreestone-fixes-source-filepath-determinism-issue
Jul 29, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/scala/higherkindness/rules_scala/workers/zinc/compile/FilteredSetup.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package sbt.internal.inc | ||
|
|
||
| import xsbti.compile.{CompileOptions, MiniSetup} | ||
| import java.nio.file.{Path, Paths} | ||
|
|
||
| /** | ||
| * Zinc's MiniSetup contains scalacOptions which include the -sourceroot flag that references absolute sandbox paths. | ||
| * These paths are non-deterministic across builds because the sandbox directory changes (e.g., __sandbox/4/_main vs | ||
| * __sandbox/8/_main), making the analysis files non-deterministic. | ||
| * | ||
| * This class filters out the -sourceroot option from the scalacOptions to ensure deterministic analysis files. | ||
| * | ||
| * TODO: Consider if there's a better way to handle this upstream in Zinc | ||
| */ | ||
|
|
||
| object FilteredSetup { | ||
| private val sourcerootFlag = "-sourceroot" | ||
|
|
||
| def getFilteredSetup(setup: MiniSetup): MiniSetup = { | ||
| val options = setup.options() | ||
| // Filter out the -sourceroot option and its value | ||
| val filteredScalacOptions = { | ||
| val originalOptions = options.scalacOptions() | ||
| val filtered = scala.collection.mutable.ArrayBuffer[String]() | ||
| var i = 0 | ||
| while (i < originalOptions.length) { | ||
| val option = originalOptions(i) | ||
| if (option == sourcerootFlag) { | ||
| // Skip both the flag and its value (next argument) | ||
| i += 2 | ||
| } else { | ||
| filtered += option | ||
| i += 1 | ||
| } | ||
| } | ||
| filtered.toArray | ||
| } | ||
|
|
||
| // Create new CompileOptions with filtered scalac options | ||
| val newOptions = options.withScalacOptions(filteredScalacOptions) | ||
| // Create new MiniSetup with filtered options | ||
| setup.withOptions(newOptions) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| load("@rules_scala_annex//rules:scala.bzl", "scala_library") | ||
|
|
||
| # Sample library to test TASTy determinism | ||
| scala_library( | ||
| name = "tasty_test_lib", | ||
| srcs = ["SampleClass.scala"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package determinism.test | ||
|
|
||
| class SampleClass { | ||
| def method1(): String = "hello" | ||
|
|
||
| def method2(x: Int, y: String): Boolean = { | ||
| x > 0 && y.nonEmpty | ||
| } | ||
|
|
||
| private val field = 42 | ||
|
|
||
| case class InnerCase(name: String, value: Int) | ||
|
|
||
| object InnerObject { | ||
| def compute(): Int = field * 2 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/bin/bash -e | ||
| . "$(dirname "$0")"/../common.sh | ||
|
|
||
| # Test that verifies TASTy files can be generated deterministically | ||
| echo "Testing TASTy file generation with Scala 3..." | ||
blainefreestone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Build the Scala 3 target, explicitly using Scala 3 toolchain | ||
| echo "Building target with Scala 3 toolchain..." | ||
| bazel build --@rules_scala_annex//rules/scala:scala-toolchain=test_zinc_3 --keep_going --remote_executor= --remote_cache= --disk_cache= :tasty_test_lib | ||
jadenPete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Get the generated jar file | ||
| bazel_bin=$(bazel info bazel-bin) | ||
| jar_file="$bazel_bin/determinism/tasty_test_lib.jar" | ||
|
|
||
| echo "Extracting TASTy files from build..." | ||
| temp_dir=$(mktemp -d) | ||
|
|
||
| cleanup() { | ||
| exit_code=$? | ||
| for dir in "${temp_dirs[@]}"; do | ||
| rm -r "$dir" 2>/dev/null || true | ||
| done | ||
| finish $exit_code | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| # Extract all .tasty files from the jar | ||
| unzip -j "$jar_file" "*.tasty" -d "$temp_dir" 2>/dev/null || { | ||
| echo "No .tasty files found in jar, checking if they exist at all..." | ||
| unzip -l "$jar_file" | grep -i tasty || { | ||
| echo "ERROR: No TASTy files found in the generated jar" | ||
| echo "Jar contents:" | ||
| unzip -l "$jar_file" | ||
| exit 1 | ||
| } | ||
| } | ||
|
|
||
| # Verify TASTy files were generated | ||
| tasty_files=$(find "$temp_dir" -name "*.tasty" | wc -l) | ||
| if [ "$tasty_files" -eq 0 ]; then | ||
| echo "ERROR: No TASTy files were extracted" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "SUCCESS: Found $tasty_files TASTy file(s) generated by Scala 3 compiler" | ||
|
|
||
| # Show what files were found | ||
| echo "Generated TASTy files:" | ||
| find "$temp_dir" -name "*.tasty" -exec basename {} \; | ||
|
|
||
| # Test determinism by rebuilding multiple times | ||
| echo "Testing determinism by rebuilding 5 times..." | ||
|
|
||
| # Array to store temp directories for each build | ||
| temp_dirs=("$temp_dir") | ||
|
|
||
| # Perform 4 additional builds (we already have one) | ||
| for i in $(seq 2 5); do | ||
| echo "Build $i/5..." | ||
| bazel clean | ||
| bazel build --@rules_scala_annex//rules/scala:scala-toolchain=test_zinc_3 --remote_executor= --remote_cache= --disk_cache= :tasty_test_lib | ||
|
|
||
| # Create temp directory for this build | ||
| temp_dir_n=$(mktemp -d) | ||
| temp_dirs+=("$temp_dir_n") | ||
|
|
||
| # Extract TASTy files from this build | ||
| unzip -j "$jar_file" "*.tasty" -d "$temp_dir_n" 2>/dev/null || { | ||
| echo "ERROR: Failed to extract TASTy files from build $i" | ||
| exit 1 | ||
| } | ||
| done | ||
|
|
||
| # Compare all builds against the first one | ||
| echo "Comparing TASTy files across all 5 builds for determinism..." | ||
| all_identical=true | ||
|
|
||
| for i in $(seq 2 5); do | ||
| build_num=$((i-1)) | ||
| echo "Comparing build 1 with build $i..." | ||
| diff_output=$(diff -r "${temp_dirs[0]}" "${temp_dirs[$build_num]}" 2>&1) | ||
| if [ $? -ne 0 ]; then | ||
| echo "ERROR: TASTy files differ between build 1 and build $i - not deterministic" | ||
| echo "Differences found:" | ||
| echo "$diff_output" | ||
| all_identical=false | ||
| fi | ||
| done | ||
|
|
||
| if [ "$all_identical" = true ]; then | ||
| echo "SUCCESS: TASTy files are identical across all 5 builds - build is deterministic!" | ||
| else | ||
| echo "ERROR: TASTy files differ between builds - not deterministic" | ||
| exit 1 | ||
| fi | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.