Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scala/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ scala_toolchain(
name = "minimal_direct_source_deps_impl",
dependency_mode = "plus-one",
dependency_tracking_method = "ast",
# Exclude the shadowing test package from strict-deps to allow classpath-order coverage without disabling strict-deps globally in CI runs.
dependency_tracking_strict_deps_patterns = [
"", # keep default include
"-//test/src/main/scala/scalarules/test/java_classpath_order",
],
strict_deps_mode = "error",
unused_dependency_checker_mode = "error",
)
Expand Down
4 changes: 2 additions & 2 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ def _compile_or_empty(

full_jars = [ctx.outputs.jar]
if java_jar:
full_jars.append(java_jar.jar)
full_jars.insert(0, java_jar.jar)

if java_jar:
merged_provider = java_common.merge([scala_compilation_provider, java_jar.java_compilation_provider])
merged_provider = java_common.merge([java_jar.java_compilation_provider, scala_compilation_provider])
else:
merged_provider = scala_compilation_provider

Expand Down
27 changes: 27 additions & 0 deletions test/src/main/scala/scalarules/test/java_classpath_order/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("//scala:scala.bzl", "scala_library", "scala_test")

# Original library with the class to be shadowed
scala_library(
name = "original",
srcs = ["Original.scala"],
)

# Library that shadows the Overridable class with a Java file
# The Java file (Overridable.java) should take precedence over
# the Overridable class from the :original dependency
scala_library(
name = "with_java_override",
srcs = ["Overridable.java"],
deps = [":original"],
)

# Test that verifies the Java override works correctly
# Note: This test is incompatible with strict deps checking because the checker
# doesn't understand class shadowing - it sees Overridable as coming from :original
# rather than the shadowed version from :with_java_override
scala_test(
name = "verify_override_test",
srcs = ["VerifyOverride.scala"],
unused_dependency_checker_mode = "off",
deps = [":with_java_override"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package scalarules.test.java_classpath_order

class Overridable {
def getValue(): String = "original"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package scalarules.test.java_classpath_order;

public class Overridable {
public String getValue() {
return "overridden";
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scalarules.test.java_classpath_order

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

/**
* This test verifies that the Java class (Overridable) shadows
* the Scala class from the dependency.
*
* The expected behavior is:
* - The Java file compiled in the scala_library with_java_override should
* appear first on the classpath
* - When instantiating Overridable, we should get the Java version
* which returns "overridden"
*/
class VerifyOverrideTest extends AnyFlatSpec with Matchers {

"Java class in scala_library" should "shadow the dependency's class" in {
val instance = new Overridable()
val value = instance.getValue()

value shouldBe "overridden"
}
}