Skip to content

gen_rust_project creates invalid entries if compile_data is used #3200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hauserx opened this issue Jan 22, 2025 · 4 comments
Closed

gen_rust_project creates invalid entries if compile_data is used #3200

hauserx opened this issue Jan 22, 2025 · 4 comments

Comments

@hauserx
Copy link
Contributor

hauserx commented Jan 22, 2025

Generated rust-project.json started to contain include_dirs which points to bin directory. Because of that rust_analyzer does not treat any file, other than root_module, as belonging to the crate.

Workaround is to manually remove the source entry, or add appropriate include dir to rust-project.json ("src" in below example).

Bisect pointed to #3176, although reading through it possibly issue was present even before that if crate contained some generated data.

Example on https://github.com/cameron-martin/bazel-lsp after bazel run @rules_rust//tools/rust_analyzer:gen_rust_project

  "crates": [
...
    {
      "display_name": "bazel_lsp",
      "root_module": "/home/hauser/repos/bazel-lsp/src/main.rs",
...
      "source": {
        "include_dirs": [
          "/home/hauser/.cache/bazel/_bazel_hauser/7b35979d7249c8686e26265de57e7009/execroot/_main/bazel-out/k8-fastbuild/bin/src"
        ],
        "exclude_dirs": []
      },
...
@UebelAndre
Copy link
Collaborator

Bisect pointed to #3176, although reading through it possibly issue was present even before that if crate contained some generated data.

I'm confused, did this introduce an additional regression or not? If so I think we should revert it.

@hauserx
Copy link
Contributor Author

hauserx commented Jan 22, 2025

Yes, it stopped working for bazel-lsp that is using compile_data.

But I am not sure it worked even before if some generated data was used - or how it worked as in such case the sources also were transformed. I have not checked it for generated data though.

@hauserx
Copy link
Contributor Author

hauserx commented Jan 22, 2025

Just confirmed, that generated data would trigger this issue even before the #3176 .

Here is small patch on current bazel-lsp that triggers the same issue on rules_rust 0.56:

diff --git a/BUILD b/BUILD
index a52b298..b57465d 100644
--- a/BUILD
+++ b/BUILD
@@ -2,7 +2,7 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test")

 rust_binary(
     name = "bazel-lsp",
-    srcs = glob(["src/**/*.rs"]),
+    srcs = glob(["src/**/*.rs"]) + ["src/empty.rs"],
     compile_data = [
         "//src/builtin:builtin.pb",
         "//src/builtin:default_build_language.pb",
@@ -31,6 +31,12 @@ rust_binary(
     ],
 )

+genrule(
+    name = "gen_empty_file",
+    cmd = "echo  > $@",
+    outs = [ "src/empty.rs" ],
+)
+
 genrule(
     name = "generate_rustc_env_file",
     srcs = [
diff --git a/MODULE.bazel b/MODULE.bazel
index 19049d3..f46ba5e 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,5 +1,5 @@
-bazel_dep(name = "rules_rust", version = "0.57.0")
-bazel_dep(name = "rules_rust_prost", version = "0.57.0")
+bazel_dep(name = "rules_rust", version = "0.56.0")
+bazel_dep(name = "rules_rust_prost", version = "0.56.0")

 rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
 rust.toolchain(

@martingms
Copy link
Contributor

martingms commented Jan 23, 2025

There are a few general issues with generated sources/compile_data (especially when mixed with non-generated) in relation to rust-analyzer that is a bit unclear how to address, see #3126 for example. Afaict #3176 made builds work in more cases, but also exposed more cases where gen_rust_project generates wrong or useless rust-project.json-entries.. :/

@hauserx hauserx closed this as completed Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants