Skip to content

Commit 207ee4f

Browse files
feat: support extra deps in rust_doc_test (#1286)
* feat: support extra deps in rust_doc_test Currently, `rust_doc_test` (unlike `rust_test`) doesn't support adding extra test-only dependencies, which makes it unnecessarily hard to migrate doc tests from Cargo to rules_rust. This change extends the `rust_doc_test` rust to support extra test-only dependencies. * sort dictionary items I'm getting unnecessarily proficient in this. * Regenerate documentation * apply review feedback * sort load statements
1 parent 4e7d114 commit 207ee4f

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

docs/flatten.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing
465465
## rust_doc_test
466466

467467
<pre>
468-
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-experimental_use_whole_archive_for_native_deps">experimental_use_whole_archive_for_native_deps</a>)
468+
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-deps">deps</a>, <a href="#rust_doc_test-experimental_use_whole_archive_for_native_deps">experimental_use_whole_archive_for_native_deps</a>)
469469
</pre>
470470

471471
Runs Rust documentation tests.
@@ -513,6 +513,7 @@ Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation t
513513
| :------------- | :------------- | :------------- | :------------- | :------------- |
514514
| <a id="rust_doc_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
515515
| <a id="rust_doc_test-crate"></a>crate | The label of the target to generate code documentation for. <code>rust_doc_test</code> can generate HTML code documentation for the source files of <code>rust_library</code> or <code>rust_binary</code> targets. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
516+
| <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 <code>rust_library</code> targets or <code>cc_library</code> targets if linking a native library. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
516517
| <a id="rust_doc_test-experimental_use_whole_archive_for_native_deps"></a>experimental_use_whole_archive_for_native_deps | Whether to use +whole-archive linking modifier for native dependencies.<br><br>TODO: This is a stopgap feature and will be removed, see https://github.com/bazelbuild/rules_rust/issues/1268. | Boolean | optional | False |
517518

518519

docs/rust_doc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Running `bazel build //hello_lib:hello_lib_doc` will build a zip file containing
6969
## rust_doc_test
7070

7171
<pre>
72-
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-experimental_use_whole_archive_for_native_deps">experimental_use_whole_archive_for_native_deps</a>)
72+
rust_doc_test(<a href="#rust_doc_test-name">name</a>, <a href="#rust_doc_test-crate">crate</a>, <a href="#rust_doc_test-deps">deps</a>, <a href="#rust_doc_test-experimental_use_whole_archive_for_native_deps">experimental_use_whole_archive_for_native_deps</a>)
7373
</pre>
7474

7575
Runs Rust documentation tests.
@@ -117,6 +117,7 @@ Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation t
117117
| :------------- | :------------- | :------------- | :------------- | :------------- |
118118
| <a id="rust_doc_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
119119
| <a id="rust_doc_test-crate"></a>crate | The label of the target to generate code documentation for. <code>rust_doc_test</code> can generate HTML code documentation for the source files of <code>rust_library</code> or <code>rust_binary</code> targets. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
120+
| <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 <code>rust_library</code> targets or <code>cc_library</code> targets if linking a native library. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
120121
| <a id="rust_doc_test-experimental_use_whole_archive_for_native_deps"></a>experimental_use_whole_archive_for_native_deps | Whether to use +whole-archive linking modifier for native dependencies.<br><br>TODO: This is a stopgap feature and will be removed, see https://github.com/bazelbuild/rules_rust/issues/1268. | Boolean | optional | False |
121122

122123

rust/private/rustdoc_test.bzl

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
"""Rules for performing `rustdoc --test` on Bazel built crates"""
1616

1717
load("//rust/private:common.bzl", "rust_common")
18+
load("//rust/private:providers.bzl", "CrateInfo")
1819
load("//rust/private:rustdoc.bzl", "rustdoc_compile_action")
19-
load("//rust/private:utils.bzl", "dedent", "find_toolchain")
20+
load("//rust/private:utils.bzl", "dedent", "find_toolchain", "transform_deps")
2021

2122
def _construct_writer_arguments(ctx, test_runner, action, crate_info):
2223
"""Construct arguments and environment variables specific to `rustdoc_test_writer`.
@@ -90,8 +91,25 @@ def _rust_doc_test_impl(ctx):
9091

9192
toolchain = find_toolchain(ctx)
9293

93-
crate = ctx.attr.crate
94-
crate_info = crate[rust_common.crate_info]
94+
crate = ctx.attr.crate[rust_common.crate_info]
95+
deps = transform_deps(ctx.attr.deps)
96+
97+
crate_info = rust_common.create_crate_info(
98+
name = crate.name,
99+
type = crate.type,
100+
root = crate.root,
101+
srcs = crate.srcs,
102+
deps = depset(deps, transitive = [crate.deps]),
103+
proc_macro_deps = crate.proc_macro_deps,
104+
aliases = {},
105+
output = crate.output,
106+
edition = crate.edition,
107+
rustc_env = crate.rustc_env,
108+
is_test = True,
109+
compile_data = crate.compile_data,
110+
wrapped_crate_type = crate.type,
111+
owner = ctx.label,
112+
)
95113

96114
if toolchain.os == "windows":
97115
test_runner = ctx.actions.declare_file(ctx.label.name + ".rustdoc_test.bat")
@@ -127,7 +145,7 @@ def _rust_doc_test_impl(ctx):
127145

128146
ctx.actions.run(
129147
mnemonic = "RustdocTestWriter",
130-
progress_message = "Generating Rustdoc test runner for {}".format(crate.label),
148+
progress_message = "Generating Rustdoc test runner for {}".format(ctx.attr.crate.label),
131149
executable = ctx.executable._test_writer,
132150
inputs = action.inputs,
133151
tools = tools,
@@ -154,6 +172,15 @@ rust_doc_test = rule(
154172
providers = [rust_common.crate_info],
155173
mandatory = True,
156174
),
175+
"deps": attr.label_list(
176+
doc = dedent("""\
177+
List of other libraries to be linked to this library target.
178+
179+
These can be either other `rust_library` targets or `cc_library` targets if
180+
linking a native library.
181+
"""),
182+
providers = [CrateInfo, CcInfo],
183+
),
157184
"experimental_use_whole_archive_for_native_deps": attr.bool(
158185
doc = dedent("""\
159186
Whether to use +whole-archive linking modifier for native dependencies.

test/unit/rustdoc/adder.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/// Increments the input.
16+
pub fn inc(n: u32) -> u32 {
17+
n + 1
18+
}

test/unit/rustdoc/rustdoc_lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ make_answer!();
99
/// fn answer() -> u32 { 42 }
1010
/// assert_eq!(answer(), 42);
1111
/// ```
12+
///
13+
/// ```
14+
/// use adder::inc;
15+
/// assert_eq!(inc(41), 42);
16+
/// ```
1217
#[cfg(not(feature = "with_proc_macro"))]
1318
pub fn answer() -> u32 {
1419
42

test/unit/rustdoc/rustdoc_unit_test.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ rustdoc_for_lib_with_proc_macro_test = analysistest.make(_rustdoc_for_lib_with_p
7474
rustdoc_for_bin_with_transitive_proc_macro_test = analysistest.make(_rustdoc_for_bin_with_transitive_proc_macro_test_impl)
7575
rustdoc_for_lib_with_cc_lib_test = analysistest.make(_rustdoc_for_lib_with_cc_lib_test_impl)
7676

77-
def _target_maker(rule_fn, name, **kwargs):
77+
def _target_maker(rule_fn, name, rustdoc_deps = [], **kwargs):
7878
rule_fn(
7979
name = name,
8080
**kwargs
@@ -93,9 +93,12 @@ def _target_maker(rule_fn, name, **kwargs):
9393
rust_doc_test(
9494
name = "{}_doctest".format(name),
9595
crate = ":{}".format(name),
96+
deps = rustdoc_deps,
9697
)
9798

9899
def _define_targets():
100+
rust_library(name = "adder", srcs = ["adder.rs"])
101+
99102
_target_maker(
100103
rust_binary,
101104
name = "bin",
@@ -106,6 +109,7 @@ def _define_targets():
106109
rust_library,
107110
name = "lib",
108111
srcs = ["rustdoc_lib.rs"],
112+
rustdoc_deps = [":adder"],
109113
)
110114

111115
_target_maker(
@@ -119,6 +123,7 @@ def _define_targets():
119123
rust_library,
120124
name = "lib_with_proc_macro",
121125
srcs = ["rustdoc_lib.rs"],
126+
rustdoc_deps = [":adder"],
122127
proc_macro_deps = [":rustdoc_proc_macro"],
123128
crate_features = ["with_proc_macro"],
124129
)
@@ -142,6 +147,7 @@ def _define_targets():
142147
rust_library,
143148
name = "lib_with_cc",
144149
srcs = ["rustdoc_lib.rs"],
150+
rustdoc_deps = [":adder"],
145151
crate_features = ["with_cc"],
146152
deps = [":cc_lib"],
147153
)

0 commit comments

Comments
 (0)