Skip to content

Commit

Permalink
Use crate_info.deps and others instead of ctx.rule.attr.deps in rust …
Browse files Browse the repository at this point in the history
…analyzer aspect
  • Loading branch information
hlopko committed May 27, 2021
1 parent 7bf61a8 commit c51e89a
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 6 deletions.
5 changes: 3 additions & 2 deletions docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ When building the whole binary in Bazel, use `rust_library` instead.

<pre>
rust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>, <a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-deps">deps</a>,
<a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-out_dir_tar">out_dir_tar</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-srcs">srcs</a>,
<a href="#rust_test-version">version</a>)
<a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-harness">harness</a>, <a href="#rust_test-out_dir_tar">out_dir_tar</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>,
<a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-srcs">srcs</a>, <a href="#rust_test-version">version</a>)
</pre>

Builds a Rust test crate.
Expand Down Expand Up @@ -613,6 +613,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`.
| <a id="rust_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 | [] |
| <a id="rust_test-edition"></a>edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
| <a id="rust_test-env"></a>env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to <code>$(execpath)</code> and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="rust_test-harness"></a>harness | Whether to use libtest. | Boolean | optional | True |
| <a id="rust_test-out_dir_tar"></a>out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_test-proc_macro_deps"></a>proc_macro_deps | List of <code>rust_library</code> targets with kind <code>proc-macro</code> used to help build this library target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="rust_test-rustc_env"></a>rustc_env | Dictionary of additional <code>"key": "value"</code> environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
Expand Down
6 changes: 4 additions & 2 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ When building the whole binary in Bazel, use `rust_library` instead.

<pre>
rust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>, <a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-deps">deps</a>,
<a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-out_dir_tar">out_dir_tar</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-srcs">srcs</a>,
<a href="#rust_test-version">version</a>)
<a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-harness">harness</a>, <a href="#rust_test-out_dir_tar">out_dir_tar</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>,
<a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-srcs">srcs</a>, <a href="#rust_test-version">version</a>)
</pre>

Builds a Rust test crate.
Expand Down Expand Up @@ -1053,6 +1053,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`.
| <a id="rust_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 | [] |
| <a id="rust_test-edition"></a>edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
| <a id="rust_test-env"></a>env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to <code>$(execpath)</code> and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
| <a id="rust_test-harness"></a>harness | Whether to use libtest. | Boolean | optional | True |
| <a id="rust_test-out_dir_tar"></a>out_dir_tar | __Deprecated__, do not use, see [#cargo_build_script] instead. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
| <a id="rust_test-proc_macro_deps"></a>proc_macro_deps | List of <code>rust_library</code> targets with kind <code>proc-macro</code> used to help build this library target. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="rust_test-rustc_env"></a>rustc_env | Dictionary of additional <code>"key": "value"</code> environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
Expand Down Expand Up @@ -1720,6 +1721,7 @@ Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.js
| :------------- | :------------- |
| deps| String |
| proc_macro_deps| String |
| crate| 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 @@ -129,6 +129,7 @@ Annotates rust rules with RustAnalyzerInfo later used to build a rust-project.js
| :------------- | :------------- |
| deps| String |
| proc_macro_deps| String |
| crate| String |


**ATTRIBUTES**
Expand Down
9 changes: 8 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def _rust_test_common(ctx, toolchain, output):
toolchain = toolchain,
crate_type = crate_type,
crate_info = crate_info,
rust_flags = ["--test"],
rust_flags = ["--test"] if ctx.attr.harness else ["--cfg", "test"],
)

return _create_test_launcher(ctx, toolchain, output, providers)
Expand Down Expand Up @@ -703,6 +703,13 @@ _rust_test_attrs = {
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
"""),
),
"harness": attr.bool(
mandatory = False,
default = True,
doc = _tidy("""\
Whether to use libtest.
"""),
),
"_launcher": attr.label(
executable = True,
default = Label("//util/launcher:launcher"),
Expand Down
5 changes: 4 additions & 1 deletion rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _rust_analyzer_aspect_impl(target, ctx):
dep_infos = [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.deps if RustAnalyzerInfo in dep]
if hasattr(ctx.rule.attr, "proc_macro_deps"):
dep_infos += [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.proc_macro_deps if RustAnalyzerInfo in dep]
if hasattr(ctx.rule.attr, "crate"):
dep_infos.append(ctx.rule.attr.crate[RustAnalyzerInfo])

transitive_deps = depset(direct = dep_infos, order = "postorder", transitive = [dep.transitive_deps for dep in dep_infos])

crate_info = target[rust_common.crate_info]
Expand Down Expand Up @@ -102,7 +105,7 @@ def find_proc_macro_dylib_path(toolchain, target):
return None

rust_analyzer_aspect = aspect(
attr_aspects = ["deps", "proc_macro_deps"],
attr_aspects = ["deps", "proc_macro_deps", "crate"],
implementation = _rust_analyzer_aspect_impl,
toolchains = [str(Label("//rust:toolchain"))],
incompatible_use_toolchain_transition = True,
Expand Down
49 changes: 49 additions & 0 deletions test/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load("//rust:defs.bzl", "rust_analyzer", "rust_library", "rust_proc_macro", "rust_test")

rust_library(
name = "mylib",
srcs = ["mylib.rs"],
proc_macro_deps = [":proc_macro_dep"],
deps = [":lib_dep"],
)

rust_library(
name = "lib_dep",
srcs = ["lib_dep.rs"],
)

rust_proc_macro(
name = "proc_macro_dep",
srcs = ["proc_macro_dep.rs"],
)

rust_test(
name = "mylib_test",
crate = ":mylib",
proc_macro_deps = [":extra_proc_macro_dep"],
deps = [":extra_test_dep"],
)

rust_library(
name = "extra_test_dep",
srcs = ["extra_test_dep.rs"],
)

rust_proc_macro(
name = "extra_proc_macro_dep",
srcs = ["extra_proc_macro_dep.rs"],
)

rust_analyzer(
name = "rust_analyzer",
testonly = True,
targets = [":mylib_test"],
)

rust_test(
name = "rust_project_json_test",
srcs = ["rust_project_json_test.rs"],
args = ["$(location :rust-project.json)"],
data = [":rust-project.json"],
harness = False,
)
Empty file.
Empty file.
Empty file added test/rust_analyzer/lib_dep.rs
Empty file.
Empty file added test/rust_analyzer/mylib.rs
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions test/rust_analyzer/rust_project_json_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::env;

fn main() {
let args: Vec<String> = env::args().collect();
let rust_project_path = args.get(1).expect("expected rust-project.json path as first argument.");
let content = std::fs::read_to_string(rust_project_path).expect(&format!("couldn't open {}", rust_project_path));

for dep in &["lib_dep", "extra_test_dep", "proc_macro_dep", "extra_proc_macro_dep"] {
if !content.contains(dep) {
panic!("expected rust-project.json to contain {}.", dep);
}
}
}
4 changes: 4 additions & 0 deletions test/unit/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(":rust_analyzer_test.bzl", "rust_analyzer_test_suite")

############################ UNIT TESTS #############################
rust_analyzer_test_suite(name = "rust_analyzer_test_suite")
Empty file.
49 changes: 49 additions & 0 deletions test/unit/rust_analyzer/rust_analyzer_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Unittests for rust rules."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rust:defs.bzl", "rust_analyzer", "rust_library")

def _rust_analyzer_hello_world_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
asserts.true(env, len(tut.actions) == 1, "expected one action, got %s" % len(tut.actions))
action = tut.actions[0]
outputs = action.outputs.to_list()
asserts.true(env, len(outputs) == 1, "expected one output, got %s" % len(outputs))
output = outputs[0]
asserts.true(env, output.path.endswith("rust-project.json"))
return analysistest.end(env)

rust_analyzer_hello_world_test = analysistest.make(_rust_analyzer_hello_world_test_impl)

def _rust_analyzer_test():
rust_library(
name = "mylib",
srcs = ["mylib.rs"],
)

rust_analyzer(
name = "rust_analyzer",
testonly = True,
targets = [":mylib"],
)

rust_analyzer_hello_world_test(
name = "rust_analyzer_hello_world_test",
target_under_test = ":rust_analyzer",
)

def rust_analyzer_test_suite(name):
"""Entry-point macro called from the BUILD file.
Args:
name: Name of the macro.
"""
_rust_analyzer_test()

native.test_suite(
name = name,
tests = [
":rust_analyzer_hello_world_test",
],
)

0 comments on commit c51e89a

Please sign in to comment.