Skip to content

Commit 65e56c4

Browse files
vinnybodfzakaria
andauthored
Make java library earlier on classpath (#1797)
* Make java library earlier on classpath Co-Authored-By: Farid Zakaria <farid.m.zakaria@gmail.com> * work around strict deps check * add additional build_tag_filters * another edit to the filters * code review feedback --------- Co-authored-by: Farid Zakaria <farid.m.zakaria@gmail.com>
1 parent 904f003 commit 65e56c4

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

scala/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ scala_toolchain(
2727
name = "minimal_direct_source_deps_impl",
2828
dependency_mode = "plus-one",
2929
dependency_tracking_method = "ast",
30+
# Exclude the shadowing test package from strict-deps to allow classpath-order coverage without disabling strict-deps globally in CI runs.
31+
dependency_tracking_strict_deps_patterns = [
32+
"", # keep default include
33+
"-//test/src/main/scala/scalarules/test/java_classpath_order",
34+
],
3035
strict_deps_mode = "error",
3136
unused_dependency_checker_mode = "error",
3237
)

scala/private/phases/phase_compile.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ def _compile_or_empty(
251251

252252
full_jars = [ctx.outputs.jar]
253253
if java_jar:
254-
full_jars.append(java_jar.jar)
254+
full_jars.insert(0, java_jar.jar)
255255

256256
if java_jar:
257-
merged_provider = java_common.merge([scala_compilation_provider, java_jar.java_compilation_provider])
257+
merged_provider = java_common.merge([java_jar.java_compilation_provider, scala_compilation_provider])
258258
else:
259259
merged_provider = scala_compilation_provider
260260

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("//scala:scala.bzl", "scala_library", "scala_test")
2+
3+
# Original library with the class to be shadowed
4+
scala_library(
5+
name = "original",
6+
srcs = ["Original.scala"],
7+
)
8+
9+
# Library that shadows the Overridable class with a Java file
10+
# The Java file (Overridable.java) should take precedence over
11+
# the Overridable class from the :original dependency
12+
scala_library(
13+
name = "with_java_override",
14+
srcs = ["Overridable.java"],
15+
deps = [":original"],
16+
)
17+
18+
# Test that verifies the Java override works correctly
19+
# Note: This test is incompatible with strict deps checking because the checker
20+
# doesn't understand class shadowing - it sees Overridable as coming from :original
21+
# rather than the shadowed version from :with_java_override
22+
scala_test(
23+
name = "verify_override_test",
24+
srcs = ["VerifyOverride.scala"],
25+
unused_dependency_checker_mode = "off",
26+
deps = [":with_java_override"],
27+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package scalarules.test.java_classpath_order
2+
3+
class Overridable {
4+
def getValue(): String = "original"
5+
}
6+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package scalarules.test.java_classpath_order;
2+
3+
public class Overridable {
4+
public String getValue() {
5+
return "overridden";
6+
}
7+
}
8+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scalarules.test.java_classpath_order
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
/**
7+
* This test verifies that the Java class (Overridable) shadows
8+
* the Scala class from the dependency.
9+
*
10+
* The expected behavior is:
11+
* - The Java file compiled in the scala_library with_java_override should
12+
* appear first on the classpath
13+
* - When instantiating Overridable, we should get the Java version
14+
* which returns "overridden"
15+
*/
16+
class VerifyOverrideTest extends AnyFlatSpec with Matchers {
17+
18+
"Java class in scala_library" should "shadow the dependency's class" in {
19+
val instance = new Overridable()
20+
val value = instance.getValue()
21+
22+
value shouldBe "overridden"
23+
}
24+
}
25+

0 commit comments

Comments
 (0)