Skip to content

Commit

Permalink
Add proc_macro_deps for rust_doc_test (#2928)
Browse files Browse the repository at this point in the history
This allows representing doctests that
depend on proc macros their crate do not depend on.
  • Loading branch information
erenon authored Nov 8, 2024
1 parent cf6e76e commit 66bcafe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/src/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing
## rust_doc_test

<pre>
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-deps">deps</a>, <a href="#rust_doc_test-crate">crate</a>)
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-deps">deps</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-proc_macro_deps">proc_macro_deps</a>)
</pre>

Runs Rust documentation tests.
Expand Down Expand Up @@ -563,6 +563,7 @@ Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation t
| <a id="rust_doc_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_doc_test-deps"></a>deps | List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="rust_doc_test-crate"></a>crate | The label of the target to generate code documentation for. `rust_doc_test` can generate HTML code documentation for the source files of `rust_library` or `rust_binary` targets. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_doc_test-proc_macro_deps"></a>proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


<a id="rust_grpc_library"></a>
Expand Down
3 changes: 2 additions & 1 deletion docs/src/rust_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing
## rust_doc_test

<pre>
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-deps">deps</a>, <a href="#rust_doc_test-crate">crate</a>)
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-deps">deps</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-proc_macro_deps">proc_macro_deps</a>)
</pre>

Runs Rust documentation tests.
Expand Down Expand Up @@ -117,5 +117,6 @@ Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation t
| <a id="rust_doc_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_doc_test-deps"></a>deps | List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="rust_doc_test-crate"></a>crate | The label of the target to generate code documentation for. `rust_doc_test` can generate HTML code documentation for the source files of `rust_library` or `rust_binary` targets. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_doc_test-proc_macro_deps"></a>proc_macro_deps | List of `rust_proc_macro` targets used to help build this library target. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |


10 changes: 9 additions & 1 deletion rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ def _rust_doc_test_impl(ctx):

crate = ctx.attr.crate[rust_common.crate_info]
deps = transform_deps(ctx.attr.deps)
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps)

crate_info = rust_common.create_crate_info(
name = crate.name,
type = crate.type,
root = crate.root,
srcs = crate.srcs,
deps = depset(deps, transitive = [crate.deps]),
proc_macro_deps = crate.proc_macro_deps,
proc_macro_deps = depset(proc_macro_deps, transitive = [crate.proc_macro_deps]),
aliases = crate.aliases,
output = crate.output,
edition = crate.edition,
Expand Down Expand Up @@ -207,6 +208,13 @@ rust_doc_test = rule(
"""),
providers = [[CrateInfo], [CcInfo]],
),
"proc_macro_deps": attr.label_list(
doc = dedent("""\
List of `rust_proc_macro` targets used to help build this library target.
"""),
cfg = "exec",
providers = [rust_common.crate_info],
),
"_cc_toolchain": attr.label(
doc = (
"In order to use find_cc_toolchain, your rule has to depend " +
Expand Down
10 changes: 9 additions & 1 deletion test/unit/rustdoc/rustdoc_unit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ rustdoc_with_json_error_format_test = analysistest.make(_rustdoc_with_json_error
str(Label("//:error_format")): "json",
})

def _target_maker(rule_fn, name, rustdoc_deps = [], **kwargs):
def _target_maker(rule_fn, name, rustdoc_deps = [], rustdoc_proc_macro_deps = [], **kwargs):
rule_fn(
name = name,
edition = "2018",
Expand All @@ -184,6 +184,7 @@ def _target_maker(rule_fn, name, rustdoc_deps = [], **kwargs):
name = "{}_doctest".format(name),
crate = ":{}".format(name),
deps = rustdoc_deps,
proc_macro_deps = rustdoc_proc_macro_deps,
)

def _define_targets():
Expand Down Expand Up @@ -250,6 +251,13 @@ def _define_targets():
proc_macro_deps = [":rustdoc_proc_macro"],
)

_target_maker(
rust_library,
name = "lib_with_proc_macro_only_in_docs",
srcs = ["procmacro_in_rustdoc.rs"],
rustdoc_proc_macro_deps = [":rustdoc_proc_macro"],
)

_target_maker(
rust_library,
name = "lib_nodep_with_proc_macro",
Expand Down

0 comments on commit 66bcafe

Please sign in to comment.