rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

gen_rust_project: support unit tests

Open TheLortex opened this issue 4 months ago • 0 comments

Currently, rules_rust's rust-project.json generation code does not provide enough information for rust-analyzer to know how to launch a unit test. It looks like this and tries to run cargo when clicking on Debug: image

When it works, it should look like this and properly run the selected test: image

Some investigation made me understand that the additional content is needed:

  • in the root object, provide a runnables list:
{
  "sysroot": "...",
  "sysroot_src": "...",
  "runnables": [
    {
      "program": "bazel",
      "args": [
        "test",
        "--test_output=streamed",
        "{label}",
        "--test_arg={test_id}",
        "--test_arg=--nocapture",
        "--test_arg=--color=always",
        "--test_arg=--exact"
      ],
      "cwd": "/Users/lucas/projets/issue-rust-analyzer-tests-bazel/",
      "kind": "testOne"
    }
  ],
  ...
  • for each crate, provide a build record:
   {  
      "display_name": "engine",
      "build": {
        "label": "//rs/engine:tests",
        "build_file": "/Users/lucas/projets/issue-rust-analyzer-tests-bazel/rs/engine/BUILD.bazel",
        "target_kind": "test"
      },
      ...

Is it already planned to add support for tests in cargoless setups ? Are there unexpected roadblocks ?

Here is the demo repository I have used to experiment with rust-project.json configuration. It's not usable as is, you need to run ./gen_rust_project.sh to obtain a version that works on your machine, then manually edit it to add the fields I describe above in the issue. https://github.com/TheLortex/issue-rust-analyzer-tests-bazel

TheLortex avatar Oct 01 '24 09:10 TheLortex