Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Nov 11, 2021
1 parent 602a26d commit 2f45838
Show file tree
Hide file tree
Showing 25 changed files with 254 additions and 168 deletions.
5 changes: 5 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ tasks:
platform: ubuntu2004
run_targets:
- "//test/rustfmt:test_runner"
rust_analyzer_tests:
name: Rust-Analyzer Tests
platform: ubuntu2004
run_targets:
- "//test/rust_analyzer:rust_analyzer_test"
ubuntu2004_examples:
name: Examples
platform: ubuntu2004
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@

# BazelCI
bazelci.py

# rust-analyzer
rust-project.json
4 changes: 0 additions & 4 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ load(
"//rust/private:rust_analyzer.bzl",
_rust_analyzer = "rust_analyzer",
_rust_analyzer_aspect = "rust_analyzer_aspect",
_rust_analyzer_detect_sysroot = "rust_analyzer_detect_sysroot",
)
load(
"//rust/private:rustc.bzl",
Expand Down Expand Up @@ -108,9 +107,6 @@ rust_common = _rust_common
rust_analyzer_aspect = _rust_analyzer_aspect
# See @rules_rust//rust/private:rust_analyzer.bzl for a complete description.

rust_analyzer_detect_sysroot = _rust_analyzer_detect_sysroot
# See @rules_rust//rust/private:rust_analyzer.bzl for a complete description.

rust_analyzer = _rust_analyzer
# See @rules_rust//rust/private:rust_analyzer.bzl for a complete description.

Expand Down
6 changes: 6 additions & 0 deletions rust/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot")
load("//rust/private:stamp.bzl", "stamp_build_setting")

bzl_library(
Expand All @@ -13,3 +14,8 @@ bzl_library(
)

stamp_build_setting(name = "stamp")

rust_analyzer_detect_sysroot(
name = "rust_analyzer_detect_sysroot",
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ RustAnalyzerInfo = provider(
fields = {
"build_info": "BuildInfo: build info for this crate if present",
"cfgs": "List[String]: features or other compilation --cfg settings",
"crate_specs": "List[File]: transitive closure of OutputGroupInfo files",
"crate": "rust_common.crate_info",
"crate_specs": "List[File]: transitive closure of OutputGroupInfo files",
"deps": "List[RustAnalyzerInfo]: direct dependencies",
"env": "Dict{String: String}: Environment variables, used for the `env!` macro",
"proc_macro_dylib_path": "File: compiled shared library output of proc-macro rule",
Expand Down
5 changes: 5 additions & 0 deletions test/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sh_binary(
name = "rust_analyzer_test",
srcs = ["rust_analyzer_test_runner.sh"],
args = [package_name()],
)
14 changes: 6 additions & 8 deletions test/rust_analyzer/aspect_traversal_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//rust:defs.bzl", "rust_analyzer", "rust_library", "rust_proc_macro", "rust_test")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_proc_macro", "rust_test")

rust_library(
name = "mylib",
Expand Down Expand Up @@ -34,16 +34,14 @@ rust_proc_macro(
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"],
data = [":rust-project.json"],
edition = "2018",
deps = ["//tools/runfiles"],
env = {"RUST_PROJECT_JSON": "$(rootpath :rust-project.json)"},
# This target is tagged as manual since it's not expected to pass in
# contexts outside of `//test/rust_analyzer:rust_analyzer_test`. Run
# that target to execute this test.
tags = ["manual"],
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#[cfg(test)]
mod tests {
use runfiles::Runfiles;
use std::env;
use std::path::PathBuf;

#[test]
fn test_aspect_traverses_all_the_right_corners_of_target_graph() {
let r = Runfiles::create().unwrap();
let rust_project_path =
r.rlocation("rules_rust/test/rust_analyzer/aspect_traversal_test/rust-project.json");
let rust_project_path = PathBuf::from(env::var("RUST_PROJECT_JSON").unwrap());

let content = std::fs::read_to_string(&rust_project_path)
.unwrap_or_else(|_| panic!("couldn't open {:?}", &rust_project_path));
Expand Down
18 changes: 6 additions & 12 deletions test/rust_analyzer/merging_crates_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//rust:defs.bzl", "rust_analyzer", "rust_library", "rust_test")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

rust_library(
name = "mylib",
Expand All @@ -22,20 +22,14 @@ rust_library(
srcs = ["extra_test_dep.rs"],
)

rust_analyzer(
name = "rust_analyzer",
testonly = True,
targets = [
# it's significant that `mylib` goes before `mylib_test`.
":mylib",
":mylib_test",
],
)

rust_test(
name = "rust_project_json_test",
srcs = ["rust_project_json_test.rs"],
data = [":rust-project.json"],
edition = "2018",
deps = ["//tools/runfiles"],
env = {"RUST_PROJECT_JSON": "$(rootpath :rust-project.json)"},
# This target is tagged as manual since it's not expected to pass in
# contexts outside of `//test/rust_analyzer:rust_analyzer_test`. Run
# that target to execute this test.
tags = ["manual"],
)
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#[cfg(test)]
mod tests {
use runfiles::Runfiles;
use std::env;
use std::path::PathBuf;

#[test]
fn test_deps_of_crate_and_its_test_are_merged() {
let r = Runfiles::create().unwrap();
let rust_project_path =
r.rlocation("rules_rust/test/rust_analyzer/merging_crates_test/rust-project.json");
let rust_project_path = PathBuf::from(env::var("RUST_PROJECT_JSON").unwrap());

let content = std::fs::read_to_string(&rust_project_path)
.unwrap_or_else(|_| panic!("couldn't open {:?}", &rust_project_path));

assert!(
content.contains(r#""root_module":"test/rust_analyzer/merging_crates_test/mylib.rs","deps":[{"crate":0,"name":"lib_dep"},{"crate":2,"name":"extra_test_dep"}]"#),
content.contains(r#""root_module":"mylib.rs","edition":"2018","deps":[{"crate":0,"name":"extra_test_dep"},{"crate":1,"name":"lib_dep"}]"#),
"expected rust-project.json to contain both lib_dep and extra_test_dep in deps of mylib.rs.");
}
}
41 changes: 0 additions & 41 deletions test/rust_analyzer/merging_crates_test_reversed/BUILD.bazel

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion test/rust_analyzer/merging_crates_test_reversed/lib_dep.rs

This file was deleted.

1 change: 0 additions & 1 deletion test/rust_analyzer/merging_crates_test_reversed/mylib.rs

This file was deleted.

This file was deleted.

94 changes: 94 additions & 0 deletions test/rust_analyzer/rust_analyzer_test_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/bash

# Runs Bazel build commands over rustfmt rules, where some are expected
# to fail.
#
# Can be run from anywhere within the rules_rust workspace.

set -euo pipefail

if [[ -z "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then
echo "This script should be run under Bazel"
exit 1
fi

PACKAGE_NAME="$1"
if [[ -z "${PACKAGE_NAME:-}" ]]; then
echo "The first argument should be the package name of the test target"
exit 1
fi

function generate_workspace() {
local temp_dir="$(mktemp -d -t rules_rust_test_rust_analyzer-XXXXXXXXXX)"
local new_workspace="${temp_dir}/rules_rust_test_rust_analyzer"

mkdir -p "${new_workspace}"
cat << EOF > "${new_workspace}/WORKSPACE.bazel"
workspace(name = "rules_rust_test_rust_analyzer")
local_repository(
name = "rules_rust",
path = "${BUILD_WORKSPACE_DIRECTORY}",
)
load("@rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories(include_rustc_srcs = True)
load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_deps")
rust_analyzer_deps()
EOF

cat << EOF > "${new_workspace}/.bazelrc"
build --keep_going
test --test_output=errors
build:strict --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
build:strict --output_groups=+rustfmt_checks
build:strict --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build:strict --output_groups=+clippy_checks
EOF

echo "${new_workspace}"
}

function rust_analyzer_test() {
local source_dir="$1"
local workspace="$2"

echo "Testing '$(basename "${source_dir}")'"
rm -f "${workspace}"/*.rs "${workspace}"/*.json "${workspace}/BUILD.bazel"
cp -r "${source_dir}"/* "${workspace}"

# Drop the 'manual' tags
if [ "$(uname)" == "Darwin" ]; then
SEDOPTS=(-i '' -e)
else
SEDOPTS=(-i)
fi
sed ${SEDOPTS[@]} 's/"manual"//' "${workspace}/BUILD.bazel"

pushd "${workspace}" &> /dev/null
echo "Generating rust-project.json..."
bazel run "@rules_rust//tools/rust_analyzer:gen_rust_project"
echo "Building..."
bazel build //...
echo "Testing..."
bazel test //...
echo "Building with Aspects..."
bazel build //... --config=strict
popd &> /dev/null
}

function run_test_suite() {
local temp_workspace="$(generate_workspace)"
echo "Generated workspace: ${temp_workspace}"

for test_dir in "${BUILD_WORKSPACE_DIRECTORY}/${PACKAGE_NAME}"/*; do
# Skip everything but directories
if [[ ! -d "${test_dir}" ]]; then
continue
fi

rust_analyzer_test "${test_dir}" "${temp_workspace}"
done

rm -rf "${temp_workspace}"
}

run_test_suite
Empty file added tools/BUILD.bazel
Empty file.
8 changes: 5 additions & 3 deletions tools/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
load("//rust:defs.bzl", "rust_analyzer_detect_sysroot", "rust_binary", "rust_clippy", "rust_library", "rust_test")

rust_analyzer_detect_sysroot(name = "detect_sysroot")
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library", "rust_test")
load("//tools:tool_utils.bzl", "aspect_repository")

rust_binary(
name = "gen_rust_project",
srcs = ["main.rs"],
edition = "2018",
rustc_env = {
"ASPECT_REPOSITORY": aspect_repository(),
},
visibility = ["//visibility:public"],
deps = [
":gen_rust_project_lib",
Expand Down
Loading

0 comments on commit 2f45838

Please sign in to comment.