From 7d859691bce3b94b4213a36de299567a9e6d047b Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Thu, 14 Nov 2024 13:45:03 -0500 Subject: [PATCH 1/5] Toolchainize all testing toolchains Moves the Scalatest, JUnit, and Specs2 toolchains into `@io_bazel_rules_scala_toolchains//testing`. Part of #1482. Updates all `WORKSPACE` files to set the appropriate `scala_toolchains` parameters and to remove the unnecessary repository import and toolchain registration macros. Adds a `fetch_sources_by_id` parameter to `repositories` from `third_party/repositories/repositories.bzl`. This enables `scala_toolchains` to build the `artifact_ids_to_fetch_sources` mapping from artifact ID lists returned by new macros extracted from `WORKSPACE` macros. The values assigned to each id match the original `fetch_sources` settings in the corresponding original `WORKSPACE` macros. Updates `scala/scala_maven_import_external.bzl` to generate a `load` line for `//scala:scala_import.bzl` based on the repo's canonical name, not `@io_bazel_rules_scala`. As usual, includes several other opportunistic removals of the `@io_bazel_rules_scala` repo name prefix to avoid an internal dependency on that name. This means Bzlmod users won't necessarily have to set the `repo_name` parameter of `bazel_dep` when using `rules_scala`. --- Introduces more macros to return a framework's Maven artifact dependencies, rather than inlining them in a `repositories` call. These inlined lists are replaced by macro invocations, and now the `scala_toolchains` macro can invoke these macros to collect artifact IDs to pass to `repositories`. This also allows for future changes to introduce a `scala_version` parameter if necessary, similar to how `scalafmt_artifact_ids` already works. This is important to avoid collisions when creating repositories for artifacts upon which more than one framework depends under Bzlmod. `WORKSPACE` doesn't seem affected by these collisions, but Bzlmod will produce errors like the following, where both `scala_proto` and `twitter_scrooge` depend upon `io_bazel_rules_scala_guava`: ```txt $ bazel build //src/... ERROR: .../scala/scala_maven_import_external.bzl:299:24: Traceback (most recent call last): File ".../scala/extensions/deps.bzl", line 140, column 21, in _scala_deps_impl scala_toolchains( File ".../scala/private/macros/toolchains.bzl", line 140, column 17, in scala_toolchains _scrooge( File ".../twitter_scrooge/twitter_scrooge.bzl", line 96, column 17, in twitter_scrooge repositories( File ".../third_party/repositories/repositories.bzl", line 113, column 37, in repositories _scala_maven_import_external( File ".../scala/scala_maven_import_external.bzl", line 263, column 30, in scala_maven_import_external jvm_maven_import_external( File ".../scala/scala_maven_import_external.bzl", line 299, column 24, in jvm_maven_import_external jvm_import_external(jar_urls = jar_urls, srcjar_urls = srcjar_urls, coordinates = artifact, **kwargs) Error in repository_rule: A repo named io_bazel_rules_scala_guava_2_13_15 is already generated by this module extension at .../scala/scala_maven_import_external.bzl:299:24 ERROR: Analysis of target '//src/java/io/bazel/rulesscala/worker:worker_test' failed; build aborted: error evaluating module extension scala_deps in //scala/extensions:deps.bzl ``` Recent updates to `scripts/create_repository.py` (#1639, #1642) make it easy to emit full direct dependency lists for artifacts included in `third_party/repositories/scala_*.bzl`. This increases the likelihood of collisions, since this expanded metadata forces the macros that instantiate artifact repos to instantiate even more repos. By fetching list of artifact IDs from these macros, `scala_toolchains` can now consolidate them into dictionary keys. Then it passes these unique keys to `repositories` directly, avoiding the problem of instantiating the same repo multiple times in the same module extension. This, in turn, also avoids the need to add parameters to the original `WORKSPACE` macros that instantiate dependencies to avoid collisions under Bzlmod. The `scala_toolchains` macro never needs to call these original macros, under either `WORKSPACE` or Bzlmod. Finally, it also reduces duplication between these artifact ID lists and the `_*_DEPS` symbols originally from `testing/BUILD` (and now in `testing/deps.bzl`). The dependency labels are now generated programatically. (Aside: As I mentioned, we may eventually need to pass a Scala version argument to these macros. It will be possible to cross that bridge without too much trouble if and when that day comes. Or I can try to future proof it in a follow up pull request.) --- WORKSPACE | 14 +-- examples/crossbuild/WORKSPACE | 8 +- .../testing/multi_frameworks_toolchain/BUILD | 6 +- .../multi_frameworks_toolchain/WORKSPACE | 15 +-- .../testing/scalatest_repositories/WORKSPACE | 11 +-- .../specs2_junit_repositories/WORKSPACE | 11 +-- junit/junit.bzl | 11 ++- scala/private/macros/toolchains.bzl | 66 +++++++++++++- scala/private/macros/toolchains_repo.bzl | 67 ++++++++++++++ scala/private/rules/scala_junit_test.bzl | 23 ++--- scala/private/rules/scala_test.bzl | 3 +- scala/scala_import.bzl | 13 ++- scalatest/scalatest.bzl | 39 ++++---- specs2/specs2.bzl | 17 ++-- specs2/specs2_junit.bzl | 11 ++- test_cross_build/WORKSPACE | 10 +- test_version/WORKSPACE.template | 14 +-- testing/BUILD | 91 +++---------------- testing/deps.bzl | 15 +++ testing/scalatest.bzl | 5 +- testing/testing.bzl | 32 +++++-- testing/toolchain/BUILD | 34 ++----- testing/toolchain/toolchain.bzl | 2 - testing/toolchain/toolchain_deps.bzl | 2 +- third_party/repositories/repositories.bzl | 3 +- .../test/example_external_workspace/WORKSPACE | 11 +-- 26 files changed, 293 insertions(+), 241 deletions(-) create mode 100644 testing/deps.bzl diff --git a/WORKSPACE b/WORKSPACE index b111cf67e..fba94c959 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -32,10 +32,12 @@ scala_config(enable_compiler_dependency_tracking = True) load("//scala:scala.bzl", "scala_toolchains") -scala_toolchains(fetch_sources = True) +scala_toolchains( + fetch_sources = True, + testing = True, +) register_toolchains( - "//testing:testing_toolchain", "//scala:unused_dependency_checker_error_toolchain", "//test/proto:scalapb_toolchain", "@io_bazel_rules_scala_toolchains//...:all", @@ -69,14 +71,6 @@ load("//scala_proto:scala_proto.bzl", "scala_proto_repositories") scala_proto_repositories() -load("//scalatest:scalatest.bzl", "scalatest_repositories") - -scalatest_repositories() - -load("//specs2:specs2_junit.bzl", "specs2_junit_repositories") - -specs2_junit_repositories() - load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_default_config", "scalafmt_repositories") scalafmt_default_config() diff --git a/examples/crossbuild/WORKSPACE b/examples/crossbuild/WORKSPACE index 8cd4ef857..d61c31e81 100644 --- a/examples/crossbuild/WORKSPACE +++ b/examples/crossbuild/WORKSPACE @@ -40,7 +40,7 @@ scala_config( load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains() +scala_toolchains(scalatest = True) register_toolchains("@io_bazel_rules_scala_toolchains//...:all") @@ -59,9 +59,3 @@ rules_proto_toolchains() load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() - -load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain") - -scalatest_repositories() - -scalatest_toolchain() diff --git a/examples/testing/multi_frameworks_toolchain/BUILD b/examples/testing/multi_frameworks_toolchain/BUILD index d6d9a3ee0..b948c8ee2 100644 --- a/examples/testing/multi_frameworks_toolchain/BUILD +++ b/examples/testing/multi_frameworks_toolchain/BUILD @@ -1,6 +1,4 @@ load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") -load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "version_suffix") -load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION") setup_scala_testing_toolchain( name = "testing_toolchain", @@ -8,7 +6,7 @@ setup_scala_testing_toolchain( "@io_bazel_rules_scala_junit_junit", "@io_bazel_rules_scala_org_hamcrest_hamcrest_core", ], - scalatest_classpath = [dep + version_suffix(SCALA_VERSION) for dep in [ + scalatest_classpath = [ "@io_bazel_rules_scala_scalactic", "@io_bazel_rules_scala_scalatest", "@io_bazel_rules_scala_scalatest_compatible", @@ -21,7 +19,7 @@ setup_scala_testing_toolchain( "@io_bazel_rules_scala_scalatest_matchers_core", "@io_bazel_rules_scala_scalatest_mustmatchers", "@io_bazel_rules_scala_scalatest_shouldmatchers", - ]], + ], specs2_classpath = [ "@io_bazel_rules_scala_org_specs2_specs2_common", "@io_bazel_rules_scala_org_specs2_specs2_core", diff --git a/examples/testing/multi_frameworks_toolchain/WORKSPACE b/examples/testing/multi_frameworks_toolchain/WORKSPACE index 2712dfaea..e0e72888d 100644 --- a/examples/testing/multi_frameworks_toolchain/WORKSPACE +++ b/examples/testing/multi_frameworks_toolchain/WORKSPACE @@ -33,7 +33,10 @@ scala_config() load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains(fetch_sources = True) +scala_toolchains( + fetch_sources = True, + testing = True, +) register_toolchains( ":testing_toolchain", @@ -55,13 +58,3 @@ rules_proto_toolchains() load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() - -load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories") -load("@io_bazel_rules_scala//testing:junit.bzl", "junit_repositories") -load("@io_bazel_rules_scala//testing:specs2_junit.bzl", "specs2_junit_repositories") - -scalatest_repositories() - -junit_repositories() - -specs2_junit_repositories() diff --git a/examples/testing/scalatest_repositories/WORKSPACE b/examples/testing/scalatest_repositories/WORKSPACE index d74bf5eae..648fa7f61 100644 --- a/examples/testing/scalatest_repositories/WORKSPACE +++ b/examples/testing/scalatest_repositories/WORKSPACE @@ -33,7 +33,10 @@ scala_config() load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains(fetch_sources = True) +scala_toolchains( + fetch_sources = True, + scalatest = True, +) register_toolchains("@io_bazel_rules_scala_toolchains//...:all") @@ -52,9 +55,3 @@ rules_proto_toolchains() load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() - -load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain") - -scalatest_repositories() - -scalatest_toolchain() diff --git a/examples/testing/specs2_junit_repositories/WORKSPACE b/examples/testing/specs2_junit_repositories/WORKSPACE index 6d5fb0649..7a4ec73ee 100644 --- a/examples/testing/specs2_junit_repositories/WORKSPACE +++ b/examples/testing/specs2_junit_repositories/WORKSPACE @@ -33,7 +33,10 @@ scala_config() load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains(fetch_sources = True) +scala_toolchains( + fetch_sources = True, + specs2 = True, +) register_toolchains("@io_bazel_rules_scala_toolchains//...:all") @@ -52,9 +55,3 @@ rules_proto_toolchains() load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() - -load("@io_bazel_rules_scala//testing:specs2_junit.bzl", "specs2_junit_repositories", "specs2_junit_toolchain") - -specs2_junit_repositories() - -specs2_junit_toolchain() diff --git a/junit/junit.bzl b/junit/junit.bzl index b712269cc..d3abd193e 100644 --- a/junit/junit.bzl +++ b/junit/junit.bzl @@ -4,14 +4,17 @@ load( ) load("//third_party/repositories:repositories.bzl", "repositories") +def junit_artifact_ids(): + return [ + "io_bazel_rules_scala_junit_junit", + "io_bazel_rules_scala_org_hamcrest_hamcrest_core", + ] + def junit_repositories( maven_servers = _default_maven_server_urls(), fetch_sources = True): repositories( - for_artifact_ids = [ - "io_bazel_rules_scala_junit_junit", - "io_bazel_rules_scala_org_hamcrest_hamcrest_core", - ], + for_artifact_ids = junit_artifact_ids(), fetch_sources = fetch_sources, maven_servers = maven_servers, ) diff --git a/scala/private/macros/toolchains.bzl b/scala/private/macros/toolchains.bzl index 70a4c0959..3a851ae22 100644 --- a/scala/private/macros/toolchains.bzl +++ b/scala/private/macros/toolchains.bzl @@ -1,8 +1,14 @@ """Macro to instantiate @io_bazel_rules_scala_toolchains""" load(":macros/toolchains_repo.bzl", "scala_toolchains_repo") +load("//junit:junit.bzl", "junit_artifact_ids") load("//scala/private:macros/scala_repositories.bzl", "scala_repositories") load("//scala:scala_cross_version.bzl", "default_maven_server_urls") +load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") +load("//specs2:specs2.bzl", "specs2_artifact_ids") +load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids") +load("//third_party/repositories:repositories.bzl", "repositories") +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") def scala_toolchains( maven_servers = default_maven_server_urls(), @@ -11,7 +17,11 @@ def scala_toolchains( load_scala_toolchain_dependencies = True, fetch_sources = False, validate_scala_version = True, - scala_compiler_srcjars = {}): + scala_compiler_srcjars = {}, + scalatest = False, + junit = False, + specs2 = False, + testing = False): """Instantiates @io_bazel_rules_scala_toolchains and all its dependencies. Provides a unified interface to configuring rules_scala both directly in a @@ -51,6 +61,11 @@ def scala_toolchains( compiler srcjar metadata dictionaries containing: - exactly one "label", "url", or "urls" key - optional "integrity" or "sha256" keys + scalatest: whether to instantiate the Scalatest toolchain + junit: whether to instantiate the JUnit toolchain + specs2: whether to instantiate the Specs2 JUnit toolchain + testing: whether to instantiate the Scalatest, JUnit, and Specs2 JUnit + toolchains combined """ scala_repositories( maven_servers = maven_servers, @@ -63,4 +78,51 @@ def scala_toolchains( scala_compiler_srcjars = scala_compiler_srcjars, ) - scala_toolchains_repo() + if testing: + scalatest = True + junit = True + specs2 = True + if specs2: + junit = True + + artifact_ids_to_fetch_sources = {} + + if scalatest: + artifact_ids_to_fetch_sources.update({ + id: True + for id in scalatest_artifact_ids() + }) + if junit: + artifact_ids_to_fetch_sources.update({ + id: True + for id in junit_artifact_ids() + }) + if specs2: + artifact_ids_to_fetch_sources.update({ + id: True + for id in specs2_artifact_ids() + specs2_junit_artifact_ids() + }) + + for scala_version in SCALA_VERSIONS: + version_specific_artifact_ids = {} + + all_artifacts = ( + artifact_ids_to_fetch_sources | version_specific_artifact_ids + ) + + repositories( + scala_version = scala_version, + for_artifact_ids = all_artifacts.keys(), + maven_servers = maven_servers, + fetch_sources = fetch_sources, + fetch_sources_by_id = all_artifacts, + overriden_artifacts = overridden_artifacts, + validate_scala_version = validate_scala_version, + ) + + scala_toolchains_repo( + scalatest = scalatest, + junit = junit, + specs2 = specs2, + testing = testing, + ) diff --git a/scala/private/macros/toolchains_repo.bzl b/scala/private/macros/toolchains_repo.bzl index 2ee1cb7df..6c977dcf7 100644 --- a/scala/private/macros/toolchains_repo.bzl +++ b/scala/private/macros/toolchains_repo.bzl @@ -1,5 +1,40 @@ """Repository rule to instantiate @io_bazel_rules_scala_toolchains""" +def _generate_testing_toolchain_build_file_args(repo_attr): + framework_deps = {} + + if repo_attr.testing: + framework_deps = { + "scalatest": "SCALATEST_DEPS", + "junit": "JUNIT_DEPS", + "specs2": "SPECS2_DEPS", + "specs2_junit": "SPECS2_JUNIT_DEPS", + } + if repo_attr.scalatest: + framework_deps["scalatest"] = "SCALATEST_DEPS" + if repo_attr.specs2: + framework_deps["specs2"] = "SPECS2_DEPS" + framework_deps["specs2_junit"] = "SPECS2_JUNIT_DEPS" + framework_deps["junit"] = "JUNIT_DEPS" + if repo_attr.junit: + framework_deps["junit"] = "JUNIT_DEPS" + + if len(framework_deps) == 0: + return None + + # The _TESTING_TOOLCHAIN_BUILD template expects that all framework keys are + # present in the dictionary, so it can set unset framework classpath + # parameters to `None`. + return { + "deps_symbols": "\",\n \"".join( + [s for s in framework_deps.values()], + ), + "scalatest": framework_deps.get("scalatest"), + "junit": framework_deps.get("junit"), + "specs2": framework_deps.get("specs2"), + "specs2_junit": framework_deps.get("specs2_junit"), + } + def _scala_toolchains_repo_impl(repository_ctx): repo_attr = repository_ctx.attr format_args = { @@ -10,6 +45,11 @@ def _scala_toolchains_repo_impl(repository_ctx): if repo_attr.scala: toolchains["scala"] = _SCALA_TOOLCHAIN_BUILD + testing_build_args = _generate_testing_toolchain_build_file_args(repo_attr) + if testing_build_args != None: + format_args.update(testing_build_args) + toolchains["testing"] = _TESTING_TOOLCHAIN_BUILD + if len(toolchains) == 0: fail("no toolchains specified") @@ -22,8 +62,13 @@ def _scala_toolchains_repo_impl(repository_ctx): _scala_toolchains_repo = repository_rule( implementation = _scala_toolchains_repo_impl, + doc = "Creates a repo containing Scala toolchain packages", attrs = { "scala": attr.bool(default = True), + "scalatest": attr.bool(), + "junit": attr.bool(), + "specs2": attr.bool(), + "testing": attr.bool(), }, ) @@ -73,3 +118,25 @@ load( ] ] """ + +_TESTING_TOOLCHAIN_BUILD = """ +load("@@{rules_scala_repo}//scala:scala.bzl", "setup_scala_testing_toolchain") +load("@@{rules_scala_repo}//scala:scala_cross_version.bzl", "version_suffix") +load( + "@@{rules_scala_repo}//testing:deps.bzl", + "{deps_symbols}", +) +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") + +[ + setup_scala_testing_toolchain( + name = "testing_toolchain" + version_suffix(scala_version), + scala_version = scala_version, + scalatest_classpath = {scalatest}, + junit_classpath = {junit}, + specs2_classpath = {specs2}, + specs2_junit_classpath = {specs2_junit}, + ) + for scala_version in SCALA_VERSIONS +] +""" diff --git a/scala/private/rules/scala_junit_test.bzl b/scala/private/rules/scala_junit_test.bzl index 9e01df5c8..c3fbc9aa2 100644 --- a/scala/private/rules/scala_junit_test.bzl +++ b/scala/private/rules/scala_junit_test.bzl @@ -2,15 +2,15 @@ load("@bazel_skylib//lib:dicts.bzl", _dicts = "dicts") load( - "@io_bazel_rules_scala//scala/private:common_attributes.bzl", + "//scala/private:common_attributes.bzl", "common_attrs", "implicit_deps", "launcher_template", ) -load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs") -load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "scala_version_transition", "toolchain_transition_attr") +load("//scala/private:common_outputs.bzl", "common_outputs") +load("//scala:scala_cross_version.bzl", "scala_version_transition", "toolchain_transition_attr") load( - "@io_bazel_rules_scala//scala/private:phases/phases.bzl", + "//scala/private:phases/phases.bzl", "extras_phases", "phase_collect_jars_junit_test", "phase_compile_junit_test", @@ -85,12 +85,10 @@ _scala_junit_test_attrs = { "env": attr.string_dict(default = {}), "env_inherit": attr.string_list(), "_junit_classpath": attr.label( - default = Label("@io_bazel_rules_scala//testing/toolchain:junit_classpath"), + default = Label("//testing/toolchain:junit_classpath"), ), "_bazel_test_runner": attr.label( - default = Label( - "@io_bazel_rules_scala//scala:bazel_test_runner_deploy", - ), + default = Label("//scala:bazel_test_runner_deploy"), allow_files = True, ), "_lcov_merger": attr.label( @@ -103,10 +101,8 @@ _scala_junit_test_attrs = { _junit_resolve_deps = { "_scala_toolchain": attr.label_list( default = [ - Label( - "@io_bazel_rules_scala//scala/private/toolchain_deps:scala_library_classpath", - ), - Label("@io_bazel_rules_scala//testing/toolchain:junit_classpath"), + Label("//scala/private/toolchain_deps:scala_library_classpath"), + Label("//testing/toolchain:junit_classpath"), ], allow_files = False, ), @@ -140,7 +136,8 @@ def make_scala_junit_test(*extras): ), test = True, toolchains = [ - "@io_bazel_rules_scala//scala:toolchain_type", + Label("//scala:toolchain_type"), + Label("//testing/toolchain:testing_toolchain_type"), "@bazel_tools//tools/jdk:toolchain_type", ], cfg = scala_version_transition, diff --git a/scala/private/rules/scala_test.bzl b/scala/private/rules/scala_test.bzl index 4f589cb1f..1fe3992a8 100644 --- a/scala/private/rules/scala_test.bzl +++ b/scala/private/rules/scala_test.bzl @@ -126,7 +126,8 @@ def make_scala_test(*extras): ), test = True, toolchains = [ - "@io_bazel_rules_scala//scala:toolchain_type", + Label("//scala:toolchain_type"), + Label("//testing/toolchain:testing_toolchain_type"), "@bazel_tools//tools/jdk:toolchain_type", ], cfg = scala_version_transition, diff --git a/scala/scala_import.bzl b/scala/scala_import.bzl index 1fb01df8f..3190349fa 100644 --- a/scala/scala_import.bzl +++ b/scala/scala_import.bzl @@ -1,9 +1,6 @@ -load("@io_bazel_rules_scala//scala:jars_to_labels.bzl", "JarsToLabelsInfo") +load("//scala/private:rule_impls.bzl", "specified_java_compile_toolchain") load("//scala/settings:stamp_settings.bzl", "StampScalaImport") -load( - "@io_bazel_rules_scala//scala/private:rule_impls.bzl", - "specified_java_compile_toolchain", -) +load("//scala:jars_to_labels.bzl", "JarsToLabelsInfo") def _stamp_jar(ctx, jar): stamped_jar_filename = "%s.stamp/%s" % (ctx.label.name, jar.basename) @@ -139,11 +136,13 @@ scala_import = rule( "srcjar": attr.label(allow_single_file = True), "_placeholder_jar": attr.label( allow_single_file = True, - default = Label("@io_bazel_rules_scala//scala:libPlaceHolderClassToCreateEmptyJarForScalaImport.jar"), + default = Label( + "//scala:libPlaceHolderClassToCreateEmptyJarForScalaImport.jar", + ), ), "stamp": attr.label( doc = "Adds Target-Label attribute to MANIFEST.MF for dep tracking", - default = Label("@io_bazel_rules_scala//scala/settings:stamp_scala_import"), + default = Label("//scala/settings:stamp_scala_import"), ), "java_compile_toolchain": attr.label( default = Label("@bazel_tools//tools/jdk:current_java_toolchain"), diff --git a/scalatest/scalatest.bzl b/scalatest/scalatest.bzl index 0449a1a07..6af1e7a6a 100644 --- a/scalatest/scalatest.bzl +++ b/scalatest/scalatest.bzl @@ -5,30 +5,33 @@ load( load("//third_party/repositories:repositories.bzl", "repositories") load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") +def scalatest_artifact_ids(): + return [ + "io_bazel_rules_scala_scalactic", + "io_bazel_rules_scala_scalatest", + "io_bazel_rules_scala_scalatest_compatible", + "io_bazel_rules_scala_scalatest_core", + "io_bazel_rules_scala_scalatest_diagrams", + "io_bazel_rules_scala_scalatest_featurespec", + "io_bazel_rules_scala_scalatest_flatspec", + "io_bazel_rules_scala_scalatest_freespec", + "io_bazel_rules_scala_scalatest_funspec", + "io_bazel_rules_scala_scalatest_funsuite", + "io_bazel_rules_scala_scalatest_matchers_core", + "io_bazel_rules_scala_scalatest_mustmatchers", + "io_bazel_rules_scala_scalatest_propspec", + "io_bazel_rules_scala_scalatest_refspec", + "io_bazel_rules_scala_scalatest_shouldmatchers", + "io_bazel_rules_scala_scalatest_wordspec", + ] + def scalatest_repositories( maven_servers = _default_maven_server_urls(), fetch_sources = True): for scala_version in SCALA_VERSIONS: repositories( scala_version = scala_version, - for_artifact_ids = [ - "io_bazel_rules_scala_scalactic", - "io_bazel_rules_scala_scalatest", - "io_bazel_rules_scala_scalatest_compatible", - "io_bazel_rules_scala_scalatest_core", - "io_bazel_rules_scala_scalatest_diagrams", - "io_bazel_rules_scala_scalatest_featurespec", - "io_bazel_rules_scala_scalatest_flatspec", - "io_bazel_rules_scala_scalatest_freespec", - "io_bazel_rules_scala_scalatest_funspec", - "io_bazel_rules_scala_scalatest_funsuite", - "io_bazel_rules_scala_scalatest_matchers_core", - "io_bazel_rules_scala_scalatest_mustmatchers", - "io_bazel_rules_scala_scalatest_propspec", - "io_bazel_rules_scala_scalatest_refspec", - "io_bazel_rules_scala_scalatest_shouldmatchers", - "io_bazel_rules_scala_scalatest_wordspec", - ], + for_artifact_ids = scalatest_artifact_ids(), maven_servers = maven_servers, fetch_sources = fetch_sources, ) diff --git a/specs2/specs2.bzl b/specs2/specs2.bzl index 12ba6b40c..43f8fc80a 100644 --- a/specs2/specs2.bzl +++ b/specs2/specs2.bzl @@ -7,20 +7,23 @@ load("//third_party/repositories:repositories.bzl", "repositories") def specs2_version(): return "4.4.1" +def specs2_artifact_ids(): + return [ + "io_bazel_rules_scala_org_specs2_specs2_common", + "io_bazel_rules_scala_org_specs2_specs2_core", + "io_bazel_rules_scala_org_specs2_specs2_fp", + "io_bazel_rules_scala_org_specs2_specs2_matcher", + ] + def specs2_repositories( maven_servers = _default_maven_server_urls(), overriden_artifacts = {}): repositories( - for_artifact_ids = [ - "io_bazel_rules_scala_org_specs2_specs2_common", - "io_bazel_rules_scala_org_specs2_specs2_core", - "io_bazel_rules_scala_org_specs2_specs2_fp", - "io_bazel_rules_scala_org_specs2_specs2_matcher", - ], + for_artifact_ids = specs2_artifact_ids(), maven_servers = maven_servers, fetch_sources = True, overriden_artifacts = overriden_artifacts, ) def specs2_dependencies(): - return ["@io_bazel_rules_scala//specs2:specs2"] + return [Label("//specs2:specs2")] diff --git a/specs2/specs2_junit.bzl b/specs2/specs2_junit.bzl index 2c2e0d4a0..d6fbf53da 100644 --- a/specs2/specs2_junit.bzl +++ b/specs2/specs2_junit.bzl @@ -10,6 +10,11 @@ load( ) load("//third_party/repositories:repositories.bzl", "repositories") +def specs2_junit_artifact_ids(): + return [ + "io_bazel_rules_scala_org_specs2_specs2_junit", + ] + def specs2_junit_repositories( maven_servers = _default_maven_server_urls(), overriden_artifacts = {}): @@ -17,9 +22,7 @@ def specs2_junit_repositories( junit_repositories() repositories( - for_artifact_ids = [ - "io_bazel_rules_scala_org_specs2_specs2_junit", - ], + for_artifact_ids = specs2_junit_artifact_ids(), maven_servers = maven_servers, fetch_sources = True, overriden_artifacts = overriden_artifacts, @@ -27,5 +30,5 @@ def specs2_junit_repositories( def specs2_junit_dependencies(): return specs2_dependencies() + [ - "@io_bazel_rules_scala//testing/toolchain:specs2_junit_classpath", + Label("//testing/toolchain:specs2_junit_classpath"), ] diff --git a/test_cross_build/WORKSPACE b/test_cross_build/WORKSPACE index 5e33f222c..6aa0c6473 100644 --- a/test_cross_build/WORKSPACE +++ b/test_cross_build/WORKSPACE @@ -74,16 +74,12 @@ scala_config( # loads other rules Rules Scala depends on load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains() +scala_toolchains( + scalatest = True, +) register_toolchains("@io_bazel_rules_scala_toolchains//...:all") -load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain") - -scalatest_repositories() - -scalatest_toolchain() - load("@io_bazel_rules_scala//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_repositories") scalafmt_repositories() diff --git a/test_version/WORKSPACE.template b/test_version/WORKSPACE.template index 280ce826c..c16c9ff68 100644 --- a/test_version/WORKSPACE.template +++ b/test_version/WORKSPACE.template @@ -61,7 +61,11 @@ load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "extract_major_vers load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains(fetch_sources = True) +scala_toolchains( + fetch_sources = True, + scalatest = True, + specs2 = True, +) register_toolchains( "@io_bazel_rules_scala//scala:unused_dependency_checker_error_toolchain", @@ -81,11 +85,3 @@ scala_proto_repositories() load("@io_bazel_rules_scala//scala_proto:toolchains.bzl", "scala_proto_register_toolchains") scala_proto_register_toolchains() - -load("@io_bazel_rules_scala//specs2:specs2_junit.bzl", "specs2_junit_repositories") - -specs2_junit_repositories() - -load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain") - -scalatest_repositories() diff --git a/testing/BUILD b/testing/BUILD index 0592dcfa4..6bc5efff8 100644 --- a/testing/BUILD +++ b/testing/BUILD @@ -1,82 +1,19 @@ -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") load("//scala:scala_cross_version.bzl", "version_suffix") -load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION", "SCALA_VERSIONS") - -_SPECS2_DEPS = [ - "@io_bazel_rules_scala_org_specs2_specs2_common", - "@io_bazel_rules_scala_org_specs2_specs2_core", - "@io_bazel_rules_scala_org_specs2_specs2_fp", - "@io_bazel_rules_scala_org_specs2_specs2_matcher", -] - -_SPECS2_JUNIT_DEPS = [ - "@io_bazel_rules_scala_org_specs2_specs2_junit", -] - -_JUNIT_DEPS = [ - "@io_bazel_rules_scala_junit_junit", - "@io_bazel_rules_scala_org_hamcrest_hamcrest_core", -] - -_SCALATEST_DEPS = [ - "@io_bazel_rules_scala_scalactic", - "@io_bazel_rules_scala_scalatest", - "@io_bazel_rules_scala_scalatest_compatible", - "@io_bazel_rules_scala_scalatest_core", - "@io_bazel_rules_scala_scalatest_featurespec", - "@io_bazel_rules_scala_scalatest_flatspec", - "@io_bazel_rules_scala_scalatest_freespec", - "@io_bazel_rules_scala_scalatest_funspec", - "@io_bazel_rules_scala_scalatest_funsuite", - "@io_bazel_rules_scala_scalatest_matchers_core", - "@io_bazel_rules_scala_scalatest_mustmatchers", - "@io_bazel_rules_scala_scalatest_shouldmatchers", -] - -[ - setup_scala_testing_toolchain( - name = "testing_toolchain" + version_suffix(scala_version), - junit_classpath = _JUNIT_DEPS, - scala_version = scala_version, - scalatest_classpath = [dep + version_suffix(scala_version) for dep in _SCALATEST_DEPS], - specs2_classpath = _SPECS2_DEPS, - specs2_junit_classpath = _SPECS2_JUNIT_DEPS, - visibility = ["//visibility:public"], - ) - for scala_version in SCALA_VERSIONS -] +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION") +# Aliases for backward compatibility: [ - setup_scala_testing_toolchain( - name = "scalatest_toolchain" + version_suffix(scala_version), - scala_version = scala_version, - scalatest_classpath = [dep + version_suffix(scala_version) for dep in _SCALATEST_DEPS], - visibility = ["//visibility:public"], + alias( + name = framework + "_toolchain", + actual = ( + "@io_bazel_rules_scala_toolchains//testing:testing_toolchain" + + version_suffix(SCALA_VERSION) + ), ) - for scala_version in SCALA_VERSIONS + for framework in [ + "testing", + "scalatest", + "specs2_junit", + "junit", + ] ] - -# Aliases for backward compatibility: -alias( - name = "testing_toolchain", - actual = "testing_toolchain" + version_suffix(SCALA_VERSION), -) - -alias( - name = "scalatest_toolchain", - actual = "scalatest_toolchain" + version_suffix(SCALA_VERSION), -) - -setup_scala_testing_toolchain( - name = "specs2_junit_toolchain", - junit_classpath = _JUNIT_DEPS, - specs2_classpath = _SPECS2_DEPS, - specs2_junit_classpath = _SPECS2_JUNIT_DEPS, - visibility = ["//visibility:public"], -) - -setup_scala_testing_toolchain( - name = "junit_toolchain", - junit_classpath = _JUNIT_DEPS, - visibility = ["//visibility:public"], -) diff --git a/testing/deps.bzl b/testing/deps.bzl new file mode 100644 index 000000000..87951e3d8 --- /dev/null +++ b/testing/deps.bzl @@ -0,0 +1,15 @@ +load("//junit:junit.bzl", "junit_artifact_ids") +load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") +load("//specs2:specs2.bzl", "specs2_artifact_ids") +load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids") + +def _repoize(ids): + return ["@" + id for id in ids] + +JUNIT_DEPS = _repoize(junit_artifact_ids()) + +SCALATEST_DEPS = _repoize(scalatest_artifact_ids()) + +SPECS2_DEPS = _repoize(specs2_artifact_ids()) + +SPECS2_JUNIT_DEPS = _repoize(specs2_junit_artifact_ids()) diff --git a/testing/scalatest.bzl b/testing/scalatest.bzl index 127b5059c..3260aa457 100644 --- a/testing/scalatest.bzl +++ b/testing/scalatest.bzl @@ -7,4 +7,7 @@ def scalatest_repositories(): def scalatest_toolchain(): for scala_version in SCALA_VERSIONS: - native.register_toolchains("@io_bazel_rules_scala//testing:scalatest_toolchain" + version_suffix(scala_version)) + native.register_toolchains(str(Label( + "//testing:scalatest_toolchain" + + version_suffix(scala_version), + ))) diff --git a/testing/testing.bzl b/testing/testing.bzl index d857ca15d..ddb61d5be 100644 --- a/testing/testing.bzl +++ b/testing/testing.bzl @@ -1,8 +1,15 @@ -load("@io_bazel_rules_scala//scala:providers.bzl", "declare_deps_provider") -load("@io_bazel_rules_scala//testing/toolchain:toolchain.bzl", "scala_testing_toolchain") -load("//scala:scala_cross_version.bzl", "version_suffix") +load("//scala:providers.bzl", "declare_deps_provider") +load("//scala:scala_cross_version.bzl", "repositories", "version_suffix") +load("//testing/toolchain:toolchain.bzl", "scala_testing_toolchain") load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION") +DEP_PROVIDERS = [ + "junit_classpath", + "scalatest_classpath", + "specs2_classpath", + "specs2_junit_classpath", +] + def _declare_deps_provider(macro_name, deps_id, deps, visibility): label = "%s_%s_provider" % (macro_name, deps_id) declare_deps_provider( @@ -11,7 +18,7 @@ def _declare_deps_provider(macro_name, deps_id, deps, visibility): visibility = visibility, deps = deps, ) - return label + return ":%s" % label def setup_scala_testing_toolchain( name, @@ -28,7 +35,7 @@ def setup_scala_testing_toolchain( _declare_deps_provider( name, "junit_classpath", - junit_classpath, + repositories(scala_version, junit_classpath), visibility, ), ) @@ -38,7 +45,7 @@ def setup_scala_testing_toolchain( _declare_deps_provider( name, "specs2_junit_classpath", - specs2_junit_classpath, + repositories(scala_version, specs2_junit_classpath), visibility, ), ) @@ -48,7 +55,7 @@ def setup_scala_testing_toolchain( _declare_deps_provider( name, "specs2_classpath", - specs2_classpath, + repositories(scala_version, specs2_classpath), visibility, ), ) @@ -58,7 +65,7 @@ def setup_scala_testing_toolchain( _declare_deps_provider( name, "scalatest_classpath", - scalatest_classpath, + repositories(scala_version, scalatest_classpath), visibility, ), ) @@ -72,7 +79,12 @@ def setup_scala_testing_toolchain( native.toolchain( name = name, toolchain = ":" + name + "_impl", - toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type", - target_settings = ["@io_bazel_rules_scala_config//:scala_version" + version_suffix(scala_version)], + toolchain_type = Label("//testing/toolchain:testing_toolchain_type"), + target_settings = [ + Label( + "@io_bazel_rules_scala_config//:scala_version" + + version_suffix(scala_version), + ), + ], visibility = visibility, ) diff --git a/testing/toolchain/BUILD b/testing/toolchain/BUILD index 20a89bbc8..e76e201d2 100644 --- a/testing/toolchain/BUILD +++ b/testing/toolchain/BUILD @@ -1,30 +1,16 @@ -load("@io_bazel_rules_scala//testing/toolchain:toolchain_deps.bzl", "testing_toolchain_deps") +load(":toolchain_deps.bzl", "testing_toolchain_deps") +load("//testing:testing.bzl", "DEP_PROVIDERS") toolchain_type( name = "testing_toolchain_type", visibility = ["//visibility:public"], ) -testing_toolchain_deps( - name = "junit_classpath", - deps_id = "junit_classpath", - visibility = ["//visibility:public"], -) - -testing_toolchain_deps( - name = "scalatest_classpath", - deps_id = "scalatest_classpath", - visibility = ["//visibility:public"], -) - -testing_toolchain_deps( - name = "specs2_classpath", - deps_id = "specs2_classpath", - visibility = ["//visibility:public"], -) - -testing_toolchain_deps( - name = "specs2_junit_classpath", - deps_id = "specs2_junit_classpath", - visibility = ["//visibility:public"], -) +[ + testing_toolchain_deps( + name = dep, + deps_id = dep, + visibility = ["//visibility:public"], + ) + for dep in DEP_PROVIDERS +] diff --git a/testing/toolchain/toolchain.bzl b/testing/toolchain/toolchain.bzl index 83571c058..cbe77a9f6 100644 --- a/testing/toolchain/toolchain.bzl +++ b/testing/toolchain/toolchain.bzl @@ -10,8 +10,6 @@ scala_testing_toolchain = rule( _scala_toolchain_impl, attrs = { "dep_providers": attr.label_list( - default = [ - ], providers = [_DepsInfo], ), }, diff --git a/testing/toolchain/toolchain_deps.bzl b/testing/toolchain/toolchain_deps.bzl index 340cc669a..0cb992de1 100644 --- a/testing/toolchain/toolchain_deps.bzl +++ b/testing/toolchain/toolchain_deps.bzl @@ -3,7 +3,7 @@ load( "expose_toolchain_deps", ) -_toolchain_type = "@io_bazel_rules_scala//testing/toolchain:testing_toolchain_type" +_toolchain_type = Label("//testing/toolchain:testing_toolchain_type") def _testing_toolchain_deps(ctx): return expose_toolchain_deps(ctx, _toolchain_type) diff --git a/third_party/repositories/repositories.bzl b/third_party/repositories/repositories.bzl index 8d00dd926..aa76d6f5f 100644 --- a/third_party/repositories/repositories.bzl +++ b/third_party/repositories/repositories.bzl @@ -85,6 +85,7 @@ def repositories( maven_servers = default_maven_server_urls(), overriden_artifacts = {}, fetch_sources = True, + fetch_sources_by_id = {}, validate_scala_version = False): """ Downloads given artifacts. @@ -128,7 +129,7 @@ def repositories( for dep in artifacts[id].get("runtime_deps", []) ], testonly_ = artifacts[id].get("testonly", False), - fetch_sources = fetch_sources, + fetch_sources = fetch_sources_by_id.get(id, fetch_sources), ) # For backward compatibility: non-suffixed repo pointing to the suffixed one, diff --git a/third_party/test/example_external_workspace/WORKSPACE b/third_party/test/example_external_workspace/WORKSPACE index 0e69d76b2..03b524665 100644 --- a/third_party/test/example_external_workspace/WORKSPACE +++ b/third_party/test/example_external_workspace/WORKSPACE @@ -33,7 +33,10 @@ scala_config() load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") -scala_toolchains(fetch_sources = True) +scala_toolchains( + fetch_sources = True, + scalatest = True, +) register_toolchains("@io_bazel_rules_scala_toolchains//...:all") @@ -52,9 +55,3 @@ rules_proto_toolchains() load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") protobuf_deps() - -load("@io_bazel_rules_scala//testing:scalatest.bzl", "scalatest_repositories", "scalatest_toolchain") - -scalatest_repositories() - -scalatest_toolchain() From 90e66e67d6bb584331c074c3daebaf8347b48364 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Sun, 1 Dec 2024 12:19:35 -0500 Subject: [PATCH 2/5] Move `scala_toolchains` to `scala/toolchains.bzl` Removes this symbol from `scala/scala.bzl` as well as `setup_scala_testing_toolchain`, and deletes `scala/private/macros/toolchains.bzl`. Part of #1482 and #1652. This is required for Bazel 8 and `rules_java` 8 compatibility, but is also compatible with Bazel 6 and 7. In #1652, @hvadehra suggested partitioning the `.bzl` files such that `WORKSPACE` doesn't `load` a file that tries to `load` symbols from `rules_java`. I successfully did so in a separate branch, and along with other minor changes, got `rules_scala` to build with `rules_java` 8.5.1. The other changes will come in separate pull requests, but it makes sense to land this change now before adding any other toolchains to `scala_toolchains`. --- Arguably, we should remove all macros exported from `scala/scala.bzl` that only instantiate toolchain dependencies and define toolchains. That may be a breaking change for some users, but will ultimately be necessary for these macros to remain compatible with Bazel 8. --- WORKSPACE | 2 +- docs/testing.md | 22 ++- dt_patches/test_dt_patches/WORKSPACE | 2 +- .../test_dt_patches_user_srcjar/WORKSPACE | 2 +- examples/crossbuild/WORKSPACE | 2 +- examples/scala3/WORKSPACE | 2 +- examples/semanticdb/WORKSPACE | 2 +- .../testing/multi_frameworks_toolchain/BUILD | 5 +- .../multi_frameworks_toolchain/WORKSPACE | 2 +- .../testing/scalatest_repositories/WORKSPACE | 2 +- .../specs2_junit_repositories/WORKSPACE | 2 +- scala/private/macros/toolchains.bzl | 128 ----------------- scala/private/macros/toolchains_repo.bzl | 5 +- scala/scala.bzl | 10 -- scala/toolchains.bzl | 129 ++++++++++++++++++ test_cross_build/WORKSPACE | 2 +- test_version/WORKSPACE.template | 2 +- .../test/example_external_workspace/WORKSPACE | 2 +- third_party/test/proto/WORKSPACE | 2 +- 19 files changed, 167 insertions(+), 158 deletions(-) delete mode 100644 scala/private/macros/toolchains.bzl diff --git a/WORKSPACE b/WORKSPACE index fba94c959..6bf5d9ace 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -30,7 +30,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(enable_compiler_dependency_tracking = True) -load("//scala:scala.bzl", "scala_toolchains") +load("//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/docs/testing.md b/docs/testing.md index e620a45b8..20a0909b7 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -31,7 +31,10 @@ Test framework dependencies are configured via testing toolchain. For convenienc `setup_scala_testing_toolchain` can be used to define such toolchains. ```starlark -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) ``` Attributes @@ -53,7 +56,10 @@ Examples (assumes maven deps are managed with rules_jvm_external): ```starlark # BUILD -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "scalatest_toolchain", @@ -77,7 +83,10 @@ register_toolchains('//:scalatest_toolchain') #### JUnit 4 ```starlark # BUILD -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "junit_toolchain", @@ -98,7 +107,10 @@ For Specs2 rules to work, `junit_classpath`, `specs2_junit_classpath` and `specs be configured. ```starlark # BUILD -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "specs2_toolchain", @@ -122,4 +134,4 @@ Register the toolchain ```starlark # WORKSPACE register_toolchains('//:specs2_toolchain') -``` \ No newline at end of file +``` diff --git a/dt_patches/test_dt_patches/WORKSPACE b/dt_patches/test_dt_patches/WORKSPACE index 7921dc838..a6e17702f 100644 --- a/dt_patches/test_dt_patches/WORKSPACE +++ b/dt_patches/test_dt_patches/WORKSPACE @@ -40,7 +40,7 @@ load("@compiler_sources//:extensions.bzl", "import_compiler_source_repos") import_compiler_source_repos() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/dt_patches/test_dt_patches_user_srcjar/WORKSPACE b/dt_patches/test_dt_patches_user_srcjar/WORKSPACE index b98e2f99c..3576c9b90 100644 --- a/dt_patches/test_dt_patches_user_srcjar/WORKSPACE +++ b/dt_patches/test_dt_patches_user_srcjar/WORKSPACE @@ -113,7 +113,7 @@ srcjars_by_version = { }, } -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/examples/crossbuild/WORKSPACE b/examples/crossbuild/WORKSPACE index d61c31e81..182bc8837 100644 --- a/examples/crossbuild/WORKSPACE +++ b/examples/crossbuild/WORKSPACE @@ -38,7 +38,7 @@ scala_config( ], ) -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains(scalatest = True) diff --git a/examples/scala3/WORKSPACE b/examples/scala3/WORKSPACE index 72c37380c..aff2d2a5c 100644 --- a/examples/scala3/WORKSPACE +++ b/examples/scala3/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(scala_version = "3.6.2") -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains(fetch_sources = True) diff --git a/examples/semanticdb/WORKSPACE b/examples/semanticdb/WORKSPACE index 72ae37bd5..0e99dfe95 100644 --- a/examples/semanticdb/WORKSPACE +++ b/examples/semanticdb/WORKSPACE @@ -34,7 +34,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config(scala_version = "2.13.15") -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains(fetch_sources = True) diff --git a/examples/testing/multi_frameworks_toolchain/BUILD b/examples/testing/multi_frameworks_toolchain/BUILD index b948c8ee2..27eedcf7e 100644 --- a/examples/testing/multi_frameworks_toolchain/BUILD +++ b/examples/testing/multi_frameworks_toolchain/BUILD @@ -1,4 +1,7 @@ -load("@io_bazel_rules_scala//scala:scala.bzl", "setup_scala_testing_toolchain") +load( + "@io_bazel_rules_scala//testing:testing.bzl", + "setup_scala_testing_toolchain", +) setup_scala_testing_toolchain( name = "testing_toolchain", diff --git a/examples/testing/multi_frameworks_toolchain/WORKSPACE b/examples/testing/multi_frameworks_toolchain/WORKSPACE index e0e72888d..631722cac 100644 --- a/examples/testing/multi_frameworks_toolchain/WORKSPACE +++ b/examples/testing/multi_frameworks_toolchain/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/examples/testing/scalatest_repositories/WORKSPACE b/examples/testing/scalatest_repositories/WORKSPACE index 648fa7f61..29b598730 100644 --- a/examples/testing/scalatest_repositories/WORKSPACE +++ b/examples/testing/scalatest_repositories/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/examples/testing/specs2_junit_repositories/WORKSPACE b/examples/testing/specs2_junit_repositories/WORKSPACE index 7a4ec73ee..5a1c4012c 100644 --- a/examples/testing/specs2_junit_repositories/WORKSPACE +++ b/examples/testing/specs2_junit_repositories/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/scala/private/macros/toolchains.bzl b/scala/private/macros/toolchains.bzl deleted file mode 100644 index 3a851ae22..000000000 --- a/scala/private/macros/toolchains.bzl +++ /dev/null @@ -1,128 +0,0 @@ -"""Macro to instantiate @io_bazel_rules_scala_toolchains""" - -load(":macros/toolchains_repo.bzl", "scala_toolchains_repo") -load("//junit:junit.bzl", "junit_artifact_ids") -load("//scala/private:macros/scala_repositories.bzl", "scala_repositories") -load("//scala:scala_cross_version.bzl", "default_maven_server_urls") -load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") -load("//specs2:specs2.bzl", "specs2_artifact_ids") -load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids") -load("//third_party/repositories:repositories.bzl", "repositories") -load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") - -def scala_toolchains( - maven_servers = default_maven_server_urls(), - overridden_artifacts = {}, - load_rules_scala_dependencies = True, - load_scala_toolchain_dependencies = True, - fetch_sources = False, - validate_scala_version = True, - scala_compiler_srcjars = {}, - scalatest = False, - junit = False, - specs2 = False, - testing = False): - """Instantiates @io_bazel_rules_scala_toolchains and all its dependencies. - - Provides a unified interface to configuring rules_scala both directly in a - `WORKSPACE` file and in a Bazel module extension. - - Instantiates the `@io_bazel_rules_scala_toolchains` repository. Under - `WORKSPACE`, you will need to call `register_toolchains` at some point. - Under Bzlmod, rules_scala does this automatically. - - ```starlark - register_toolchains("@io_bazel_rules_scala_toolchains//...:all") - ``` - - All arguments are optional. - - Args: - maven_servers: Maven servers used to fetch dependency jar files - overridden_artifacts: specific dependency jar files to use instead of - those from `maven_servers`, in the format: - ```starlark - "repo_name": { - "artifact": "", - "sha256": "", - "deps": [ - "repository_names_of_dependencies", - ], - } - ``` - load_rules_scala_dependencies: whether load rules_scala repository - dependencies - load_scala_toolchain_dependencies: whether to load repository - dependencies of the core Scala language toolchain - fetch_sources: whether to download dependency source jars - validate_scala_version: whether to check if the configured Scala version - matches the default version supported by rules_scala - scala_compiler_srcjars: optional dictionary of Scala version string to - compiler srcjar metadata dictionaries containing: - - exactly one "label", "url", or "urls" key - - optional "integrity" or "sha256" keys - scalatest: whether to instantiate the Scalatest toolchain - junit: whether to instantiate the JUnit toolchain - specs2: whether to instantiate the Specs2 JUnit toolchain - testing: whether to instantiate the Scalatest, JUnit, and Specs2 JUnit - toolchains combined - """ - scala_repositories( - maven_servers = maven_servers, - # Note the internal macro parameter misspells "overriden". - overriden_artifacts = overridden_artifacts, - load_dep_rules = load_rules_scala_dependencies, - load_jar_deps = load_scala_toolchain_dependencies, - fetch_sources = fetch_sources, - validate_scala_version = validate_scala_version, - scala_compiler_srcjars = scala_compiler_srcjars, - ) - - if testing: - scalatest = True - junit = True - specs2 = True - if specs2: - junit = True - - artifact_ids_to_fetch_sources = {} - - if scalatest: - artifact_ids_to_fetch_sources.update({ - id: True - for id in scalatest_artifact_ids() - }) - if junit: - artifact_ids_to_fetch_sources.update({ - id: True - for id in junit_artifact_ids() - }) - if specs2: - artifact_ids_to_fetch_sources.update({ - id: True - for id in specs2_artifact_ids() + specs2_junit_artifact_ids() - }) - - for scala_version in SCALA_VERSIONS: - version_specific_artifact_ids = {} - - all_artifacts = ( - artifact_ids_to_fetch_sources | version_specific_artifact_ids - ) - - repositories( - scala_version = scala_version, - for_artifact_ids = all_artifacts.keys(), - maven_servers = maven_servers, - fetch_sources = fetch_sources, - fetch_sources_by_id = all_artifacts, - overriden_artifacts = overridden_artifacts, - validate_scala_version = validate_scala_version, - ) - - scala_toolchains_repo( - scalatest = scalatest, - junit = junit, - specs2 = specs2, - testing = testing, - ) diff --git a/scala/private/macros/toolchains_repo.bzl b/scala/private/macros/toolchains_repo.bzl index 6c977dcf7..8234d5c94 100644 --- a/scala/private/macros/toolchains_repo.bzl +++ b/scala/private/macros/toolchains_repo.bzl @@ -120,12 +120,15 @@ load( """ _TESTING_TOOLCHAIN_BUILD = """ -load("@@{rules_scala_repo}//scala:scala.bzl", "setup_scala_testing_toolchain") load("@@{rules_scala_repo}//scala:scala_cross_version.bzl", "version_suffix") load( "@@{rules_scala_repo}//testing:deps.bzl", "{deps_symbols}", ) +load( + "@@{rules_scala_repo}//testing:testing.bzl", + "setup_scala_testing_toolchain", +) load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") [ diff --git a/scala/scala.bzl b/scala/scala.bzl index 54588ecc8..3e8d5187e 100644 --- a/scala/scala.bzl +++ b/scala/scala.bzl @@ -2,10 +2,6 @@ load( "//specs2:specs2_junit.bzl", _specs2_junit_dependencies = "specs2_junit_dependencies", ) -load( - "//scala/private:macros/toolchains.bzl", - _scala_toolchains = "scala_toolchains", -) load( "//scala/private:macros/toolchains_repo.bzl", _scala_toolchains_repo = "scala_toolchains_repo", @@ -50,10 +46,6 @@ load( _scala_test = "scala_test", _scala_test_suite = "scala_test_suite", ) -load( - "//testing:testing.bzl", - _setup_scala_testing_toolchain = "setup_scala_testing_toolchain", -) def scala_specs2_junit_test(name, **kwargs): _scala_junit_test( @@ -87,7 +79,5 @@ rules_scala_setup = _rules_scala_setup rules_scala_toolchain_deps_repositories = _rules_scala_toolchain_deps_repositories scala_test = _scala_test scala_test_suite = _scala_test_suite -setup_scala_testing_toolchain = _setup_scala_testing_toolchain setup_scala_toolchain = _setup_scala_toolchain -scala_toolchains = _scala_toolchains scala_toolchains_repo = _scala_toolchains_repo diff --git a/scala/toolchains.bzl b/scala/toolchains.bzl index eebdb7453..a3a14e1e9 100644 --- a/scala/toolchains.bzl +++ b/scala/toolchains.bzl @@ -1,3 +1,132 @@ +"""Macros to instantiate and register @io_bazel_rules_scala_toolchains""" + +load("//junit:junit.bzl", "junit_artifact_ids") +load("//scala/private:macros/scala_repositories.bzl", "scala_repositories") +load("//scala/private:macros/toolchains_repo.bzl", "scala_toolchains_repo") +load("//scala:scala_cross_version.bzl", "default_maven_server_urls") +load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") +load("//specs2:specs2.bzl", "specs2_artifact_ids") +load("//specs2:specs2_junit.bzl", "specs2_junit_artifact_ids") +load("//third_party/repositories:repositories.bzl", "repositories") +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") + +def scala_toolchains( + maven_servers = default_maven_server_urls(), + overridden_artifacts = {}, + load_rules_scala_dependencies = True, + load_scala_toolchain_dependencies = True, + fetch_sources = False, + validate_scala_version = True, + scala_compiler_srcjars = {}, + scalatest = False, + junit = False, + specs2 = False, + testing = False): + """Instantiates @io_bazel_rules_scala_toolchains and all its dependencies. + + Provides a unified interface to configuring rules_scala both directly in a + `WORKSPACE` file and in a Bazel module extension. + + Instantiates the `@io_bazel_rules_scala_toolchains` repository. Under + `WORKSPACE`, you will need to call `register_toolchains` at some point. + Under Bzlmod, rules_scala does this automatically. + + ```starlark + register_toolchains("@io_bazel_rules_scala_toolchains//...:all") + ``` + + All arguments are optional. + + Args: + maven_servers: Maven servers used to fetch dependency jar files + overridden_artifacts: specific dependency jar files to use instead of + those from `maven_servers`, in the format: + ```starlark + "repo_name": { + "artifact": "", + "sha256": "", + "deps": [ + "repository_names_of_dependencies", + ], + } + ``` + load_rules_scala_dependencies: whether load rules_scala repository + dependencies + load_scala_toolchain_dependencies: whether to load repository + dependencies of the core Scala language toolchain + fetch_sources: whether to download dependency source jars + validate_scala_version: whether to check if the configured Scala version + matches the default version supported by rules_scala + scala_compiler_srcjars: optional dictionary of Scala version string to + compiler srcjar metadata dictionaries containing: + - exactly one "label", "url", or "urls" key + - optional "integrity" or "sha256" keys + scalatest: whether to instantiate the Scalatest toolchain + junit: whether to instantiate the JUnit toolchain + specs2: whether to instantiate the Specs2 JUnit toolchain + testing: whether to instantiate the Scalatest, JUnit, and Specs2 JUnit + toolchains combined + """ + scala_repositories( + maven_servers = maven_servers, + # Note the internal macro parameter misspells "overriden". + overriden_artifacts = overridden_artifacts, + load_dep_rules = load_rules_scala_dependencies, + load_jar_deps = load_scala_toolchain_dependencies, + fetch_sources = fetch_sources, + validate_scala_version = validate_scala_version, + scala_compiler_srcjars = scala_compiler_srcjars, + ) + + if testing: + scalatest = True + junit = True + specs2 = True + if specs2: + junit = True + + artifact_ids_to_fetch_sources = {} + + if scalatest: + artifact_ids_to_fetch_sources.update({ + id: True + for id in scalatest_artifact_ids() + }) + if junit: + artifact_ids_to_fetch_sources.update({ + id: True + for id in junit_artifact_ids() + }) + if specs2: + artifact_ids_to_fetch_sources.update({ + id: True + for id in specs2_artifact_ids() + specs2_junit_artifact_ids() + }) + + for scala_version in SCALA_VERSIONS: + version_specific_artifact_ids = {} + + all_artifacts = ( + artifact_ids_to_fetch_sources | version_specific_artifact_ids + ) + + repositories( + scala_version = scala_version, + for_artifact_ids = all_artifacts.keys(), + maven_servers = maven_servers, + fetch_sources = fetch_sources, + fetch_sources_by_id = all_artifacts, + overriden_artifacts = overridden_artifacts, + validate_scala_version = validate_scala_version, + ) + + scala_toolchains_repo( + scalatest = scalatest, + junit = junit, + specs2 = specs2, + testing = testing, + ) + def scala_register_toolchains(): native.register_toolchains("@io_bazel_rules_scala_toolchains//...:all") diff --git a/test_cross_build/WORKSPACE b/test_cross_build/WORKSPACE index 6aa0c6473..c21b83d0c 100644 --- a/test_cross_build/WORKSPACE +++ b/test_cross_build/WORKSPACE @@ -72,7 +72,7 @@ scala_config( ) # loads other rules Rules Scala depends on -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( scalatest = True, diff --git a/test_version/WORKSPACE.template b/test_version/WORKSPACE.template index c16c9ff68..eaeff1ba0 100644 --- a/test_version/WORKSPACE.template +++ b/test_version/WORKSPACE.template @@ -59,7 +59,7 @@ scala_config(enable_compiler_dependency_tracking = True) load("@io_bazel_rules_scala//scala:scala_cross_version.bzl", "extract_major_version") -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/third_party/test/example_external_workspace/WORKSPACE b/third_party/test/example_external_workspace/WORKSPACE index 03b524665..58402d1bf 100644 --- a/third_party/test/example_external_workspace/WORKSPACE +++ b/third_party/test/example_external_workspace/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains( fetch_sources = True, diff --git a/third_party/test/proto/WORKSPACE b/third_party/test/proto/WORKSPACE index 12cb91cb3..332d8f446 100644 --- a/third_party/test/proto/WORKSPACE +++ b/third_party/test/proto/WORKSPACE @@ -31,7 +31,7 @@ load("@io_bazel_rules_scala//:scala_config.bzl", "scala_config") scala_config() -load("@io_bazel_rules_scala//scala:scala.bzl", "scala_toolchains") +load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_toolchains") scala_toolchains() From a86756a390a3d1742ea41e84c6eac326e20e621c Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Mon, 2 Dec 2024 00:05:08 -0500 Subject: [PATCH 3/5] Extract versioned `_JUNIT_DEPS` in `test/BUILD` Eliminates reliance on the default `@io_bazel_rules_scala_junit_junit` artifact repository. --- test/BUILD | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/test/BUILD b/test/BUILD index 4c2d7a263..4b3485a26 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,5 +1,3 @@ -load("@rules_python//python:defs.bzl", "py_binary") -load("@rules_java//java:defs.bzl", "java_binary", "java_library") load( "//scala:scala.bzl", "scala_binary", @@ -13,10 +11,21 @@ load( "scala_test", "scala_test_suite", ) +load("//scala:scala_cross_version.bzl", "repositories") load(":check_statsfile.bzl", "check_statsfile") +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION") +load("@rules_java//java:defs.bzl", "java_binary", "java_library") +load("@rules_python//python:defs.bzl", "py_binary") package(default_testonly = 1) +_JUNIT_DEPS = repositories( + SCALA_VERSION, + [ + "@io_bazel_rules_scala_junit_junit", + ], +) + # The examples below show how to combine Scala and Java rules. # ScalaBinary is the Scala equivalent of JavaBinary. @@ -491,7 +500,6 @@ scala_junit_test( "//test/src/main/resources/java_sources:CompiledWithJava11", "//test/src/main/resources/java_sources:CompiledWithJava8", ], - deps = ["@io_bazel_rules_scala_junit_junit"], ) # Generate a file containing the rootpaths of a Scala binary. @@ -612,7 +620,7 @@ scala_library( scala_library( name = "customJunitRunner", srcs = ["src/main/scala/scalarules/test/junit/JunitCustomRunner.java"], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) # make sure making a fat jar strips signatures @@ -698,7 +706,7 @@ check_statsfile("ScalaLibBinary") scala_library( name = "JunitSeparateTarget", srcs = ["src/main/scala/scalarules/test/junit/separate_target/JunitSeparateTargetTest.scala"], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_junit_test( @@ -712,7 +720,7 @@ scala_junit_test( java_library( name = "JunitJavaSeparateTarget", srcs = ["src/main/scala/scalarules/test/junit/separate_target/JunitJavaSeparateTargetTest.java"], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_junit_test( @@ -726,14 +734,14 @@ scala_junit_test( scala_library( name = "TargetWithTestThatShouldNotRun", srcs = ["src/main/scala/scalarules/test/junit/separate_target/FailingTest.scala"], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_library( name = "JunitSeparateTargetWithDependencyOnTest", srcs = ["src/main/scala/scalarules/test/junit/separate_target/JunitSeparateTargetTest.scala"], runtime_deps = [":TargetWithTestThatShouldNotRun"], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_junit_test( @@ -748,7 +756,7 @@ java_library( name = "JunitSeparateJavaTargetWithDependencyOnTest", srcs = ["src/main/scala/scalarules/test/junit/separate_target/JunitJavaSeparateTargetTest.java"], runtime_deps = [":TargetWithTestThatShouldNotRun"], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_junit_test( @@ -765,7 +773,7 @@ scala_library( "src/main/scala/scalarules/test/junit/separate_target/JunitJavaSeparateTargetTest.java", "src/main/scala/scalarules/test/junit/separate_target/SomeScalaClass.scala", ], - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_junit_test( @@ -783,7 +791,7 @@ scala_library( ], # make sure java compilation toolchain matches runtime toolchain ie --target java_compile_toolchain = "@bazel_tools//tools/jdk:toolchain_java11", - deps = ["@io_bazel_rules_scala_junit_junit"], + deps = _JUNIT_DEPS, ) scala_junit_test( @@ -800,7 +808,6 @@ scala_junit_test( size = "small", srcs = ["src/main/scala/scalarules/test/junit/JunitNoTestEnvironmentTest.scala"], suffixes = ["Test"], - deps = ["@io_bazel_rules_scala_junit_junit"], ) scala_junit_test( @@ -811,7 +818,6 @@ scala_junit_test( "my_env_var": "my_value", }, suffixes = ["Test"], - deps = ["@io_bazel_rules_scala_junit_junit"], ) py_binary( From 26f0ffbfc1607fe9bd80613d74379fe7b7e5f805 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Mon, 2 Dec 2024 09:44:01 -0500 Subject: [PATCH 4/5] Update `{junit,specs2_junit}_toolchain()` These macros now mirror the implementation of `scalatest_toolchain()`. However, I realized that the old pattern of calling `scalatest_repositories()` followed by `scalatest_toolchain()` will no longer work without first calling `scala_toolchains(scalatest = True)`. This is because the `alias` targets in `testing/BUILD` that replace the previous implementations all point to `@io_bazel_rules_scala_toolchains`. So if we want to keep these macros, it seems like we should maybe restore the original toolchain targets in `testing/BUILD`. If we don't, we can remove these macros, but we can document these as breaking changes, and update other documentation accordingly. --- testing/junit.bzl | 7 ++++++- testing/scalatest.bzl | 5 ++--- testing/specs2_junit.bzl | 7 ++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/testing/junit.bzl b/testing/junit.bzl index a87ac6817..2208675b9 100644 --- a/testing/junit.bzl +++ b/testing/junit.bzl @@ -1,7 +1,12 @@ load("//junit:junit.bzl", _repositories = "junit_repositories") +load("//scala:scala_cross_version.bzl", "version_suffix") +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") def junit_repositories(): _repositories() def junit_toolchain(): - native.register_toolchains("@io_bazel_rules_scala//testing:junit_toolchain") + for scala_version in SCALA_VERSIONS: + native.register_toolchains(str(Label( + "//testing:junit_toolchain" + version_suffix(scala_version), + ))) diff --git a/testing/scalatest.bzl b/testing/scalatest.bzl index 3260aa457..1d66fe50c 100644 --- a/testing/scalatest.bzl +++ b/testing/scalatest.bzl @@ -1,5 +1,5 @@ -load("//scalatest:scalatest.bzl", _repositories = "scalatest_repositories") load("//scala:scala_cross_version.bzl", "version_suffix") +load("//scalatest:scalatest.bzl", _repositories = "scalatest_repositories") load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") def scalatest_repositories(): @@ -8,6 +8,5 @@ def scalatest_repositories(): def scalatest_toolchain(): for scala_version in SCALA_VERSIONS: native.register_toolchains(str(Label( - "//testing:scalatest_toolchain" + - version_suffix(scala_version), + "//testing:scalatest_toolchain" + version_suffix(scala_version), ))) diff --git a/testing/specs2_junit.bzl b/testing/specs2_junit.bzl index a7da792d8..eed731317 100644 --- a/testing/specs2_junit.bzl +++ b/testing/specs2_junit.bzl @@ -1,7 +1,12 @@ +load("//scala:scala_cross_version.bzl", "version_suffix") load("//specs2:specs2_junit.bzl", _repositories = "specs2_junit_repositories") +load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSIONS") def specs2_junit_repositories(): _repositories() def specs2_junit_toolchain(): - native.register_toolchains("@io_bazel_rules_scala//testing:specs2_junit_toolchain") + for scala_version in SCALA_VERSIONS: + native.register_toolchains(str(Label( + "//testing:specs2_junit_toolchain" + version_suffix(scala_version), + ))) From edec71734d9d64374ef600cfccb69a55d21ddbd5 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Wed, 4 Dec 2024 20:13:36 -0500 Subject: [PATCH 5/5] Move `scala/{private/macros/,}toolchains_repo.bzl` Like "Move `scala_toolchains` to `scala/toolchains.bzl`", removes the `scala_toolchains_repo` symbol from `scala/scala.bzl` and makes it available from `scala/toolchains_repo.bzl`. This avoids a future `test_scala_version 2.12.20` failure during Bazel 8 builds after adding `twitter_scrooge` toolchain support in the new `test_version/version_specific_tests_dir/scrooge_repositories.bzl` file. Otherwise, this new file would load `toolchains_repo` from `scala/scala.bzl`. The `test_version/test_scala_version_.../WORKSPACE` file generated from `test_version/WORKSPACE.template` would then transitively load `.bzl` files with `rules_java` symbols, breaking the test. ```txt $ RULES_SCALA_TEST_ONLY="test_scala_version 2.12.20" ./test_version.sh ERROR: Traceback (most recent call last): File ".../external/rules_scala/scala/private/common_attributes.bzl", line 18, column 28, in "deps": attr.label_list( Error in label_list: Illegal argument: element in 'providers' is of unexpected type. Either all elements should be providers, or all elements should be lists of providers, but got list with an element of type NoneType. ERROR: Error computing the main repository mapping: at test_version/test_scala_version_.../scrooge_repositories.bzl:1:6: at .../scala/scala.bzl:30:5: at .../scala/private/rules/scala_junit_test.bzl:5:5: initialization of module 'scala/private/common_attributes.bzl' failed ``` --- scala/scala.bzl | 5 ----- scala/toolchains.bzl | 2 +- scala/{private/macros => }/toolchains_repo.bzl | 0 3 files changed, 1 insertion(+), 6 deletions(-) rename scala/{private/macros => }/toolchains_repo.bzl (100%) diff --git a/scala/scala.bzl b/scala/scala.bzl index 3e8d5187e..643b655d1 100644 --- a/scala/scala.bzl +++ b/scala/scala.bzl @@ -2,10 +2,6 @@ load( "//specs2:specs2_junit.bzl", _specs2_junit_dependencies = "specs2_junit_dependencies", ) -load( - "//scala/private:macros/toolchains_repo.bzl", - _scala_toolchains_repo = "scala_toolchains_repo", -) load( "//scala/private:macros/scala_repositories.bzl", _rules_scala_setup = "rules_scala_setup", @@ -80,4 +76,3 @@ rules_scala_toolchain_deps_repositories = _rules_scala_toolchain_deps_repositori scala_test = _scala_test scala_test_suite = _scala_test_suite setup_scala_toolchain = _setup_scala_toolchain -scala_toolchains_repo = _scala_toolchains_repo diff --git a/scala/toolchains.bzl b/scala/toolchains.bzl index a3a14e1e9..07816e384 100644 --- a/scala/toolchains.bzl +++ b/scala/toolchains.bzl @@ -2,7 +2,7 @@ load("//junit:junit.bzl", "junit_artifact_ids") load("//scala/private:macros/scala_repositories.bzl", "scala_repositories") -load("//scala/private:macros/toolchains_repo.bzl", "scala_toolchains_repo") +load("//scala:toolchains_repo.bzl", "scala_toolchains_repo") load("//scala:scala_cross_version.bzl", "default_maven_server_urls") load("//scalatest:scalatest.bzl", "scalatest_artifact_ids") load("//specs2:specs2.bzl", "specs2_artifact_ids") diff --git a/scala/private/macros/toolchains_repo.bzl b/scala/toolchains_repo.bzl similarity index 100% rename from scala/private/macros/toolchains_repo.bzl rename to scala/toolchains_repo.bzl