Skip to content

Commit 3ace6ac

Browse files
gkk-stripeIgal Tabachnik
authored and
Igal Tabachnik
committed
Scala 2.12 support (bazel-contrib#304)
* Scala 2.12 updates Bump dependencies to versions that support Scala 2.12. * Fix incompatible scrooge api change. They swapped the logger interface and now there's a different went to silence the logging. * Add scalactic as a transitive dep of scalatest Newer versions of scalatest have scalactic as a dependency. Let bazel know about it and add its a transitive dep. * Add a missing dependency on util_core from scrooge rule Add a missing dependency on Twitter's util_core from scrooge rule * Set -source 1.8 and -target 1.8 javac options This makes the code to compile again on my laptop. * Remove dropped args when calling java_common.create_provider The 031e73c restructured java_commmon rule and dropped both `transitive_compile_time_jars` and `transitive_runtime_jars` from that rule. Let's sync scala.bzl with that change. * Upgrade to Scala 2.12.3 * Switch to Scala nightly build with classpath caching fix Switch to a recent nightly build that has a fix for the flat classpath caching bug that is breaking bazel's incremental compilation when worker is enabled. See bazel-contrib#251 (comment) for the details of the bug. * Crash jmh with an explanation why it doesn't work Link to an issue explaining the jmh failure is related to Java 8 and not directly caused by the Scala 2.12. * Upgrade the hash of the nightly build For mysterious reasons, the hash has changed for a build that should be unique. A sign that relying on nightly builds is risky. Yolo for now, though. * Revert "Switch to Scala nightly build with classpath caching fix" This reverts commit 55fd807. Switch back to 2.12.3 until 2.12.4 is released. This will make the testing easier for projects that rely on compiler plugins. * Disable jmh test See bazel-contrib#295 for why it's failing. * Switch back to bazel maven mirror Not sure why I switched to the main one in the first place. * Revert "Switch back to bazel maven mirror" This reverts commit 4e266fc. It turns out, the bazel mirror is not great for us because it misses some artifacts: ERROR: /Users/gkk/stripe/rules_scala/twitter_scrooge/twitter_scrooge.bzl:46:3: no such package '@util_core//jar': Failed to fetch Maven dependency: Could not transfer artifact com.twitter:util-core_2.12:jar:6.45.0 from/to util_core (http://mirror.bazel.build/repo1.maven.org/maven2/): Forbidden (403) and referenced by '//external:io_bazel_rules_scala/dependency/thrift/util_core'. * Append YdisableFlatCpCaching to scalacopts by default Append the `YdisableFlatCpCaching` compiler option by default as a workaround to bazel-contrib#305 * Disable the jmh test in test_run.sh The target is already commented out in BUILD file, but test_run.sh still tries to run it.
1 parent 9008293 commit 3ace6ac

File tree

7 files changed

+106
-179
lines changed

7 files changed

+106
-179
lines changed

WORKSPACE

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ maven_jar(
5656

5757
http_archive(
5858
name = "com_google_protobuf",
59-
urls = ["https://github.com/google/protobuf/archive/b04e5cba356212e4e8c66c61bbe0c3a20537c5b9.zip"],
60-
strip_prefix = "protobuf-b04e5cba356212e4e8c66c61bbe0c3a20537c5b9",
61-
sha256 = "cf4a434ac3a83040e9f65be92e153d00d075d8cd25e3f6c6d8670879f5796aa0",
59+
urls = ["https://github.com/google/protobuf/archive/b4b0e304be5a68de3d0ee1af9b286f958750f5e4.zip"],
60+
strip_prefix = "protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4",
61+
sha256 = "ff771a662fb6bd4d3cc209bcccedef3e93980a49f71df1e987f6afa3bcdcba3a",
6262
)
6363

6464
http_archive(
6565
name = "com_google_protobuf_java",
66-
urls = ["https://github.com/google/protobuf/archive/b04e5cba356212e4e8c66c61bbe0c3a20537c5b9.zip"],
67-
strip_prefix = "protobuf-b04e5cba356212e4e8c66c61bbe0c3a20537c5b9",
68-
sha256 = "cf4a434ac3a83040e9f65be92e153d00d075d8cd25e3f6c6d8670879f5796aa0",
66+
urls = ["https://github.com/google/protobuf/archive/b4b0e304be5a68de3d0ee1af9b286f958750f5e4.zip"],
67+
strip_prefix = "protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4",
68+
sha256 = "ff771a662fb6bd4d3cc209bcccedef3e93980a49f71df1e987f6afa3bcdcba3a",
6969
)
7070

7171
new_local_repository(

scala/BUILD

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1 @@
11
java_library(name = "transitive_scalatest", exports = ["@scalatest//jar", "@scalactic//jar"])
2-
load("//scala:scala_toolchain.bzl", "scala_toolchain")
3-
4-
toolchain_type(
5-
name = "toolchain_type",
6-
visibility = ["//visibility:public"]
7-
)
8-
9-
10-
scala_toolchain(
11-
name = 'default_toolchain_impl',
12-
scalacopts = [],
13-
visibility = ["//visibility:public"]
14-
)
15-
16-
17-
toolchain(
18-
name = 'default_toolchain',
19-
toolchain_type = '@io_bazel_rules_scala//scala:toolchain_type',
20-
toolchain = ':default_toolchain_impl',
21-
visibility = ["//visibility:public"]
22-
)

scala/scala.bzl

Lines changed: 52 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
"""Rules for supporting the Scala language."""
1616

1717
load("//specs2:specs2_junit.bzl", "specs2_junit_dependencies")
18-
load(":scala_cross_version.bzl", "scala_version", "scala_mvn_artifact")
19-
load("@io_bazel_rules_scala//scala:scala_toolchain.bzl", "scala_toolchain")
20-
2118
_jar_filetype = FileType([".jar"])
2219
_java_filetype = FileType([".java"])
2320
_scala_filetype = FileType([".scala"])
@@ -69,7 +66,7 @@ def _adjust_resources_path(path, resource_strip_prefix):
6966
def _add_resources_cmd(ctx):
7067
res_cmd = []
7168
for f in ctx.files.resources:
72-
c_dir, res_path = _adjust_resources_path(f.short_path, ctx.attr.resource_strip_prefix)
69+
c_dir, res_path = _adjust_resources_path(f.path, ctx.attr.resource_strip_prefix)
7370
target_path = res_path
7471
if target_path[0] == "/":
7572
target_path = target_path[1:]
@@ -210,15 +207,17 @@ PrintCompileTime: {print_compile_time}
210207
ResourceDests: {resource_dest}
211208
ResourceJars: {resource_jars}
212209
ResourceSrcs: {resource_src}
213-
ResourceShortPaths: {resource_short_paths}
214210
ResourceStripPrefix: {resource_strip_prefix}
215211
ScalacOpts: {scala_opts}
216212
SourceJars: {srcjars}
217213
DependencyAnalyzerMode: {dependency_analyzer_mode}
218214
""".format(
219215
out=ctx.outputs.jar.path,
220216
manifest=ctx.outputs.manifest.path,
221-
scala_opts=",".join(scalacopts),
217+
# always append -YdisableFlatCpCaching, workaround for
218+
# https://github.com/bazelbuild/rules_scala/issues/305
219+
# remove once we upgrade to Scala 2.12.4
220+
scala_opts=",".join(ctx.attr.scalacopts + ["-YdisableFlatCpCaching"]),
222221
print_compile_time=ctx.attr.print_compile_time,
223222
plugin_arg=plugin_arg,
224223
cp=compiler_classpath,
@@ -229,9 +228,8 @@ DependencyAnalyzerMode: {dependency_analyzer_mode}
229228
srcjars=",".join([f.path for f in all_srcjars]),
230229
java_files=",".join([f.path for f in java_srcs]),
231230
resource_src=",".join([f.path for f in ctx.files.resources]),
232-
resource_short_paths=",".join([f.short_path for f in ctx.files.resources]),
233231
resource_dest=",".join(
234-
[_adjust_resources_path_by_default_prefixes(f.short_path)[1] for f in ctx.files.resources]
232+
[_adjust_resources_path_by_default_prefixes(f.path)[1] for f in ctx.files.resources]
235233
),
236234
resource_strip_prefix=ctx.attr.resource_strip_prefix,
237235
resource_jars=",".join([f.path for f in ctx.files.resource_jars]),
@@ -295,24 +293,6 @@ DependencyAnalyzerMode: {dependency_analyzer_mode}
295293
return java_jar
296294

297295

298-
def _interim_java_provider_for_java_compilation(scala_output):
299-
# This is needed because Bazel >=0.7.0 requires ctx.actions and a Java
300-
# toolchain. Fortunately, the same change that added this requirement also
301-
# added this field to the Java provider so we can use it to test which
302-
# Bazel version we are running under.
303-
test_provider = java_common.create_provider()
304-
if hasattr(test_provider, "full_compile_jars"):
305-
return java_common.create_provider(
306-
use_ijar = False,
307-
compile_time_jars = [scala_output],
308-
runtime_jars = [],
309-
)
310-
else:
311-
return java_common.create_provider(
312-
compile_time_jars = [scala_output],
313-
runtime_jars = [],
314-
)
315-
316296
def try_to_compile_java_jar(ctx,
317297
scala_output,
318298
all_srcjars,
@@ -323,7 +303,10 @@ def try_to_compile_java_jar(ctx,
323303

324304
providers_of_dependencies = collect_java_providers_of(ctx.attr.deps)
325305
providers_of_dependencies += collect_java_providers_of(implicit_junit_deps_needed_for_java_compilation)
326-
scala_sources_java_provider = _interim_java_provider_for_java_compilation(scala_output)
306+
scala_sources_java_provider = java_common.create_provider(
307+
compile_time_jars = [scala_output],
308+
runtime_jars = [],
309+
)
327310
providers_of_dependencies += [scala_sources_java_provider]
328311

329312
full_java_jar = ctx.actions.declare_file(ctx.label.name + "_java.jar")
@@ -341,7 +324,6 @@ def try_to_compile_java_jar(ctx,
341324
exports = [],
342325
java_toolchain = ctx.attr._java_toolchain,
343326
host_javabase = ctx.attr._host_javabase,
344-
strict_deps = ctx.fragments.java.strict_java_deps,
345327
)
346328
return struct(jar = full_java_jar, ijar = provider.compile_jars.to_list().pop())
347329

@@ -414,35 +396,21 @@ def _write_launcher(ctx, rjars, main_class, jvm_flags, args="", wrapper_preamble
414396
# RUNPATH is defined here:
415397
# https://github.com/bazelbuild/bazel/blob/0.4.5/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt#L227
416398
classpath = ":".join(["${RUNPATH}%s" % (j.short_path) for j in rjars])
417-
418-
if ctx.attr.testonly:
419-
classpath = ":".join(["%s" % (j.short_path) for j in rjars])
420-
classpath = "target/test-classes:%s" % classpath
421-
422399
jvm_flags = " ".join([ctx.expand_location(f, ctx.attr.data) for f in jvm_flags])
423400
javabin = "%s/%s" % (runfiles_root, ctx.executable._java.short_path)
424401
template = ctx.attr._java_stub_template.files.to_list()[0]
425402

426-
exec_str = ""
427-
if wrapper_preamble == "":
428-
exec_str = "exec "
429-
430403
wrapper = ctx.new_file(ctx.label.name + "_wrapper.sh")
431404
ctx.file_action(
432405
output = wrapper,
433406
content = """#!/bin/bash
434407
{preamble}
435408
436-
rm -rf {runfiles_root}/target/test-classes
437-
mkdir -p {runfiles_root}/target/test-classes
438-
439-
{exec_str}{javabin} "$@" {args}
409+
{javabin} "$@" {args}
440410
""".format(
441411
preamble=wrapper_preamble,
442-
exec_str=exec_str,
443412
javabin=javabin,
444413
args=args,
445-
runfiles_root = runfiles_root
446414
),
447415
)
448416

@@ -469,17 +437,12 @@ def collect_srcjars(targets):
469437
srcjars += [target.srcjars.srcjar]
470438
return srcjars
471439

472-
def add_labels_of_jars_to(jars2labels, dependency, all_jars, direct_jars):
473-
for jar in direct_jars:
474-
add_label_of_direct_jar_to(jars2labels, dependency, jar)
440+
def add_labels_of_jars_to(jars2labels, dependency, all_jars):
475441
for jar in all_jars:
476-
add_label_of_indirect_jar_to(jars2labels, dependency, jar)
442+
add_label_of_jar_to(jars2labels, dependency, jar)
477443

478444

479-
def add_label_of_direct_jar_to(jars2labels, dependency, jar):
480-
jars2labels[jar.path] = dependency.label
481-
482-
def add_label_of_indirect_jar_to(jars2labels, dependency, jar):
445+
def add_label_of_jar_to(jars2labels, dependency, jar):
483446
if label_already_exists(jars2labels, jar):
484447
return
485448

@@ -488,10 +451,8 @@ def add_label_of_indirect_jar_to(jars2labels, dependency, jar):
488451
if provider_of_dependency_contains_label_of(dependency, jar):
489452
jars2labels[jar.path] = dependency.jars_to_labels[jar.path]
490453
else:
491-
jars2labels[jar.path] = "Unknown label of file {jar_path} which came from {dependency_label}".format(
492-
jar_path = jar.path,
493-
dependency_label = dependency.label
494-
)
454+
jars2labels[jar.path] = dependency.label
455+
495456

496457
def label_already_exists(jars2labels, jar):
497458
return jar.path in jars2labels
@@ -517,19 +478,6 @@ def not_sources_jar(name):
517478
def filter_not_sources(deps):
518479
return depset([dep for dep in deps.to_list() if not_sources_jar(dep.basename) ])
519480

520-
def _collect_runtime_jars(dep_targets):
521-
runtime_jars = depset()
522-
523-
for dep_target in dep_targets:
524-
if java_common.provider in dep_target:
525-
runtime_jars += dep_target[java_common.provider].transitive_runtime_jars
526-
else:
527-
# support http_file pointed at a jar. http_jar uses ijar,
528-
# which breaks scala macros
529-
runtime_jars += filter_not_sources(dep_target.files)
530-
531-
return runtime_jars
532-
533481
def _collect_jars_when_dependency_analyzer_is_off(dep_targets):
534482
compile_jars = depset()
535483
runtime_jars = depset()
@@ -557,31 +505,26 @@ def _collect_jars_when_dependency_analyzer_is_on(dep_targets):
557505
runtime_jars = depset()
558506

559507
for dep_target in dep_targets:
560-
current_dep_compile_jars = depset()
561-
current_dep_transitive_compile_jars = depset()
562-
563508
if java_common.provider in dep_target:
564509
java_provider = dep_target[java_common.provider]
565-
current_dep_compile_jars = java_provider.compile_jars
566-
current_dep_transitive_compile_jars = java_provider.transitive_compile_time_jars
510+
compile_jars += java_provider.compile_jars
511+
transitive_compile_jars += java_provider.transitive_compile_time_jars + java_provider.compile_jars
567512
runtime_jars += java_provider.transitive_runtime_jars
568513
else:
569514
# support http_file pointed at a jar. http_jar uses ijar,
570515
# which breaks scala macros
571-
current_dep_compile_jars = filter_not_sources(dep_target.files)
516+
compile_jars += filter_not_sources(dep_target.files)
572517
runtime_jars += filter_not_sources(dep_target.files)
573-
current_dep_transitive_compile_jars = filter_not_sources(dep_target.files)
518+
transitive_compile_jars += filter_not_sources(dep_target.files)
574519

575-
compile_jars += current_dep_compile_jars
576-
transitive_compile_jars += current_dep_transitive_compile_jars
577-
add_labels_of_jars_to(jars2labels, dep_target, current_dep_transitive_compile_jars, current_dep_compile_jars)
520+
add_labels_of_jars_to(jars2labels, dep_target, transitive_compile_jars)
578521

579522
return struct(compile_jars = compile_jars,
580523
transitive_runtime_jars = runtime_jars,
581524
jars2labels = jars2labels,
582525
transitive_compile_jars = transitive_compile_jars)
583526

584-
def collect_jars(dep_targets, dependency_analyzer_is_off = True):
527+
def _collect_jars(dep_targets, dependency_analyzer_is_off = True):
585528
"""Compute the runtime and compile-time dependencies from the given targets""" # noqa
586529

587530
if dependency_analyzer_is_off:
@@ -609,26 +552,31 @@ def _collect_jars_from_common_ctx(ctx, extra_deps = [], extra_runtime_deps = [])
609552

610553
# Get jars from deps
611554
auto_deps = [ctx.attr._scalalib, ctx.attr._scalareflect]
612-
deps_jars = collect_jars(ctx.attr.deps + auto_deps + extra_deps, dependency_analyzer_is_off)
555+
deps_jars = _collect_jars(ctx.attr.deps + auto_deps + extra_deps, dependency_analyzer_is_off)
613556
(cjars, transitive_rjars, jars2labels, transitive_compile_jars) = (deps_jars.compile_jars, deps_jars.transitive_runtime_jars, deps_jars.jars2labels, deps_jars.transitive_compile_jars)
614557

615-
transitive_rjars += _collect_runtime_jars(ctx.attr.runtime_deps + extra_runtime_deps)
558+
runtime_dep_jars = _collect_jars(ctx.attr.runtime_deps + extra_runtime_deps, dependency_analyzer_is_off)
559+
transitive_rjars += runtime_dep_jars.transitive_runtime_jars
560+
561+
if not dependency_analyzer_is_off:
562+
jars2labels.update(runtime_dep_jars.jars2labels)
616563

617564
return struct(compile_jars = cjars, transitive_runtime_jars = transitive_rjars, jars2labels=jars2labels, transitive_compile_jars = transitive_compile_jars)
618565

619566
def _format_full_jars_for_intellij_plugin(full_jars):
620567
return [struct (class_jar = jar, ijar = None) for jar in full_jars]
621568

622-
def create_java_provider(scalaattr, transitive_compile_time_jars):
623-
# This is needed because Bazel >=0.7.0 requires ctx.actions and a Java
569+
def _create_java_provider(ctx, scalaattr, transitive_compile_time_jars):
570+
# This is needed because Bazel >=0.6.0 requires ctx.actions and a Java
624571
# toolchain. Fortunately, the same change that added this requirement also
625572
# added this field to the Java provider so we can use it to test which
626573
# Bazel version we are running under.
627574
test_provider = java_common.create_provider()
628575

629576
if hasattr(test_provider, "full_compile_jars"):
630577
return java_common.create_provider(
631-
use_ijar = False,
578+
ctx.actions,
579+
java_toolchain = ctx.attr._java_toolchain,
632580
compile_time_jars = scalaattr.compile_jars,
633581
runtime_jars = scalaattr.transitive_runtime_jars,
634582
transitive_compile_time_jars = transitive_compile_time_jars + scalaattr.compile_jars,
@@ -672,7 +620,7 @@ def _lib(ctx, non_macro_lib):
672620
# Add information from exports (is key that AFTER all build actions/runfiles analysis)
673621
# Since after, will not show up in deploy_jar or old jars runfiles
674622
# Notice that compile_jars is intentionally transitive for exports
675-
exports_jars = collect_jars(ctx.attr.exports)
623+
exports_jars = _collect_jars(ctx.attr.exports)
676624
next_cjars += exports_jars.compile_jars
677625
transitive_rjars += exports_jars.transitive_runtime_jars
678626

@@ -693,7 +641,7 @@ def _lib(ctx, non_macro_lib):
693641
transitive_exports = [] #needed by intellij plugin
694642
)
695643

696-
java_provider = create_java_provider(scalaattr, jars.transitive_compile_jars)
644+
java_provider = _create_java_provider(ctx, scalaattr, jars.transitive_compile_jars)
697645

698646
return struct(
699647
files = depset([ctx.outputs.jar]), # Here is the default output
@@ -757,7 +705,7 @@ def _scala_binary_common(ctx, cjars, rjars, transitive_compile_time_jars, jars2l
757705
transitive_exports = [] #needed by intellij plugin
758706
)
759707

760-
java_provider = create_java_provider(scalaattr, transitive_compile_time_jars)
708+
java_provider = _create_java_provider(ctx, scalaattr, transitive_compile_time_jars)
761709

762710
return struct(
763711
files=depset([ctx.outputs.executable]),
@@ -834,13 +782,13 @@ def _scala_test_impl(ctx):
834782
jars.transitive_compile_jars, jars.jars2labels)
835783
# _scalatest is an http_jar, so its compile jar is run through ijar
836784
# however, contains macros, so need to handle separately
837-
scalatest_jars = collect_jars([ctx.attr._scalatest]).transitive_runtime_jars
785+
scalatest_jars = _collect_jars([ctx.attr._scalatest]).transitive_runtime_jars
838786
cjars += scalatest_jars
839787
transitive_rjars += scalatest_jars
840788

841789
if is_dependency_analyzer_on(ctx):
842790
transitive_compile_jars += scalatest_jars
843-
add_labels_of_jars_to(jars_to_labels, ctx.attr._scalatest, scalatest_jars, scalatest_jars)
791+
add_labels_of_jars_to(jars_to_labels, ctx.attr._scalatest, scalatest_jars)
844792

845793
args = " ".join([
846794
"-R \"{path}\"".format(path=ctx.outputs.jar.short_path),
@@ -1074,6 +1022,17 @@ scala_repl = rule(
10741022
toolchains = ['@io_bazel_rules_scala//scala:toolchain_type'],
10751023
)
10761024

1025+
def scala_version():
1026+
"""return the scala version for use in maven coordinates"""
1027+
return "2.12"
1028+
1029+
def scala_mvn_artifact(artifact):
1030+
gav = artifact.split(":")
1031+
groupid = gav[0]
1032+
artifactid = gav[1]
1033+
version = gav[2]
1034+
return "%s:%s_%s:%s" % (groupid, artifactid, scala_version(), version)
1035+
10771036
SCALA_BUILD_FILE = """
10781037
# scala.BUILD
10791038
java_import(
@@ -1116,9 +1075,9 @@ java_library(
11161075
def scala_repositories():
11171076
native.new_http_archive(
11181077
name = "scala",
1119-
strip_prefix = "scala-2.12.4",
1120-
sha256 = "9554a0ca31aa8701863e881281b1772370a87e993ce785bb24505f2431292a21",
1121-
url = "https://downloads.lightbend.com/scala/2.12.4/scala-2.12.4.tgz",
1078+
strip_prefix = "scala-2.12.3",
1079+
sha256 = "2b796ab773fbedcc734ba881a6486d54180b699ade8ba7493e91912044267c8c",
1080+
url = "https://downloads.lightbend.com/scala/2.12.3/scala-2.12.3.tgz",
11221081
build_file_content = SCALA_BUILD_FILE,
11231082
)
11241083

@@ -1138,7 +1097,7 @@ def scala_repositories():
11381097

11391098
native.maven_server(
11401099
name = "scalac_deps_maven_server",
1141-
url = "https://mirror.bazel.build/repo1.maven.org/maven2/",
1100+
url = "http://mirror.bazel.build/repo1.maven.org/maven2/",
11421101
)
11431102

11441103
native.maven_jar(

0 commit comments

Comments
 (0)