Skip to content

Commit

Permalink
Fix resource priority under name collision (#1456)
Browse files Browse the repository at this point in the history
* Add failing test for resource name duplication

* Topological sort jars (so that the target jar comes first)

fix #1456

* Sort jars by pre-topological sort to preserve leftmost-first guarantee

With topological order, there's no left-to-right order guarantee
https://bazel.build/rules/lib/depset

As a result, `test/shell/test_misc`'s `test_multi_service_manifest`
fails because the order of `exepected_service_manifest.txt` reverses.
  • Loading branch information
tanishiking authored Dec 12, 2022
1 parent dee33f4 commit 15f2461
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _phase_compile(
# TODO: simplify the return values and use provider
return struct(
files = depset(out.full_jars),
rjars = depset(out.full_jars, transitive = [rjars]),
rjars = depset(out.full_jars, transitive = [rjars], order = "preorder"),
merged_provider = out.merged_provider,
external_providers = {
"JavaInfo": out.merged_provider,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//scala:scala.bzl", "scala_test")

# https://github.com/bazelbuild/rules_scala/issues/1455
scala_test(
name = "child",
size = "small",
srcs = ["ScalaLibResourcesDuplicatedTest.scala"],
resource_strip_prefix = "test/src/main/scala/scalarules/test/duplicated_resources/child/",
resources = ["resource.txt"],
unused_dependency_checker_mode = "off",
deps = ["//test/src/main/scala/scalarules/test/duplicated_resources/parent"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scalarules.test.duplicated

import org.scalatest.funsuite._

class ScalaLibResourcesDuplicatedTest extends AnyFunSuite {

test("Scala library depends on resource + deps that contains same name resources, have higher priority on this target's resource.") {
assert(get("/resource.txt") === "I am a text resource from child!\n")
}

private def get(s: String): String =
scala.io.Source.fromInputStream(getClass.getResourceAsStream(s)).mkString

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am a text resource from child!
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//scala:scala.bzl", "scala_library")

scala_library(
name = "parent",
resource_strip_prefix = "test/src/main/scala/scalarules/test/duplicated_resources/parent/",
resources = ["resource.txt"],
visibility = ["//test/src/main/scala/scalarules/test/duplicated_resources/child:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am a text resource from parent!

0 comments on commit 15f2461

Please sign in to comment.