From 585faa22d2dee009e37e56b04956b504096c53e7 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Thu, 20 May 2021 11:44:34 +0200 Subject: [PATCH 1/6] Use crate_info.deps and others instead of ctx.rule.attr.deps in rust analyzer aspect --- docs/flatten.md | 1 + docs/rust_analyzer.md | 1 + rust/private/rust_analyzer.bzl | 5 +- test/rust_analyzer/BUILD.bazel | 49 +++++++++++++++++++ test/rust_analyzer/extra_proc_macro_dep.rs | 0 test/rust_analyzer/extra_test_dep.rs | 0 test/rust_analyzer/lib_dep.rs | 0 test/rust_analyzer/mylib.rs | 0 test/rust_analyzer/proc_macro_dep.rs | 0 test/rust_analyzer/rust_project_json_test.rs | 13 +++++ test/unit/rust_analyzer/BUILD.bazel | 4 ++ test/unit/rust_analyzer/mylib.rs | 0 .../unit/rust_analyzer/rust_analyzer_test.bzl | 49 +++++++++++++++++++ 13 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 test/rust_analyzer/BUILD.bazel create mode 100644 test/rust_analyzer/extra_proc_macro_dep.rs create mode 100644 test/rust_analyzer/extra_test_dep.rs create mode 100644 test/rust_analyzer/lib_dep.rs create mode 100644 test/rust_analyzer/mylib.rs create mode 100644 test/rust_analyzer/proc_macro_dep.rs create mode 100644 test/rust_analyzer/rust_project_json_test.rs create mode 100644 test/unit/rust_analyzer/BUILD.bazel create mode 100644 test/unit/rust_analyzer/mylib.rs create mode 100644 test/unit/rust_analyzer/rust_analyzer_test.bzl diff --git a/docs/flatten.md b/docs/flatten.md index 9606a49f68..ffc10f312a 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1721,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** diff --git a/docs/rust_analyzer.md b/docs/rust_analyzer.md index f6b2a5e196..8678da8629 100644 --- a/docs/rust_analyzer.md +++ b/docs/rust_analyzer.md @@ -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** diff --git a/rust/private/rust_analyzer.bzl b/rust/private/rust_analyzer.bzl index 15156f1ae9..3b16dcd574 100644 --- a/rust/private/rust_analyzer.bzl +++ b/rust/private/rust_analyzer.bzl @@ -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] @@ -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, diff --git a/test/rust_analyzer/BUILD.bazel b/test/rust_analyzer/BUILD.bazel new file mode 100644 index 0000000000..7b55126562 --- /dev/null +++ b/test/rust_analyzer/BUILD.bazel @@ -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, +) diff --git a/test/rust_analyzer/extra_proc_macro_dep.rs b/test/rust_analyzer/extra_proc_macro_dep.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/rust_analyzer/extra_test_dep.rs b/test/rust_analyzer/extra_test_dep.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/rust_analyzer/lib_dep.rs b/test/rust_analyzer/lib_dep.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/rust_analyzer/mylib.rs b/test/rust_analyzer/mylib.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/rust_analyzer/proc_macro_dep.rs b/test/rust_analyzer/proc_macro_dep.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/rust_analyzer/rust_project_json_test.rs b/test/rust_analyzer/rust_project_json_test.rs new file mode 100644 index 0000000000..175667e584 --- /dev/null +++ b/test/rust_analyzer/rust_project_json_test.rs @@ -0,0 +1,13 @@ +use std::env; + +fn main() { + let args: Vec = 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); + } + } +} diff --git a/test/unit/rust_analyzer/BUILD.bazel b/test/unit/rust_analyzer/BUILD.bazel new file mode 100644 index 0000000000..346d342008 --- /dev/null +++ b/test/unit/rust_analyzer/BUILD.bazel @@ -0,0 +1,4 @@ +load(":rust_analyzer_test.bzl", "rust_analyzer_test_suite") + +############################ UNIT TESTS ############################# +rust_analyzer_test_suite(name = "rust_analyzer_test_suite") diff --git a/test/unit/rust_analyzer/mylib.rs b/test/unit/rust_analyzer/mylib.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/unit/rust_analyzer/rust_analyzer_test.bzl b/test/unit/rust_analyzer/rust_analyzer_test.bzl new file mode 100644 index 0000000000..3f5271672b --- /dev/null +++ b/test/unit/rust_analyzer/rust_analyzer_test.bzl @@ -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", + ], + ) From 0e95409ff4b9f5b54c9bc9e12a7f2b15310d10fe Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Sun, 30 May 2021 07:22:19 +0200 Subject: [PATCH 2/6] Use runfiles instead --- test/rust_analyzer/BUILD.bazel | 3 ++- test/rust_analyzer/extra_proc_macro_dep.rs | 1 + test/rust_analyzer/extra_test_dep.rs | 1 + test/rust_analyzer/lib_dep.rs | 1 + test/rust_analyzer/mylib.rs | 1 + test/rust_analyzer/proc_macro_dep.rs | 1 + test/rust_analyzer/rust_project_json_test.rs | 17 ++++++++++++----- 7 files changed, 19 insertions(+), 6 deletions(-) diff --git a/test/rust_analyzer/BUILD.bazel b/test/rust_analyzer/BUILD.bazel index 7b55126562..9991609c99 100644 --- a/test/rust_analyzer/BUILD.bazel +++ b/test/rust_analyzer/BUILD.bazel @@ -43,7 +43,8 @@ rust_analyzer( rust_test( name = "rust_project_json_test", srcs = ["rust_project_json_test.rs"], - args = ["$(location :rust-project.json)"], data = [":rust-project.json"], + edition = "2018", harness = False, + deps = ["//tools/runfiles"], ) diff --git a/test/rust_analyzer/extra_proc_macro_dep.rs b/test/rust_analyzer/extra_proc_macro_dep.rs index e69de29bb2..8b13789179 100644 --- a/test/rust_analyzer/extra_proc_macro_dep.rs +++ b/test/rust_analyzer/extra_proc_macro_dep.rs @@ -0,0 +1 @@ + diff --git a/test/rust_analyzer/extra_test_dep.rs b/test/rust_analyzer/extra_test_dep.rs index e69de29bb2..8b13789179 100644 --- a/test/rust_analyzer/extra_test_dep.rs +++ b/test/rust_analyzer/extra_test_dep.rs @@ -0,0 +1 @@ + diff --git a/test/rust_analyzer/lib_dep.rs b/test/rust_analyzer/lib_dep.rs index e69de29bb2..8b13789179 100644 --- a/test/rust_analyzer/lib_dep.rs +++ b/test/rust_analyzer/lib_dep.rs @@ -0,0 +1 @@ + diff --git a/test/rust_analyzer/mylib.rs b/test/rust_analyzer/mylib.rs index e69de29bb2..8b13789179 100644 --- a/test/rust_analyzer/mylib.rs +++ b/test/rust_analyzer/mylib.rs @@ -0,0 +1 @@ + diff --git a/test/rust_analyzer/proc_macro_dep.rs b/test/rust_analyzer/proc_macro_dep.rs index e69de29bb2..8b13789179 100644 --- a/test/rust_analyzer/proc_macro_dep.rs +++ b/test/rust_analyzer/proc_macro_dep.rs @@ -0,0 +1 @@ + diff --git a/test/rust_analyzer/rust_project_json_test.rs b/test/rust_analyzer/rust_project_json_test.rs index 175667e584..758adee4b1 100644 --- a/test/rust_analyzer/rust_project_json_test.rs +++ b/test/rust_analyzer/rust_project_json_test.rs @@ -1,11 +1,18 @@ -use std::env; +use runfiles::Runfiles; fn main() { - let args: Vec = 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)); + let r = Runfiles::create().unwrap(); + let rust_project_path = r.rlocation("rules_rust/test/rust_analyzer/rust-project.json"); - for dep in &["lib_dep", "extra_test_dep", "proc_macro_dep", "extra_proc_macro_dep"] { + 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); } From 3cf7d7d4ba0a05a9fe21ec5b9082471c168747aa Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Tue, 1 Jun 2021 22:45:43 +0200 Subject: [PATCH 3/6] stuff --- test/rust_analyzer/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/rust_analyzer/BUILD.bazel b/test/rust_analyzer/BUILD.bazel index 9991609c99..c120cc24e2 100644 --- a/test/rust_analyzer/BUILD.bazel +++ b/test/rust_analyzer/BUILD.bazel @@ -45,6 +45,6 @@ rust_test( srcs = ["rust_project_json_test.rs"], data = [":rust-project.json"], edition = "2018", - harness = False, + use_libtest_harness = False, deps = ["//tools/runfiles"], ) From 12bcbc43ecbec0d937d74a6f45b188a53994f6f2 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Jun 2021 09:13:31 +0200 Subject: [PATCH 4/6] Rustfmt --- test/unit/rust_analyzer/mylib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/rust_analyzer/mylib.rs b/test/unit/rust_analyzer/mylib.rs index e69de29bb2..8b13789179 100644 --- a/test/unit/rust_analyzer/mylib.rs +++ b/test/unit/rust_analyzer/mylib.rs @@ -0,0 +1 @@ + From bb389c365e4ae17daf28f851e537cf7713e473bc Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Jun 2021 21:24:35 +0200 Subject: [PATCH 5/6] Rename tut -> target --- test/unit/rust_analyzer/rust_analyzer_test.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/rust_analyzer/rust_analyzer_test.bzl b/test/unit/rust_analyzer/rust_analyzer_test.bzl index 3f5271672b..7f600b9a0f 100644 --- a/test/unit/rust_analyzer/rust_analyzer_test.bzl +++ b/test/unit/rust_analyzer/rust_analyzer_test.bzl @@ -5,9 +5,9 @@ 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] + target = analysistest.target_under_test(env) + asserts.true(env, len(target.actions) == 1, "expected one action, got %s" % len(target.actions)) + action = target.actions[0] outputs = action.outputs.to_list() asserts.true(env, len(outputs) == 1, "expected one output, got %s" % len(outputs)) output = outputs[0] From 81669bc2799a00e18829f68b647a7a918845266a Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Jun 2021 21:41:18 +0200 Subject: [PATCH 6/6] Remove unit test --- test/unit/rust_analyzer/BUILD.bazel | 4 -- test/unit/rust_analyzer/mylib.rs | 1 - .../unit/rust_analyzer/rust_analyzer_test.bzl | 49 ------------------- 3 files changed, 54 deletions(-) delete mode 100644 test/unit/rust_analyzer/BUILD.bazel delete mode 100644 test/unit/rust_analyzer/mylib.rs delete mode 100644 test/unit/rust_analyzer/rust_analyzer_test.bzl diff --git a/test/unit/rust_analyzer/BUILD.bazel b/test/unit/rust_analyzer/BUILD.bazel deleted file mode 100644 index 346d342008..0000000000 --- a/test/unit/rust_analyzer/BUILD.bazel +++ /dev/null @@ -1,4 +0,0 @@ -load(":rust_analyzer_test.bzl", "rust_analyzer_test_suite") - -############################ UNIT TESTS ############################# -rust_analyzer_test_suite(name = "rust_analyzer_test_suite") diff --git a/test/unit/rust_analyzer/mylib.rs b/test/unit/rust_analyzer/mylib.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/test/unit/rust_analyzer/mylib.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/test/unit/rust_analyzer/rust_analyzer_test.bzl b/test/unit/rust_analyzer/rust_analyzer_test.bzl deleted file mode 100644 index 7f600b9a0f..0000000000 --- a/test/unit/rust_analyzer/rust_analyzer_test.bzl +++ /dev/null @@ -1,49 +0,0 @@ -"""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) - target = analysistest.target_under_test(env) - asserts.true(env, len(target.actions) == 1, "expected one action, got %s" % len(target.actions)) - action = target.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", - ], - )