Skip to content

Commit f5c1cfd

Browse files
authored
Make scala_xxx rules declare that they provide JavaInfo (bazel-contrib#1643)
* make scala_xxx declare that they provide JavaInfo - Made scala_xxx rules declare they provide JavaInfo by setting the provides attribute. - This allows aspects to visit them when the aspect uses required_providers. - This matches functionality of java_xxx rules * lint fix
1 parent 9fccc25 commit f5c1cfd

File tree

10 files changed

+92
-0
lines changed

10 files changed

+92
-0
lines changed

scala/private/rules/scala_binary.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def make_scala_binary(*extras):
9292
],
9393
cfg = scala_version_transition,
9494
incompatible_use_toolchain_transition = True,
95+
provides = [JavaInfo],
9596
implementation = _scala_binary_impl,
9697
)
9798

scala/private/rules/scala_junit_test.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def make_scala_junit_test(*extras):
145145
],
146146
cfg = scala_version_transition,
147147
incompatible_use_toolchain_transition = True,
148+
provides = [JavaInfo],
148149
implementation = _scala_junit_test_impl,
149150
)
150151

scala/private/rules/scala_library.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def make_scala_library(*extras):
108108
],
109109
cfg = scala_version_transition,
110110
incompatible_use_toolchain_transition = True,
111+
provides = [JavaInfo],
111112
implementation = _scala_library_impl,
112113
)
113114

@@ -209,6 +210,7 @@ def make_scala_library_for_plugin_bootstrapping(*extras):
209210
],
210211
cfg = scala_version_transition,
211212
incompatible_use_toolchain_transition = True,
213+
provides = [JavaInfo],
212214
implementation = _scala_library_for_plugin_bootstrapping_impl,
213215
)
214216

@@ -284,6 +286,7 @@ def make_scala_macro_library(*extras):
284286
],
285287
cfg = scala_version_transition,
286288
incompatible_use_toolchain_transition = True,
289+
provides = [JavaInfo],
287290
implementation = _scala_macro_library_impl,
288291
)
289292

scala/private/rules/scala_repl.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def make_scala_repl(*extras):
8787
],
8888
cfg = scala_version_transition,
8989
incompatible_use_toolchain_transition = True,
90+
provides = [JavaInfo],
9091
implementation = _scala_repl_impl,
9192
)
9293

scala/private/rules/scala_test.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def make_scala_test(*extras):
131131
],
132132
cfg = scala_version_transition,
133133
incompatible_use_toolchain_transition = True,
134+
provides = [JavaInfo],
134135
implementation = _scala_test_impl,
135136
)
136137

scala/scala_import.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,5 @@ scala_import = rule(
150150
),
151151
},
152152
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
153+
provides = [JavaInfo],
153154
)

test/aspect/A.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scalarules.test
2+
3+
object A {
4+
def main(args: Array[String]) {
5+
println("4 8 15 16 23 42")
6+
}
7+
}

test/aspect/B.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scalarules.test
2+
3+
object B {
4+
def main(args: Array[String]) {
5+
println("4 8 15 16 23 42")
6+
}
7+
}

test/aspect/BUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
load(":aspect.bzl", "aspect_testscript")
2+
load(":javainfo_from_aspect_test.bzl", "javainfo_from_aspect_test")
23
load(
34
"//scala:scala.bzl",
45
"scala_junit_test",
56
"scala_library",
67
"scala_specs2_junit_test",
78
"scala_test",
89
)
10+
load("//scala:scala_import.bzl", "scala_import")
911

1012
aspect_testscript(
1113
name = "aspect_testscript",
@@ -39,3 +41,35 @@ scala_specs2_junit_test(
3941
srcs = ["FakeJunitTest.scala"],
4042
suffixes = ["Test"],
4143
)
44+
45+
#####################################
46+
#use ensure_javainfo_from_aspect_test() to make sure scala_xxx can be visited in an aspect that has required_aspects=[JavaInfo]
47+
48+
scala_import(
49+
name = "com_google_guava_guava_21_0",
50+
jars = ["@com_google_guava_guava_21_0_with_file//:guava-21.0.jar"],
51+
)
52+
53+
scala_library(
54+
name = "A",
55+
srcs = ["A.scala"],
56+
)
57+
58+
#This library depends on a scala_library and a scala_import
59+
scala_library(
60+
name = "B",
61+
deps = [
62+
"A",
63+
"com_google_guava_guava_21_0",
64+
],
65+
)
66+
67+
javainfo_from_aspect_test(
68+
name = "javainfo_from_aspect_test",
69+
expected = [
70+
"A",
71+
"com_google_guava_guava_21_0",
72+
],
73+
target = "B",
74+
target_under_test = "B",
75+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("@bazel_skylib//lib:unittest.bzl", "analysistest")
2+
3+
_VisitedRulesInfo = provider()
4+
5+
def _aspect_with_javainfo_required_imp(target, ctx):
6+
visited = dict([(target.label.name, "")])
7+
for dep in ctx.rule.attr.deps:
8+
visited.update(dep[_VisitedRulesInfo].visited)
9+
10+
return [
11+
_VisitedRulesInfo(visited = visited),
12+
]
13+
14+
#An aspect that has "required_providers = [JavaInfo]."
15+
#This is used to test that an aspect that has required_providers can access the JavaInfo provided by the scala_xxx targets
16+
_aspect_with_javainfo_required = aspect(
17+
attr_aspects = ["deps"],
18+
required_providers = [JavaInfo],
19+
implementation = _aspect_with_javainfo_required_imp,
20+
)
21+
22+
def _javainfo_from_aspect_test_imp(ctx):
23+
testenv = analysistest.begin(ctx)
24+
25+
for expected in ctx.attr.expected:
26+
if (expected not in ctx.attr.target[_VisitedRulesInfo].visited):
27+
analysistest.fail(testenv, "Aspect did not visit expected target %s" % ctx.attr.expected)
28+
return analysistest.end(testenv)
29+
30+
javainfo_from_aspect_test = analysistest.make(
31+
impl = _javainfo_from_aspect_test_imp,
32+
attrs = {
33+
"target": attr.label(aspects = [_aspect_with_javainfo_required]),
34+
"expected": attr.string_list(), #The targets we expect to be visited by the aspect
35+
},
36+
)

0 commit comments

Comments
 (0)