Skip to content

Commit

Permalink
[prost] includes prost crate infos in rust_analyzer_aspect (#2232)
Browse files Browse the repository at this point in the history
The change is fairly straightforward, but I did need to move prost
providers up to `prost/providers.bzl` and out of `private/prost.bzl` to
avoid a cyclic dependency.

What we do is:
* change `rust_prost_library` to return the `rust_proto_info` provider
* in the `rust-analyzer` aspect we iterate over transitive dep infos and
  generate the appropriate `RustAnalyzerInfo`
* lastly, we use the `crate_info` from `ProstProtoInfo` if we're
  processing a prost target as the "root" `crate_info`

There are a couple of things I'm not sure are correct:
1. For every prost dep I set `deps` to `[]` - I think that's correct but
   I'm not sure-sure.
2. Similarly I set `proc_macro_dylib_path` to always `None` - again I
   think that's correct, but not sure-sure.

Fixes #2121.

---------

Co-authored-by: UebelAndre <[email protected]>
  • Loading branch information
iamricard and UebelAndre authored Nov 1, 2023
1 parent 4aafa0a commit 25411ee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,7 @@ Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.js
| proc_macro_deps| String |
| crate| String |
| actual| String |
| proto| String |


**ATTRIBUTES**
Expand Down
1 change: 1 addition & 0 deletions docs/rust_analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.js
| proc_macro_deps| String |
| crate| String |
| actual| String |
| proto| String |


**ATTRIBUTES**
Expand Down
12 changes: 2 additions & 10 deletions proto/prost/private/prost.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Rules for building protos in Rust with Prost and Tonic."""

load("@rules_proto//proto:defs.bzl", "ProtoInfo", "proto_common")
load("//proto/prost:providers.bzl", "ProstProtoInfo")
load("//rust:defs.bzl", "rust_common")

# buildifier: disable=bzl-visibility
Expand All @@ -13,16 +14,6 @@ RUST_EDITION = "2021"

TOOLCHAIN_TYPE = "@rules_rust//proto/prost:toolchain_type"

ProstProtoInfo = provider(
doc = "Rust Prost provider info",
fields = {
"dep_variant_info": "DepVariantInfo: For the compiled Rust gencode (also covers its " +
"transitive dependencies)",
"package_info": "File: A newline delimited file of `--extern_path` values for protoc.",
"transitive_dep_infos": "depset[DepVariantInfo]: Transitive dependencies of the compiled crate.",
},
)

def _create_proto_lang_toolchain(ctx, prost_toolchain):
proto_lang_toolchain = proto_common.ProtoLangToolchainInfo(
out_replacement_format_flag = "--prost_out=%s",
Expand Down Expand Up @@ -328,6 +319,7 @@ def _rust_prost_library_impl(ctx):

return [
DefaultInfo(files = depset([dep_variant_info.crate_info.output])),
rust_proto_info,
rust_common.crate_group_info(
dep_variant_infos = depset(
[dep_variant_info],
Expand Down
11 changes: 11 additions & 0 deletions proto/prost/providers.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Module containing definitions of all Prost providers."""

ProstProtoInfo = provider(
doc = "Rust Prost provider info",
fields = {
"dep_variant_info": "DepVariantInfo: For the compiled Rust gencode (also covers its " +
"transitive dependencies)",
"package_info": "File: A newline delimited file of `--extern_path` values for protoc.",
"transitive_dep_infos": "depset[DepVariantInfo]: Transitive dependencies of the compiled crate.",
},
)
33 changes: 27 additions & 6 deletions rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ given targets. This file can be consumed by rust-analyzer as an alternative
to Cargo.toml files.
"""

load("//proto/prost:providers.bzl", "ProstProtoInfo")
load("//rust/platform:triple_mappings.bzl", "system_to_dylib_ext", "triple_to_system")
load("//rust/private:common.bzl", "rust_common")
load("//rust/private:rustc.bzl", "BuildInfo")
Expand Down Expand Up @@ -99,12 +100,32 @@ def _rust_analyzer_aspect_impl(target, ctx):
dep_infos.append(ctx.rule.attr.actual[RustAnalyzerInfo])

if RustAnalyzerGroupInfo in ctx.rule.attr.actual:
dep_infos.extend(ctx.rule.attr.actul[RustAnalyzerGroupInfo])

if rust_common.crate_group_info in target:
dep_infos.extend(ctx.rule.attr.actual[RustAnalyzerGroupInfo])

if ProstProtoInfo in target:
for info in target[ProstProtoInfo].transitive_dep_infos.to_list():
crate_info = info.crate_info
crate_spec = ctx.actions.declare_file(crate_info.owner.name + ".rust_analyzer_crate_spec")
rust_analyzer_info = RustAnalyzerInfo(
crate = crate_info,
cfgs = cfgs,
env = crate_info.rustc_env,
deps = [],
crate_specs = depset(direct = [crate_spec]),
proc_macro_dylib_path = None,
build_info = info.build_info,
)
ctx.actions.write(
output = crate_spec,
content = json.encode(_create_single_crate(ctx, rust_analyzer_info)),
)
dep_infos.append(rust_analyzer_info)

if ProstProtoInfo in target:
crate_info = target[ProstProtoInfo].dep_variant_info.crate_info
elif rust_common.crate_group_info in target:
return [RustAnalyzerGroupInfo(deps = dep_infos)]

if rust_common.crate_info in target:
elif rust_common.crate_info in target:
crate_info = target[rust_common.crate_info]
elif rust_common.test_crate_info in target:
crate_info = target[rust_common.test_crate_info].crate
Expand Down Expand Up @@ -163,7 +184,7 @@ def find_proc_macro_dylib_path(toolchain, target):
return None

rust_analyzer_aspect = aspect(
attr_aspects = ["deps", "proc_macro_deps", "crate", "actual"],
attr_aspects = ["deps", "proc_macro_deps", "crate", "actual", "proto"],
implementation = _rust_analyzer_aspect_impl,
toolchains = [str(Label("//rust:toolchain_type"))],
incompatible_use_toolchain_transition = True,
Expand Down

0 comments on commit 25411ee

Please sign in to comment.