Skip to content

Commit 4947f9d

Browse files
authored
Add examples/overridden_artifacts and test (#1713)
Adds the `examples/overridden_artifacts` repository and the corresponding `overridden_artifacts_example` test case in `test/shell/test_examples.sh`. Broken out from #1710, and part of #1482 and #1652. @dmivankov noticed the design bug in the upcoming Bzlmod API for `overridden_artifacts` that this change addresses. See: - #1482 (comment) - #1482 (comment) Makes `_validate_scalac_srcjar()` and `dt_patched_compiler_setup()` in `scala/private/macros/scala_repositories.bzl` more tolerant of dictionaries containing keys mapped to `None`. The new `overridden_artifacts_example` test covers this. Sets `.bazelversion` in the new repo to 7.5.0 to match changes in both #1710 and #1711. This change is smaller and more focused than #1710, and should ultimately make that pull request smaller and/or easier to review.
1 parent b5b5a28 commit 4947f9d

File tree

8 files changed

+137
-3
lines changed

8 files changed

+137
-3
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import ../../.bazelrc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.5.0

examples/overridden_artifacts/BUILD

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@rules_scala//scala:scala.bzl", "scala_library", "scala_test")
2+
3+
scala_library(
4+
name = "hello",
5+
srcs = ["Hello.scala"],
6+
)
7+
8+
scala_test(
9+
name = "hello-test",
10+
srcs = ["HelloTest.scala"],
11+
deps = [":hello"],
12+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package overriddenartifactstest
2+
3+
class Hello(version: String):
4+
def greetings(): String = "Hello, World! This is Scala " + version + "."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package overriddenartifactstest
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class HelloTest extends AnyFunSuite:
6+
test("greetings includes the correct Scala version number") {
7+
val hello = new Hello(util.Properties.versionNumberString)
8+
9+
// Apparently Scala 3 code will still return a Scala 2 version number:
10+
// - https://users.scala-lang.org/t/what-scala-library-version-is-used-by-which-scala-3-version/9999
11+
assert(hello.greetings().endsWith("2.13.14."))
12+
}
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
workspace(name = "overridden_artifacts")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
local_repository(
6+
name = "rules_scala",
7+
path = "../..",
8+
)
9+
10+
load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")
11+
12+
rules_scala_dependencies()
13+
14+
load(
15+
"@rules_java//java:repositories.bzl",
16+
"rules_java_dependencies",
17+
"rules_java_toolchains",
18+
)
19+
20+
rules_java_dependencies()
21+
22+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
23+
24+
bazel_skylib_workspace()
25+
26+
http_archive(
27+
name = "rules_python",
28+
sha256 = "ca2671529884e3ecb5b79d6a5608c7373a82078c3553b1fa53206e6b9dddab34",
29+
strip_prefix = "rules_python-0.38.0",
30+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.38.0/rules_python-0.38.0.tar.gz",
31+
)
32+
33+
load("@rules_python//python:repositories.bzl", "py_repositories")
34+
35+
py_repositories()
36+
37+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
38+
39+
protobuf_deps()
40+
41+
rules_java_toolchains()
42+
43+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")
44+
45+
rules_proto_dependencies()
46+
47+
load("@rules_proto//proto:setup.bzl", "rules_proto_setup")
48+
49+
rules_proto_setup()
50+
51+
load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")
52+
53+
rules_proto_toolchains()
54+
55+
load("@rules_scala//:scala_config.bzl", "scala_config")
56+
57+
scala_config(scala_version = "3.3.5")
58+
59+
load(
60+
"@rules_scala//scala:toolchains.bzl",
61+
"scala_register_toolchains",
62+
"scala_toolchains",
63+
)
64+
65+
scala_toolchains(
66+
# Deliberately set for Scala 3.3 and 2.13 versions less than the most
67+
# recently supported. See the `scala_version` setting at the top of
68+
# `third_party/repositories/scala_{2_13,3_3}.bzl`.
69+
overridden_artifacts = {
70+
"io_bazel_rules_scala_scala_library": {
71+
"artifact": "org.scala-lang:scala3-library_3:3.3.4",
72+
"sha256": "d95184acfcd814da2e051378e4962c653f4b468f4086452ab427af030482bd3c",
73+
},
74+
"io_bazel_rules_scala_scala_compiler": {
75+
"artifact": "org.scala-lang:scala3-compiler_3:3.3.4",
76+
"sha256": "2cca65fdb92e2cc393786cae61b4f7bcb9032ad4be61f9cebae1dca72997e52f",
77+
# These are _not_ strictly required in this case, but we want to
78+
# test that nothing breaks when they're specified.
79+
"deps": [
80+
"@io_bazel_rules_scala_scala_asm",
81+
"@io_bazel_rules_scala_scala_interfaces",
82+
"@io_bazel_rules_scala_scala_library",
83+
"@io_bazel_rules_scala_scala_tasty_core",
84+
"@org_jline_jline_reader",
85+
"@org_jline_jline_terminal",
86+
"@org_jline_jline_terminal_jni",
87+
"@org_scala_sbt_compiler_interface",
88+
],
89+
},
90+
"io_bazel_rules_scala_scala_library_2": {
91+
"artifact": "org.scala-lang:scala-library:2.13.14",
92+
"sha256": "43e0ca1583df1966eaf02f0fbddcfb3784b995dd06bfc907209347758ce4b7e3",
93+
},
94+
},
95+
scalatest = True,
96+
)
97+
98+
scala_register_toolchains()

scala/private/macros/scala_repositories.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _validate_scalac_srcjar(srcjar):
5656
oneof = ["url", "urls", "label"]
5757
count = 0
5858
for key in oneof:
59-
if key in srcjar:
59+
if srcjar.get(key):
6060
count += 1
6161
return count == 1
6262

@@ -94,7 +94,7 @@ def dt_patched_compiler_setup(scala_version, scala_compiler_srcjar = None):
9494
("scala_compiler_srcjar invalid, must be a dict with exactly one of \"label\", \"url\"" +
9595
" or \"urls\" keys, got: ") + repr(srcjar),
9696
)
97-
if "label" in srcjar:
97+
if srcjar.get("label"):
9898
dt_patched_compiler(
9999
name = "scala_compiler_source" + version_suffix(scala_version),
100100
build_file_content = build_file_content,

test/shell/test_examples.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ function cross_build_example() {
6767
test_example examples/crossbuild "bazel build //..."
6868
}
6969

70+
function overridden_artifacts_example() {
71+
test_example examples/overridden_artifacts \
72+
"bazel test --test_output=errors //..."
73+
}
74+
7075
$runner scalatest_repositories_example
7176
$runner specs2_junit_repositories_example
7277
$runner multi_framework_toolchain_example
@@ -77,4 +82,5 @@ $runner scala3_3_example
7782
$runner scala3_4_example
7883
$runner scala3_5_example
7984
$runner scala3_6_example
80-
$runner cross_build_example
85+
$runner cross_build_example
86+
$runner overridden_artifacts_example

0 commit comments

Comments
 (0)