Skip to content

Commit

Permalink
Add examples/overridden_artifacts and test
Browse files Browse the repository at this point in the history
Adds the `examples/overridden_artifacts` repository and the
corresponding `overridden_artifacts_example` test case in
`test/shell/test_examples.sh`. Broken out from bazelbuild#1710, and part of bazelbuild#1482
and bazelbuild#1652.

@dmivankov noticed the design bug in the upcoming Bzlmod API for
`overridden_artifacts` that this change addresses. See:

- bazelbuild#1482 (comment)
- bazelbuild#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 bazelbuild#1710 and bazelbuild#1711.

This change is smaller and more focused than bazelbuild#1710, and should
ultimately make that pull request smaller and/or easier to review.
  • Loading branch information
mbland committed Mar 7, 2025
1 parent eadc090 commit 9fe85de
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/overridden_artifacts/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ../../.bazelrc
1 change: 1 addition & 0 deletions examples/overridden_artifacts/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.5.0
12 changes: 12 additions & 0 deletions examples/overridden_artifacts/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_scala//scala:scala.bzl", "scala_library", "scala_test")

scala_library(
name = "hello",
srcs = ["Hello.scala"],
)

scala_test(
name = "hello-test",
srcs = ["HelloTest.scala"],
deps = [":hello"],
)
4 changes: 4 additions & 0 deletions examples/overridden_artifacts/Hello.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package overriddenartifactstest

class Hello(version: String):
def greetings(): String = "Hello, World! This is Scala " + version + "."
12 changes: 12 additions & 0 deletions examples/overridden_artifacts/HelloTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package overriddenartifactstest

import org.scalatest.funsuite.AnyFunSuite

class HelloTest extends AnyFunSuite:
test("greetings includes the correct Scala version number") {
val hello = new Hello(util.Properties.versionNumberString)

// Apparently Scala 3 code will still return a Scala 2 version number:
// - https://users.scala-lang.org/t/what-scala-library-version-is-used-by-which-scala-3-version/9999
assert(hello.greetings().endsWith("2.13.14."))
}
98 changes: 98 additions & 0 deletions examples/overridden_artifacts/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
workspace(name = "overridden_artifacts")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "rules_scala",
path = "../..",
)

load("@rules_scala//scala:deps.bzl", "rules_scala_dependencies")

rules_scala_dependencies()

load(
"@rules_java//java:repositories.bzl",
"rules_java_dependencies",
"rules_java_toolchains",
)

rules_java_dependencies()

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
name = "rules_python",
sha256 = "ca2671529884e3ecb5b79d6a5608c7373a82078c3553b1fa53206e6b9dddab34",
strip_prefix = "rules_python-0.38.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.38.0/rules_python-0.38.0.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

rules_java_toolchains()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

rules_proto_dependencies()

load("@rules_proto//proto:setup.bzl", "rules_proto_setup")

rules_proto_setup()

load("@rules_proto//proto:toolchains.bzl", "rules_proto_toolchains")

rules_proto_toolchains()

load("@rules_scala//:scala_config.bzl", "scala_config")

scala_config(scala_version = "3.3.5")

load(
"@rules_scala//scala:toolchains.bzl",
"scala_register_toolchains",
"scala_toolchains",
)

scala_toolchains(
# Deliberately set for Scala 3.3 and 2.13 versions less than the most
# recently supported. See the `scala_version` setting at the top of
# `third_party/repositories/scala_{2_13,3_3}.bzl`.
overridden_artifacts = {
"io_bazel_rules_scala_scala_library": {
"artifact": "org.scala-lang:scala3-library_3:3.3.4",
"sha256": "d95184acfcd814da2e051378e4962c653f4b468f4086452ab427af030482bd3c",
},
"io_bazel_rules_scala_scala_compiler": {
"artifact": "org.scala-lang:scala3-compiler_3:3.3.4",
"sha256": "2cca65fdb92e2cc393786cae61b4f7bcb9032ad4be61f9cebae1dca72997e52f",
# These are _not_ strictly required in this case, but we want to
# test that nothing breaks when they're specified.
"deps": [
"@io_bazel_rules_scala_scala_asm",
"@io_bazel_rules_scala_scala_interfaces",
"@io_bazel_rules_scala_scala_library",
"@io_bazel_rules_scala_scala_tasty_core",
"@org_jline_jline_reader",
"@org_jline_jline_terminal",
"@org_jline_jline_terminal_jni",
"@org_scala_sbt_compiler_interface",
],
},
"io_bazel_rules_scala_scala_library_2": {
"artifact": "org.scala-lang:scala-library:2.13.14",
"sha256": "43e0ca1583df1966eaf02f0fbddcfb3784b995dd06bfc907209347758ce4b7e3",
},
},
scalatest = True,
)

scala_register_toolchains()
4 changes: 2 additions & 2 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _validate_scalac_srcjar(srcjar):
oneof = ["url", "urls", "label"]
count = 0
for key in oneof:
if key in srcjar:
if srcjar.get(key):
count += 1
return count == 1

Expand Down Expand Up @@ -94,7 +94,7 @@ def dt_patched_compiler_setup(scala_version, scala_compiler_srcjar = None):
("scala_compiler_srcjar invalid, must be a dict with exactly one of \"label\", \"url\"" +
" or \"urls\" keys, got: ") + repr(srcjar),
)
if "label" in srcjar:
if srcjar.get("label"):
dt_patched_compiler(
name = "scala_compiler_source" + version_suffix(scala_version),
build_file_content = build_file_content,
Expand Down
8 changes: 7 additions & 1 deletion test/shell/test_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ function cross_build_example() {
test_example examples/crossbuild "bazel build //..."
}

function overridden_artifacts_example() {
test_example examples/overridden_artifacts \
"bazel test --test_output=errors //..."
}

$runner scalatest_repositories_example
$runner specs2_junit_repositories_example
$runner multi_framework_toolchain_example
Expand All @@ -77,4 +82,5 @@ $runner scala3_3_example
$runner scala3_4_example
$runner scala3_5_example
$runner scala3_6_example
$runner cross_build_example
$runner cross_build_example
$runner overridden_artifacts_example

0 comments on commit 9fe85de

Please sign in to comment.