rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

Infinite cycle in rust-analyzer caused by test deps

Open UebelAndre opened this issue 1 year ago • 2 comments

I added the following BUILD file to //tools/rust_analyzer/demo:BUILD.bazel to reproduce a failure in @rules_rust//tools/rust_analyzer:gen_rust_project where a test dependency is causing an infinite cycle when generating rust-project.json

load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

rust_library(
    name = "lib_c",
    srcs = ["lib_c.rs"],
    deps = [":lib_b"],
    edition = "2021",
)

rust_library(
    name = "lib_b",
    srcs = ["lib_b.rs"],
    deps = [":lib_a"],
    edition = "2021",
)

rust_library(
    name = "lib_a",
    srcs = ["lib_a.rs"],
    edition = "2021",
)

rust_test(
    name = "lib_a_test",
    crate = ":lib_a",
    deps = [":lib_c"],
)
~/Code/rules_rust (rust-analyzer ✗) RUST_LOG=warn bazel run //tools/rust_analyzer:gen_rust_project
INFO: Analyzed target //tools/rust_analyzer:gen_rust_project (1 packages loaded, 6 targets configured).
INFO: Found 1 target...
Target //tools/rust_analyzer:gen_rust_project up-to-date:
  bazel-bin/tools/rust_analyzer/gen_rust_project
INFO: Elapsed time: 0.290s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/tools/rust_analyzer/gen_rust_project
[2024-02-17T18:15:10Z WARN  gen_rust_project_lib::rust_project] Cycle detected: ["ID-tools/rust_analyzer/demo/lib_a.rs", "ID-tools/rust_analyzer/demo/lib_c.rs", "ID-tools/rust_analyzer/demo/lib_b.rs", "ID-tools/rust_analyzer/demo/lib_a.rs"]
[2024-02-17T18:15:10Z WARN  gen_rust_project_lib::rust_project] Cycle detected: ["ID-tools/rust_analyzer/demo/lib_b.rs", "ID-tools/rust_analyzer/demo/lib_a.rs", "ID-tools/rust_analyzer/demo/lib_c.rs", "ID-tools/rust_analyzer/demo/lib_b.rs"]
[2024-02-17T18:15:10Z WARN  gen_rust_project_lib::rust_project] Cycle detected: ["ID-tools/rust_analyzer/demo/lib_c.rs", "ID-tools/rust_analyzer/demo/lib_b.rs", "ID-tools/rust_analyzer/demo/lib_a.rs", "ID-tools/rust_analyzer/demo/lib_c.rs"]
Error: Failed to make progress on building crate dependency graph

However, Bazel does not consider this a loop since the test target and it's dependencies are all different targets. The issue in rust-analyzer is caused by the following being collapsed into a shared crate spec (https://github.com/bazelbuild/rules_rust/blob/0.39.0/tools/rust_analyzer/aquery.rs#L173-L209)

rust_library(
    name = "lib_a",
    srcs = ["lib_a.rs"],
    edition = "2021",
)

rust_test(
    name = "lib_a_test",
    crate = ":lib_a",
    deps = [":lib_c"],
)

I'm not sure what the right solution is here or even if rust-analyzer should be expected to handle this situation but my initial thoughts are that this is a bug and some solution should be provided.

UebelAndre avatar Feb 17 '24 18:02 UebelAndre

I opened a discussion to see if anyone has more context on this issue

  • https://github.com/rust-lang/rust-analyzer/discussions/17046

UebelAndre avatar Apr 10 '24 14:04 UebelAndre