rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

rust_analyzer ignores repo srcs in crates with generated srcs

Open UebelAndre opened this issue 1 year ago • 2 comments

Any target which has a single generated source will have all sources converted to generated sources so they live next to each other. This works fine for rustc but when using rust-analyzer on your repo, it is confusing as to why entire crates are getting completely ignored. The root cause is the source code in the repo is never referenced in the generated rust-project.json file and thus rust-analyzer does not know to process those files. An easy repro is to run the following in the rules_rust repo:

bazel build //...
bazel run @rules_rust//tools/rust_analyzer:gen_rust_project

From here, observe any of the targets in tests/generated_inputs package.

{
  "display_name": "gen_src_in_different_cfg",
  "root_module": "/private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/execroot/rules_rust/bazel-out/darwin_arm64-fastbuild/bin/test/generated_inputs/root.rs",
  "edition": "2018",
  "deps": [],
  "is_workspace_member": true,
  "cfg": [
    "test",
    "debug_assertions"
  ],
  "target": "aarch64-apple-darwin",
  "env": {
    "CARGO_CFG_TARGET_ARCH": "aarch64",
    "CARGO_CFG_TARGET_OS": "darwin",
    "CARGO_CRATE_NAME": "gen_src_in_different_cfg",
    "CARGO_MANIFEST_DIR": "/private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/execroot/rules_rust/test/generated_inputs",
    "CARGO_PKG_AUTHORS": "",
    "CARGO_PKG_DESCRIPTION": "",
    "CARGO_PKG_HOMEPAGE": "",
    "CARGO_PKG_NAME": "gen_src_in_different_cfg",
    "CARGO_PKG_VERSION": "0.0.0",
    "CARGO_PKG_VERSION_MAJOR": "0",
    "CARGO_PKG_VERSION_MINOR": "0",
    "CARGO_PKG_VERSION_PATCH": "0",
    "CARGO_PKG_VERSION_PRE": "",
    "REPOSITORY_NAME": ""
  },
  "is_proc_macro": false
},

Despite test/generated_inputs/root.rs being a source file, the output path of /private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/execroot/rules_rust/bazel-out/darwin_arm64-fastbuild/bin/test/generated_inputs/root.rs is used which does get processed by rust-analyzer but not the source file developers will be editing.

This issue should be fixed such that sources within the repo get processed by rust-analyzer.

UebelAndre avatar Jun 27 '24 16:06 UebelAndre

Trying to point the root back to the real source file can result in the generated modules not being located, despite using the include_dirs directive:

--- rust-project.old.json	2024-06-27 09:04:25
+++ rust-project.new.json	2024-06-27 09:04:24
@@ -1,9 +1,15 @@
 {
   "display_name": "gen_src_in_different_cfg",
-  "root_module": "/private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/execroot/rules_rust/bazel-out/darwin_arm64-fastbuild/bin/test/generated_inputs/root.rs",
+  "root_module": "test/generated_inputs/root.rs",
   "edition": "2018",
   "deps": [],
   "is_workspace_member": true,
+  "source": {
+    "include_dirs": [
+      "/private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/execroot/rules_rust/bazel-out/darwin_arm64-fastbuild/bin/test/generated_inputs"
+    ],
+    "exclude_dirs": []
+  },
   "cfg": [
     "test",
     "debug_assertions"

UebelAndre avatar Jun 27 '24 16:06 UebelAndre

Oh, this might be due to the module being generated_in_different_cfg which would probably not be in that directory!

UebelAndre avatar Jun 27 '24 16:06 UebelAndre