Skip to content

Commit a665447

Browse files
authored
Traverse custom alias-like rules in the rust analyzer aspect (#1190)
1 parent 4504983 commit a665447

File tree

8 files changed

+51
-6
lines changed

8 files changed

+51
-6
lines changed

docs/flatten.md

+1
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,7 @@ Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.js
17401740
| deps| String |
17411741
| proc_macro_deps| String |
17421742
| crate| String |
1743+
| actual| String |
17431744

17441745

17451746
**ATTRIBUTES**

docs/rust_analyzer.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.js
109109
| deps| String |
110110
| proc_macro_deps| String |
111111
| crate| String |
112+
| actual| String |
112113

113114

114115
**ATTRIBUTES**

rust/private/rust_analyzer.bzl

+10-6
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,21 @@ def _rust_analyzer_aspect_impl(target, ctx):
5252
if hasattr(ctx.rule.attr, "rustc_flags"):
5353
cfgs += [f[6:] for f in ctx.rule.attr.rustc_flags if f.startswith("--cfg ") or f.startswith("--cfg=")]
5454

55-
# Save BuildInfo if we find any (for build script output)
5655
build_info = None
57-
for dep in ctx.rule.attr.deps:
58-
if BuildInfo in dep:
59-
build_info = dep[BuildInfo]
56+
dep_infos = []
57+
if hasattr(ctx.rule.attr, "deps"):
58+
for dep in ctx.rule.attr.deps:
59+
# Save BuildInfo if we find any (for build script output)
60+
if BuildInfo in dep:
61+
build_info = dep[BuildInfo]
62+
dep_infos = [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.deps if RustAnalyzerInfo in dep]
6063

61-
dep_infos = [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.deps if RustAnalyzerInfo in dep]
6264
if hasattr(ctx.rule.attr, "proc_macro_deps"):
6365
dep_infos += [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.proc_macro_deps if RustAnalyzerInfo in dep]
6466
if hasattr(ctx.rule.attr, "crate") and ctx.rule.attr.crate != None:
6567
dep_infos.append(ctx.rule.attr.crate[RustAnalyzerInfo])
68+
if hasattr(ctx.rule.attr, "actual") and ctx.rule.attr.actual != None and RustAnalyzerInfo in ctx.rule.attr.actual:
69+
dep_infos.append(ctx.rule.attr.actual[RustAnalyzerInfo])
6670

6771
crate_spec = ctx.actions.declare_file(ctx.label.name + ".rust_analyzer_crate_spec")
6872

@@ -111,7 +115,7 @@ def find_proc_macro_dylib_path(toolchain, target):
111115
return None
112116

113117
rust_analyzer_aspect = aspect(
114-
attr_aspects = ["deps", "proc_macro_deps", "crate"],
118+
attr_aspects = ["deps", "proc_macro_deps", "crate", "actual"],
115119
implementation = _rust_analyzer_aspect_impl,
116120
toolchains = [str(Label("//rust:toolchain"))],
117121
incompatible_use_toolchain_transition = True,

test/rust_analyzer/aspect_traversal_test/BUILD.bazel

+18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_proc_macro", "rust_test")
2+
load(":custom_alias.bzl", "custom_alias")
23

34
rust_library(
45
name = "mylib",
56
srcs = ["mylib.rs"],
67
proc_macro_deps = [":proc_macro_dep"],
78
deps = [
89
":alias_dep",
10+
":custom_alias_dep",
911
":lib_dep",
1012
],
1113
)
@@ -31,6 +33,22 @@ rust_library(
3133
srcs = ["dep_of_aliased_dep.rs"],
3234
)
3335

36+
custom_alias(
37+
name = "custom_alias_dep",
38+
actual = ":custom_actual_dep",
39+
)
40+
41+
rust_library(
42+
name = "custom_actual_dep",
43+
srcs = ["custom_actual_dep.rs"],
44+
deps = [":dep_of_custom_aliased_dep"],
45+
)
46+
47+
rust_library(
48+
name = "dep_of_custom_aliased_dep",
49+
srcs = ["dep_of_custom_aliased_dep.rs"],
50+
)
51+
3452
rust_proc_macro(
3553
name = "proc_macro_dep",
3654
srcs = ["proc_macro_dep.rs"],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Alias-like rule for testing."""
2+
3+
load("@rules_rust//rust:defs.bzl", "rust_common")
4+
5+
def _custom_alias_impl(ctx):
6+
actual = ctx.attr.actual
7+
return [actual[rust_common.crate_info], actual[rust_common.dep_info]]
8+
9+
custom_alias = rule(
10+
implementation = _custom_alias_impl,
11+
attrs = {
12+
"actual": attr.label(
13+
allow_single_file = True,
14+
mandatory = True,
15+
),
16+
},
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/rust_analyzer/aspect_traversal_test/rust_project_json_test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ mod tests {
1414
"lib_dep",
1515
"actual_dep",
1616
"dep_of_aliased_dep",
17+
"custom_actual_dep",
18+
"dep_of_custom_aliased_dep",
1719
"extra_test_dep",
1820
"proc_macro_dep",
1921
"extra_proc_macro_dep",

0 commit comments

Comments
 (0)