Skip to content

Commit 7456a0b

Browse files
johnynekittaiz
authored andcommitted
Fix remaining incompatible changes warnings (#480)
* Fix remaining incompatible changes warnings * add tests, fix test for strict deps
1 parent 4afc7ab commit 7456a0b

File tree

4 files changed

+78
-76
lines changed

4 files changed

+78
-76
lines changed

scala/scala.bzl

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,27 @@ touch {statsfile}
113113

114114

115115
def _collect_plugin_paths(plugins):
116-
paths = depset()
116+
paths = []
117117
for p in plugins:
118118
if hasattr(p, "path"):
119-
paths += [p.path]
119+
paths.append(p)
120120
elif hasattr(p, "scala"):
121-
paths += [p.scala.outputs.jar.path]
121+
paths.append(p.scala.outputs.jar)
122122
elif hasattr(p, "java"):
123-
paths += [j.class_jar.path for j in p.java.outputs.jars]
123+
paths.extend([j.class_jar for j in p.java.outputs.jars])
124124
# support http_file pointed at a jar. http_jar uses ijar,
125125
# which breaks scala macros
126126
elif hasattr(p, "files"):
127-
paths += [f.path for f in p.files if not_sources_jar(f.basename) ]
128-
return paths
127+
paths.extend([f for f in p.files if not_sources_jar(f.basename)])
128+
return depset(paths)
129129

130130

131131
def _expand_location(ctx, flags):
132132
return [ctx.expand_location(f, ctx.attr.data) for f in flags]
133133

134+
def _join_path(args, sep=","):
135+
return sep.join([f.path for f in args])
136+
134137
def _compile(ctx, cjars, dep_srcjars, buildijar, transitive_compile_jars, labels, implicit_junit_deps_needed_for_java_compilation):
135138
ijar_output_path = ""
136139
ijar_cmd_path = ""
@@ -141,7 +144,7 @@ def _compile(ctx, cjars, dep_srcjars, buildijar, transitive_compile_jars, labels
141144
java_srcs = _java_filetype.filter(ctx.files.srcs)
142145
sources = _scala_filetype.filter(ctx.files.srcs) + java_srcs
143146
srcjars = _srcjar_filetype.filter(ctx.files.srcs)
144-
all_srcjars = depset(srcjars + list(dep_srcjars))
147+
all_srcjars = depset(srcjars, transitive = [dep_srcjars])
145148
# look for any plugins:
146149
plugins = _collect_plugin_paths(ctx.attr.plugins)
147150
dependency_analyzer_plugin_jars = []
@@ -156,13 +159,14 @@ def _compile(ctx, cjars, dep_srcjars, buildijar, transitive_compile_jars, labels
156159
# "off" mode is used as a feature toggle, that preserves original behaviour
157160
dependency_analyzer_mode = ctx.fragments.java.strict_java_deps
158161
dep_plugin = ctx.attr._dependency_analyzer_plugin
159-
plugins += [f.path for f in dep_plugin.files]
162+
plugins = depset(transitive = [plugins, dep_plugin.files])
160163
dependency_analyzer_plugin_jars = ctx.files._dependency_analyzer_plugin
161164
compiler_classpath_jars = transitive_compile_jars
162165

163-
direct_jars = ",".join([j.path for j in cjars])
164-
indirect_jars = ",".join([j.path for j in transitive_compile_jars])
165-
indirect_targets = ",".join([labels[j.path] for j in transitive_compile_jars])
166+
direct_jars = _join_path(cjars.to_list())
167+
transitive_cjars_list = transitive_compile_jars.to_list()
168+
indirect_jars = _join_path(transitive_cjars_list)
169+
indirect_targets = ",".join([labels[j.path] for j in transitive_cjars_list])
166170
current_target = str(ctx.label)
167171

168172
optional_scalac_args = """
@@ -177,10 +181,10 @@ CurrentTarget: {current_target}
177181
current_target = current_target
178182
)
179183

180-
plugin_arg = ",".join(list(plugins))
184+
plugin_arg = _join_path(plugins.to_list())
181185

182186
separator = ctx.configuration.host_path_separator
183-
compiler_classpath = separator.join([j.path for j in compiler_classpath_jars])
187+
compiler_classpath = _join_path(compiler_classpath_jars.to_list(), separator)
184188

185189
toolchain = ctx.toolchains['@io_bazel_rules_scala//scala:toolchain_type']
186190
scalacopts = toolchain.scalacopts + ctx.attr.scalacopts
@@ -213,20 +217,21 @@ StatsfileOutput: {statsfile_output}
213217
print_compile_time=ctx.attr.print_compile_time,
214218
plugin_arg=plugin_arg,
215219
cp=compiler_classpath,
216-
classpath_resource_src=",".join([f.path for f in classpath_resources]),
217-
files=",".join([f.path for f in sources]),
220+
classpath_resource_src=_join_path(classpath_resources),
221+
files=_join_path(sources),
218222
enableijar=buildijar,
219223
ijar_out=ijar_output_path,
220224
ijar_cmd_path=ijar_cmd_path,
221-
srcjars=",".join([f.path for f in all_srcjars]),
222-
java_files=",".join([f.path for f in java_srcs]),
225+
srcjars=_join_path(all_srcjars.to_list()),
226+
java_files=_join_path(java_srcs),
227+
# the resource paths need to be aligned in order
223228
resource_src=",".join([f.path for f in ctx.files.resources]),
224229
resource_short_paths=",".join([f.short_path for f in ctx.files.resources]),
225230
resource_dest=",".join(
226231
[_adjust_resources_path_by_default_prefixes(f.short_path)[1] for f in ctx.files.resources]
227232
),
228233
resource_strip_prefix=ctx.attr.resource_strip_prefix,
229-
resource_jars=",".join([f.path for f in ctx.files.resource_jars]),
234+
resource_jars=_join_path(ctx.files.resource_jars),
230235
dependency_analyzer_mode = dependency_analyzer_mode,
231236
statsfile_output = ctx.outputs.statsfile.path
232237
)
@@ -240,8 +245,8 @@ StatsfileOutput: {statsfile_output}
240245
outs = [ctx.outputs.jar, ctx.outputs.statsfile]
241246
if buildijar:
242247
outs.extend([ctx.outputs.ijar])
243-
ins = (list(compiler_classpath_jars) +
244-
list(dep_srcjars) +
248+
ins = (compiler_classpath_jars.to_list() +
249+
dep_srcjars.to_list() +
245250
list(srcjars) +
246251
list(sources) +
247252
ctx.files.srcs +
@@ -344,7 +349,7 @@ def collect_java_providers_of(deps):
344349

345350
def _compile_or_empty(ctx, jars, srcjars, buildijar, transitive_compile_jars, jars2labels, implicit_junit_deps_needed_for_java_compilation):
346351
# We assume that if a srcjar is present, it is not empty
347-
if len(ctx.files.srcs) + len(srcjars) == 0:
352+
if len(ctx.files.srcs) + len(srcjars.to_list()) == 0:
348353
_build_nosrc_jar(ctx, buildijar)
349354
# no need to build ijar when empty
350355
return struct(ijar=ctx.outputs.jar,
@@ -372,17 +377,17 @@ def _compile_or_empty(ctx, jars, srcjars, buildijar, transitive_compile_jars, ja
372377
full_jars=full_jars,
373378
ijars=ijars)
374379

375-
def _build_deployable(ctx, jars):
380+
def _build_deployable(ctx, jars_list):
376381
# This calls bazels singlejar utility.
377382
# For a full list of available command line options see:
378383
# https://github.com/bazelbuild/bazel/blob/master/src/java_tools/singlejar/java/com/google/devtools/build/singlejar/SingleJar.java#L311
379384
args = ["--normalize", "--sources"]
380-
args.extend([j.path for j in jars])
385+
args.extend([j.path for j in jars_list])
381386
if getattr(ctx.attr, "main_class", ""):
382387
args.extend(["--main_class", ctx.attr.main_class])
383388
args.extend(["--output", ctx.outputs.deploy_jar.path])
384389
ctx.actions.run(
385-
inputs=list(jars),
390+
inputs=jars_list,
386391
outputs=[ctx.outputs.deploy_jar],
387392
executable=ctx.executable._singlejar,
388393
mnemonic="ScalaDeployJar",
@@ -452,7 +457,7 @@ def _write_executable(ctx, rjars, main_class, jvm_flags, wrapper):
452457
template = ctx.attr._java_stub_template.files.to_list()[0]
453458
# RUNPATH is defined here:
454459
# 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
455-
classpath = ":".join(["${RUNPATH}%s" % (j.short_path) for j in rjars])
460+
classpath = ":".join(["${RUNPATH}%s" % (j.short_path) for j in rjars.to_list()])
456461
jvm_flags = " ".join([ctx.expand_location(f, ctx.attr.data) for f in jvm_flags])
457462
ctx.actions.expand_template(
458463
template = template,
@@ -471,11 +476,11 @@ def _write_executable(ctx, rjars, main_class, jvm_flags, wrapper):
471476
)
472477

473478
def collect_srcjars(targets):
474-
srcjars = depset()
479+
srcjars = []
475480
for target in targets:
476481
if hasattr(target, "srcjars"):
477-
srcjars += [target.srcjars.srcjar]
478-
return srcjars
482+
srcjars.append(target.srcjars.srcjar)
483+
return depset(srcjars)
479484

480485
def add_labels_of_jars_to(jars2labels, dependency, all_jars, direct_jars):
481486
for jar in direct_jars:
@@ -526,68 +531,68 @@ def filter_not_sources(deps):
526531
return depset([dep for dep in deps.to_list() if not_sources_jar(dep.basename) ])
527532

528533
def _collect_runtime_jars(dep_targets):
529-
runtime_jars = depset()
534+
runtime_jars = []
530535

531536
for dep_target in dep_targets:
532537
if java_common.provider in dep_target:
533-
runtime_jars += dep_target[java_common.provider].transitive_runtime_jars
538+
runtime_jars.append(dep_target[java_common.provider].transitive_runtime_jars)
534539
else:
535540
# support http_file pointed at a jar. http_jar uses ijar,
536541
# which breaks scala macros
537-
runtime_jars += filter_not_sources(dep_target.files)
542+
runtime_jars.append(filter_not_sources(dep_target.files))
538543

539544
return runtime_jars
540545

541546
def _collect_jars_when_dependency_analyzer_is_off(dep_targets):
542-
compile_jars = depset()
543-
runtime_jars = depset()
547+
compile_jars = []
548+
runtime_jars = []
544549

545550
for dep_target in dep_targets:
546551
if java_common.provider in dep_target:
547552
java_provider = dep_target[java_common.provider]
548-
compile_jars += java_provider.compile_jars
549-
runtime_jars += java_provider.transitive_runtime_jars
553+
compile_jars.append(java_provider.compile_jars)
554+
runtime_jars.append(java_provider.transitive_runtime_jars)
550555
else:
551556
# support http_file pointed at a jar. http_jar uses ijar,
552557
# which breaks scala macros
553-
compile_jars += filter_not_sources(dep_target.files)
554-
runtime_jars += filter_not_sources(dep_target.files)
558+
compile_jars.append(filter_not_sources(dep_target.files))
559+
runtime_jars.append(filter_not_sources(dep_target.files))
555560

556-
return struct(compile_jars = compile_jars,
557-
transitive_runtime_jars = runtime_jars,
561+
return struct(compile_jars = depset(transitive = compile_jars),
562+
transitive_runtime_jars = depset(transitive = runtime_jars),
558563
jars2labels = {},
559564
transitive_compile_jars = depset())
560565

561566
def _collect_jars_when_dependency_analyzer_is_on(dep_targets):
562-
transitive_compile_jars = depset()
567+
transitive_compile_jars = []
563568
jars2labels = {}
564-
compile_jars = depset()
565-
runtime_jars = depset()
569+
compile_jars = []
570+
runtime_jars = []
566571

567572
for dep_target in dep_targets:
568-
current_dep_compile_jars = depset()
569-
current_dep_transitive_compile_jars = depset()
573+
current_dep_compile_jars = None
574+
current_dep_transitive_compile_jars = None
570575

571576
if java_common.provider in dep_target:
572577
java_provider = dep_target[java_common.provider]
573578
current_dep_compile_jars = java_provider.compile_jars
574579
current_dep_transitive_compile_jars = java_provider.transitive_compile_time_jars
575-
runtime_jars += java_provider.transitive_runtime_jars
580+
runtime_jars.append(java_provider.transitive_runtime_jars)
576581
else:
577582
# support http_file pointed at a jar. http_jar uses ijar,
578583
# which breaks scala macros
579584
current_dep_compile_jars = filter_not_sources(dep_target.files)
580-
runtime_jars += filter_not_sources(dep_target.files)
581585
current_dep_transitive_compile_jars = filter_not_sources(dep_target.files)
586+
runtime_jars.append(filter_not_sources(dep_target.files))
582587

583-
compile_jars += current_dep_compile_jars
584-
transitive_compile_jars += current_dep_transitive_compile_jars
585-
add_labels_of_jars_to(jars2labels, dep_target, current_dep_transitive_compile_jars, current_dep_compile_jars)
588+
compile_jars.append(current_dep_compile_jars)
589+
transitive_compile_jars.append(current_dep_transitive_compile_jars)
590+
add_labels_of_jars_to(jars2labels, dep_target, current_dep_transitive_compile_jars.to_list(), current_dep_compile_jars.to_list())
586591

587-
return struct(compile_jars = compile_jars,
588-
transitive_runtime_jars = runtime_jars,
592+
return struct(compile_jars = depset(transitive = compile_jars),
593+
transitive_runtime_jars = depset(transitive = runtime_jars),
589594
jars2labels = jars2labels,
590-
transitive_compile_jars = transitive_compile_jars)
595+
transitive_compile_jars = depset(transitive = transitive_compile_jars))
591596

592597
def collect_jars(dep_targets, dependency_analyzer_is_off = True):
593598
"""Compute the runtime and compile-time dependencies from the given targets""" # noqa
@@ -620,7 +625,7 @@ def _collect_jars_from_common_ctx(ctx, extra_deps = [], extra_runtime_deps = [])
620625
deps_jars = collect_jars(ctx.attr.deps + auto_deps + extra_deps, dependency_analyzer_is_off)
621626
(cjars, transitive_rjars, jars2labels, transitive_compile_jars) = (deps_jars.compile_jars, deps_jars.transitive_runtime_jars, deps_jars.jars2labels, deps_jars.transitive_compile_jars)
622627

623-
transitive_rjars += _collect_runtime_jars(ctx.attr.runtime_deps + extra_runtime_deps)
628+
transitive_rjars = depset(transitive = [transitive_rjars] + _collect_runtime_jars(ctx.attr.runtime_deps + extra_runtime_deps))
624629

625630
return struct(compile_jars = cjars, transitive_runtime_jars = transitive_rjars, jars2labels=jars2labels, transitive_compile_jars = transitive_compile_jars)
626631

@@ -636,7 +641,7 @@ def create_java_provider(scalaattr, transitive_compile_time_jars):
636641
use_ijar = False,
637642
compile_time_jars = scalaattr.compile_jars,
638643
runtime_jars = scalaattr.transitive_runtime_jars,
639-
transitive_compile_time_jars = transitive_compile_time_jars + scalaattr.compile_jars,
644+
transitive_compile_time_jars = depset(transitive = [transitive_compile_time_jars, scalaattr.compile_jars]),
640645
transitive_runtime_jars = scalaattr.transitive_runtime_jars,
641646
)
642647
else:
@@ -692,14 +697,9 @@ def _lib(ctx, non_macro_lib):
692697
write_manifest(ctx)
693698
outputs = _compile_or_empty(ctx, cjars, srcjars, non_macro_lib, jars.transitive_compile_jars, jars.jars2labels, [])
694699

695-
transitive_rjars += outputs.full_jars
696-
697-
_build_deployable(ctx, transitive_rjars)
700+
transitive_rjars = depset(outputs.full_jars, transitive = [transitive_rjars])
698701

699-
# Now, need to setup providers for dependents
700-
# Notice that transitive_rjars just carries over from dependency analysis
701-
# but cjars 'resets' between cjars and next_cjars
702-
next_cjars = depset(outputs.ijars) # use ijar, if available, for future compiles
702+
_build_deployable(ctx, transitive_rjars.to_list())
703703

704704
# Using transitive_files since transitive_rjars a depset and avoiding linearization
705705
runfiles = ctx.runfiles(
@@ -711,13 +711,12 @@ def _lib(ctx, non_macro_lib):
711711
# Since after, will not show up in deploy_jar or old jars runfiles
712712
# Notice that compile_jars is intentionally transitive for exports
713713
exports_jars = collect_jars(ctx.attr.exports)
714-
next_cjars += exports_jars.compile_jars
715-
transitive_rjars += exports_jars.transitive_runtime_jars
714+
transitive_rjars = depset(transitive = [transitive_rjars, exports_jars.transitive_runtime_jars])
716715

717716
scalaattr = create_scala_provider(
718717
ijar = outputs.ijar,
719718
class_jar = outputs.class_jar,
720-
compile_jars = next_cjars,
719+
compile_jars = depset(outputs.ijars, transitive = [exports_jars.compile_jars]),
721720
transitive_runtime_jars = transitive_rjars,
722721
deploy_jar = ctx.outputs.deploy_jar,
723722
full_jars = outputs.full_jars,
@@ -760,14 +759,13 @@ def _scala_macro_library_impl(ctx):
760759
# Common code shared by all scala binary implementations.
761760
def _scala_binary_common(ctx, cjars, rjars, transitive_compile_time_jars, jars2labels, java_wrapper, implicit_junit_deps_needed_for_java_compilation = []):
762761
write_manifest(ctx)
763-
outputs = _compile_or_empty(ctx, cjars, [], False, transitive_compile_time_jars, jars2labels, implicit_junit_deps_needed_for_java_compilation) # no need to build an ijar for an executable
764-
rjars += outputs.full_jars
762+
outputs = _compile_or_empty(ctx, cjars, depset(), False, transitive_compile_time_jars, jars2labels, implicit_junit_deps_needed_for_java_compilation) # no need to build an ijar for an executable
763+
rjars = depset(outputs.full_jars, transitive = [rjars])
765764

766-
rjars_list = list(rjars)
767-
_build_deployable(ctx, rjars_list)
765+
_build_deployable(ctx, rjars.to_list())
768766

769767
runfiles = ctx.runfiles(
770-
files = rjars_list + [ctx.outputs.executable, java_wrapper] + ctx.files._java_runtime,
768+
transitive_files = depset([ctx.outputs.executable, java_wrapper] + ctx.files._java_runtime, transitive = [rjars]),
771769
collect_data = True)
772770

773771
scalaattr = create_scala_provider(
@@ -860,12 +858,13 @@ def _scala_test_impl(ctx):
860858
# _scalatest is an http_jar, so its compile jar is run through ijar
861859
# however, contains macros, so need to handle separately
862860
scalatest_jars = collect_jars([ctx.attr._scalatest]).transitive_runtime_jars
863-
cjars += scalatest_jars
864-
transitive_rjars += scalatest_jars
861+
cjars = depset(transitive = [cjars, scalatest_jars])
862+
transitive_rjars = depset(transitive = [transitive_rjars, scalatest_jars])
865863

866864
if is_dependency_analyzer_on(ctx):
867-
transitive_compile_jars += scalatest_jars
868-
add_labels_of_jars_to(jars_to_labels, ctx.attr._scalatest, scalatest_jars, scalatest_jars)
865+
transitive_compile_jars = depset(transitive = [scalatest_jars, transitive_compile_jars])
866+
scalatest_jars_list = scalatest_jars.to_list()
867+
add_labels_of_jars_to(jars_to_labels, ctx.attr._scalatest, scalatest_jars_list, scalatest_jars_list)
869868

870869
args = " ".join([
871870
"-R \"{path}\"".format(path=ctx.outputs.jar.short_path),
@@ -1206,7 +1205,8 @@ def scala_repositories():
12061205

12071206
def _sanitize_string_for_usage(s):
12081207
res_array = []
1209-
for c in s:
1208+
for idx in range(len(s)):
1209+
c = s[idx]
12101210
if c.isalnum() or c == ".":
12111211
res_array.append(c)
12121212
else:

test/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ scala_specs2_junit_test(
307307

308308
# Make sure scala_binary works in test environment
309309
[sh_test(
310-
name = "Run" + "".join([c if c.isalnum() else "_" for c in binary]),
310+
name = "Run" + "".join([binary[idx] if binary[idx].isalnum() else "_" for idx in range(len(binary))]),
311311
srcs = ["test_binary.sh"],
312312
args = ["$(location %s)" % binary],
313313
data = [binary if ":" in binary else ":%s" % binary],

test/aspect/aspect.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _rule_impl(ctx):
6565
""".format(name=target.label.name,
6666
expected=', '.join(expected),
6767
visited=', '.join(visited))
68-
ctx.file_action(
68+
ctx.actions.write(
6969
output = ctx.outputs.executable,
7070
content = content,
7171
)

test_rules_scala.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,11 @@ dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
722722
runner=$(get_test_runner "${1:-local}")
723723

724724
$runner bazel build test/...
725+
$runner bazel build "test/... --all_incompatible_changes"
725726
$runner bazel test test/...
726727
$runner bazel test third_party/...
727728
$runner bazel build "test/... --strict_java_deps=ERROR"
729+
$runner bazel build "test/... --strict_java_deps=ERROR --all_incompatible_changes"
728730
$runner bazel test "test/... --strict_java_deps=ERROR"
729731
$runner bazel run test/src/main/scala/scala/test/twitter_scrooge:justscrooges
730732
$runner bazel run test:JavaBinary

0 commit comments

Comments
 (0)