Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add bzlmod minimal support (continuation) #1595

Closed
wants to merge 11 commits into from
5 changes: 5 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dt_patches/test_dt_patches
dt_patches/test_dt_patches_user_srcjar
test_version
test_cross_build
examples
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.3.0
7.2.1
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ hash2
.vscode
unformatted-*.backup.scala
.scala-build
test/semanticdb/tempsrc
test/semanticdb/tempsrc
MODULE.bazel.lock
54 changes: 54 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module(name = "io_bazel_rules_scala")

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_cc", version = "0.0.6")
bazel_dep(name = "rules_java", version = "7.8.0")
bazel_dep(name = "rules_proto", version = "6.0.2")
bazel_dep(name = "bazel_features", version = "1.14.0")
bazel_dep(name = "protobuf", version = "27.3", repo_name = "com_google_protobuf")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_python", version = "0.34.0", dev_dependency = True)

single_version_override(
module_name = "bazel_features",
version = "1.14.0",
)

version = use_extension("@bazel_features//private:extensions.bzl", "version_extension")
use_repo(version, "bazel_features_globals", "bazel_features_version")

bazel_dep(name = "rules_go", version = "0.39.1")

bazel_dep(name = "buildtools", repo_name = "com_github_bazelbuild_buildtools")

git_override(
module_name = "buildtools",
remote = "https://github.com/bazelbuild/buildtools.git",
commit = "37932ddd72301f8ca6075ee6b24e9e5d333c96a5"
)

use_repo(
use_extension("//:extensions/scala_config_ext.bzl", "scala_config_dep"),
"io_bazel_rules_scala_config"
)

non_module_deps = use_extension("//:extensions/non_module_deps.bzl", "non_module_deps")
use_repo(non_module_deps, "scala_compiler_source_2_12_18")
use_repo(non_module_deps, "io_bazel_rules_scala_scala_library_2_12_18")
use_repo(non_module_deps, "io_bazel_rules_scala_scala_compiler_2_12_18")
use_repo(non_module_deps, "io_bazel_rules_scala_scala_xml_2_12_18")
use_repo(non_module_deps, "io_bazel_rules_scala_scala_parser_combinators_2_12_18")
use_repo(non_module_deps, "io_bazel_rules_scala_scala_interfaces")
use_repo(non_module_deps, "io_bazel_rules_scala_scala_reflect_2_12_18")
use_repo(non_module_deps, "org_scalameta_semanticdb_scalac_2_12_18")
# use_repo(non_module_deps, "io_bazel_rules_scala_scala_tasty_core")
# use_repo(non_module_deps, "io_bazel_rules_scala_scala_asm")
# use_repo(non_module_deps, "io_bazel_rules_scala_scala_library_2")
use_repo(non_module_deps, "io_bazel_rules_scala_org_openjdk_jmh_jmh_core")
use_repo(non_module_deps, "io_bazel_rules_scala_org_openjdk_jmh_jmh_generator_asm")
use_repo(non_module_deps, "io_bazel_rules_scala_org_openjdk_jmh_jmh_generator_reflection")
use_repo(non_module_deps, "io_bazel_rules_scala_org_ows2_asm_asm")
use_repo(non_module_deps, "io_bazel_rules_scala_net_sf_jopt_simple_jopt_simple")
use_repo(non_module_deps, "io_bazel_rules_scala_org_apache_commons_commons_math3")

register_toolchains("@io_bazel_rules_scala//jmh:jmh_toolchain")
134 changes: 134 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
workspace(name = "io_bazel_rules_scala")

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

load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
load("//twitter_scrooge:twitter_scrooge.bzl", "twitter_scrooge")

twitter_scrooge()

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()

register_toolchains("//testing:testing_toolchain")

load("//scala/scalafmt:scalafmt_repositories.bzl", "scalafmt_default_config", "scalafmt_repositories")

scalafmt_default_config()

scalafmt_repositories()

MAVEN_SERVER_URLS = default_maven_server_urls()

# needed for the cross repo proto test
load("//test/proto_cross_repo_boundary:repo.bzl", "proto_cross_repo_boundary_repository")

proto_cross_repo_boundary_repository()

new_local_repository(
name = "test_new_local_repo",
build_file_content =
"""
filegroup(
name = "data",
srcs = glob(["**/*.txt"]),
visibility = ["//visibility:public"],
)
""",
path = "third_party/test/new_local_repo",
)

local_repository(
name = "example_external_workspace",
path = "third_party/test/example_external_workspace",
)

load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")

scala_register_unused_deps_toolchains()

register_toolchains("@io_bazel_rules_scala//test/proto:scalapb_toolchain")

load("//scala:scala_maven_import_external.bzl", "java_import_external")

# bazel's java_import_external has been altered in rules_scala to be a macro based on jvm_import_external
# in order to allow for other jvm-language imports (e.g. scala_import)
# the 3rd-party dependency below is using the java_import_external macro
# in order to make sure no regression with the original java_import_external
java_import_external(
name = "org_apache_commons_commons_lang_3_5_without_file",
generated_linkable_rule_name = "linkable_org_apache_commons_commons_lang_3_5_without_file",
jar_sha256 = "8ac96fc686512d777fca85e144f196cd7cfe0c0aec23127229497d1a38ff651c",
jar_urls = ["https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar"],
licenses = ["notice"], # Apache 2.0
neverlink = True,
testonly_ = True,
)

## Linting

# load("//private:format.bzl", "format_repositories")
#
# format_repositories()

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

# We need to select based on platform when we use these
# https://github.com/bazelbuild/bazel/issues/11655
remote_jdk8_repos()

http_archive(
name = "bazelci_rules",
sha256 = "eca21884e6f66a88c358e580fd67a6b148d30ab57b1680f62a96c00f9bc6a07e",
strip_prefix = "bazelci_rules-1.0.0",
url = "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz",
)

load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")

rbe_preconfig(
name = "rbe_default",
toolchain = "ubuntu2004-bazel-java11",
)

load("//third_party/repositories:repositories.bzl", "repositories")

repositories(
fetch_sources = False,
for_artifact_ids = [
# test adding a scala jar:
"com_twitter__scalding_date",
# test of strict deps (scalac plugin UT + E2E)
"com_google_guava_guava_21_0_with_file",
"com_github_jnr_jffi_native",
"org_apache_commons_commons_lang_3_5",
"com_google_guava_guava_21_0",
# test of import external
# scala maven import external decodes maven artifacts to its parts
# (group id, artifact id, packaging, version and classifier). To make sure
# the decoding and then the download url composition are working the artifact example
# must contain all the different parts and sha256s so the downloaded content will be
# validated against it
"org_springframework_spring_core",
"org_springframework_spring_tx",
"org_typelevel_kind_projector",
# For testing that we don't include sources jars to the classpath
"org_typelevel__cats_core",
],
maven_servers = MAVEN_SERVER_URLS,
)

load("//test/toolchains:jdk.bzl", "remote_jdk21_repositories", "remote_jdk21_toolchains")

remote_jdk21_repositories()

remote_jdk21_toolchains()
75 changes: 75 additions & 0 deletions extensions/non_module_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
load("//third_party/repositories:repositories.bzl", "repository")
load(
"@io_bazel_rules_scala//scala/private:macros/scala_repositories.bzl",
_dt_patched_compiler_setup = "dt_patched_compiler_setup",
)
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")

def _non_module_deps_impl(ctx):
_dt_patched_compiler_setup(SCALA_VERSION)
repository(
id = "io_bazel_rules_scala_scala_library",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_compiler",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_xml",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_parser_combinators",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_interfaces",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_reflect",
validate_scala_version = True,
)
repository(
id = "org_scalameta_semanticdb_scalac",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_tasty_core",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_asm",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_scala_library_2",
validate_scala_version = True,
)
repository(
id = "io_bazel_rules_scala_org_openjdk_jmh_jmh_core",
fetch_sources = False,
)
repository(
id = "io_bazel_rules_scala_org_openjdk_jmh_jmh_generator_asm",
fetch_sources = False,
)
repository(
id = "io_bazel_rules_scala_org_openjdk_jmh_jmh_generator_reflection",
fetch_sources = False,
)
repository(
id = "io_bazel_rules_scala_org_ows2_asm_asm",
fetch_sources = False,
)
repository(
id = "io_bazel_rules_scala_net_sf_jopt_simple_jopt_simple",
fetch_sources = False,
)
repository(
id = "io_bazel_rules_scala_org_apache_commons_commons_math3",
fetch_sources = False,
)

non_module_deps = module_extension(implementation = _non_module_deps_impl)
8 changes: 8 additions & 0 deletions extensions/scala_config_ext.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//:scala_config.bzl", "scala_config")

def _scala_config_dep_impl(ctx):
scala_config(enable_compiler_dependency_tracking = True)

scala_config_dep = module_extension(
implementation = _scala_config_dep_impl,
)
4 changes: 2 additions & 2 deletions scala/private/phases/phase_dependency.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def _get_unused_deps_mode(ctx):

def _is_target_included(target, includes, excludes):
for exclude in excludes:
if target.startswith(exclude):
if target.startswith(exclude) or target.startswith("@" + exclude):
return False

for include in includes:
if target.startswith(include):
if target.startswith(include) or target.startswith("@" + include):
return True

return False
5 changes: 4 additions & 1 deletion scala/private/rules/scala_repl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def make_scala_repl(*extras):
common_outputs,
*[extra["outputs"] for extra in extras if "outputs" in extra]
),
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
toolchains = [
"@io_bazel_rules_scala//scala:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
],
cfg = scala_version_transition,
incompatible_use_toolchain_transition = True,
implementation = _scala_repl_impl,
Expand Down
7 changes: 5 additions & 2 deletions scala/scala_maven_import_external.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def _jvm_import_external(repository_ctx):
if (repository_ctx.attr.generated_linkable_rule_name and
not repository_ctx.attr.neverlink):
fail("Only use generated_linkable_rule_name if neverlink is set")
name = repository_ctx.attr.generated_rule_name or repository_ctx.name
name = repository_ctx.attr.repo_name or repository_ctx.attr.generated_rule_name or repository_ctx.name
# HACK: remove the prefix up to the last ~
name = name[name.rfind("~") + 1:]
urls = repository_ctx.attr.jar_urls
if repository_ctx.attr.jar_sha256:
print("'jar_sha256' is deprecated. Please use 'artifact_sha256'")
Expand Down Expand Up @@ -136,7 +138,7 @@ def _jvm_import_external(repository_ctx):
"",
"alias(",
" name = \"jar\",",
" actual = \"@%s\"," % repository_ctx.name,
" actual = \"@%s\"," % (repository_ctx.attr.repo_name or repository_ctx.name),
")",
"",
]))
Expand Down Expand Up @@ -226,6 +228,7 @@ jvm_import_external = repository_rule(
implementation = _jvm_import_external,
attrs = {
"rule_name": attr.string(mandatory = True),
"repo_name": attr.string(),
"licenses": attr.string_list(mandatory = True, allow_empty = False),
"jar_urls": attr.string_list(mandatory = True, allow_empty = False),
"jar_sha256": attr.string(doc = "'jar_sha256' is deprecated. Please use 'artifact_sha256'"),
Expand Down
2 changes: 1 addition & 1 deletion test/proto_cross_repo_boundary/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ def proto_cross_repo_boundary_repository():
native.new_local_repository(
name = "proto_cross_repo_boundary",
path = "test/proto_cross_repo_boundary/repo",
build_file = "test/proto_cross_repo_boundary/repo/BUILD.repo",
build_file = "//test/proto_cross_repo_boundary:repo/BUILD.repo",
)
2 changes: 1 addition & 1 deletion test/shell/test_scala_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test_classpath_contains_2_13() {

test_scala_config_content() {
bazel build --repo_env=SCALA_VERSION=0.0.0 @io_bazel_rules_scala_config//:all 2> /dev/null
grep "SCALA_MAJOR_VERSION='0.0'" $(bazel info output_base)/external/io_bazel_rules_scala_config/config.bzl
grep "SCALA_MAJOR_VERSION='0.0'" $(bazel info output_base)/external/*/config.bzl
}

$runner test_classpath_contains_2_12
Expand Down
2 changes: 1 addition & 1 deletion test/shell/test_scala_library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test_scala_library_expect_better_failure_with_target_label_from_stamped_jar_on_m

test_scala_library_expect_better_failure_message_on_missing_transitive_dependency_labels_from_other_jvm_rules() {
transitive_target='.*transitive_dependency_without_manifest.jar'
direct_target='@//test_expect_failure/missing_direct_deps/internal_deps:unstamped_direct_java_provider_dependency'
direct_target='@.*//test_expect_failure/missing_direct_deps/internal_deps:unstamped_direct_java_provider_dependency'
test_target='//test_expect_failure/missing_direct_deps/internal_deps:unstamped_jar_dependent_on_some_java_provider'

expected_message="Unknown label of file $transitive_target which came from $direct_target"
Expand Down
Loading