Skip to content
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

breaking change when handling multiple cargo targets #18869

Closed
qjerome opened this issue Jan 7, 2025 · 13 comments
Closed

breaking change when handling multiple cargo targets #18869

qjerome opened this issue Jan 7, 2025 · 13 comments
Labels
C-bug Category: bug

Comments

@qjerome
Copy link
Contributor

qjerome commented Jan 7, 2025

Description

I could track down there is breaking change concerning multiple cargo targets since release 0.3.2237

Issue: prior 0.3.2237 it was possible to set multiple cargo targets in .config/cargo.toml and rust-analyzer was working fine with it. Since the mentioned release it no longer works, at least on VSCode. One can confirm that by trying to analyze the code available in the repository referenced below with a RA version >=0.3.2237. The expected behavior is to have all the cargo targets specified in .config/cargo.toml being analyzed correctly (code completion, hints, jumps ...).

Temporary fix: revert back to rust-analyzer 0.3.2228

Additional Information

rust-analyzer version: since 0.3.2237

rustc version: rustc 1.83.0 (90b35a623 2024-11-26)

editor or extension: VSCode with official rust-analyzer extension

code snippet to reproduce: https://github.com/qjerome/rust-analyzer-repro-bug-1

@qjerome qjerome added the C-bug Category: bug label Jan 7, 2025
@Veykril
Copy link
Member

Veykril commented Jan 7, 2025

The expected behavior is to have all the cargo targets specified in .config/cargo.toml being analyzed correctly (code completion, hints, jumps ...).

This never worked, rust-analyzer is inherently incapable of handling multiple targets. The only thing that works (unless that is what broke here) is allowing cargo check to run on multiple targets.

@qjerome
Copy link
Contributor Author

qjerome commented Jan 7, 2025

I can confirm that code completion, hints and jumps used to work, this is something I am using every day since 2 years or so. I think this got implemented in this PR: #13290

It is straightforward to verify that it used to work using the provided POC repository: https://github.com/qjerome/rust-analyzer-repro-bug-1

@Veykril
Copy link
Member

Veykril commented Jan 7, 2025

#13290 only implemented cargo check to be supported for multiple targets, as the PR title states.

@Veykril
Copy link
Member

Veykril commented Jan 7, 2025

Waht exactly is the behavior you are observing in that example repo now. That only one of the cfgs is being semantically analyzed? (which I can only repeat, has always been the case)

@qjerome
Copy link
Contributor Author

qjerome commented Jan 7, 2025

Ok, maybe I didn't use the proper terminology but something has changed since 0.3.2237 and the following no longer works. After the mentioned release, only one target gets analyzed.

Image

NB: the screenshot is a working scenario where both the targets get analyzed (using 0.3.2228)

@Veykril
Copy link
Member

Veykril commented Jan 7, 2025

Huh, well that looks like a bug to me. This implies that target_arch is set to two differing values at once which can't be right. So whatever changed (a bunch of things have changed on the cfg fetching code recently) fixed a bug here that prevents that scenario. Either way I am sorry to say but while you are right that we are analyzing both cfg blocks there, we shouldn't have done so due to the nature of how cfg-ed code works.

rust-analyzer is not able to analyze for multiple targets at once, this is a huge architecture change that have no capacity to implement

@Veykril Veykril closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
@qjerome
Copy link
Contributor Author

qjerome commented Jan 7, 2025

Ok, so you mean its been 2 years I am relying on a bug on RA to serve well my multiple target use case and that won't be fixed ?

I actually have a Rust workspace where I have to develop two targets ["x86_64-unknown-linux-gnu", "bpfel-unknown-none"] (eBPF + another linux target) at the same time (with code in common) so I really really need to be able to analyze and work on the two targets at the same time.

What would you recommend to be able to do that with rust-analyzer ?

@Veykril
Copy link
Member

Veykril commented Jan 7, 2025

Unfortunately the only option until we improve the situtation is to open r-a twice, once for each target (and configure each instance via the editor configs).

@qjerome
Copy link
Contributor Author

qjerome commented Jan 7, 2025

Well that is not ideal ... I think it will be cumbersome (at least for the lazy ass I am :)) to always jump between two instances of the IDE !
Are you open to contributions to fix this ?

@ChayimFriedman2
Copy link
Contributor

Try including your extra targets in rust-analyzer.cargo.cfgs.

@qjerome
Copy link
Contributor Author

qjerome commented Jan 8, 2025

Thanks @ChayimFriedman2 it works with this to add only one extra target, I didn't find yet how to do to add several targets.

    "rust-analyzer.cargo.cfgs": {
        "target_arch": "x86_64",
    }

@qjerome
Copy link
Contributor Author

qjerome commented Jan 8, 2025

Maybe a quick win for a partial / light multi-target support would be either to have rust-analyzer.cargo.cfgs being able to handle repeated same key or leverage the cfg derive parser implemented in the cfg crate. TBH I was very happy with that bug driven unintended multi-target support and maybe other people would be happy too, even if the solution isn't perfect from RA devs side.

@qjerome
Copy link
Contributor Author

qjerome commented Jan 9, 2025

For posterity here is a PR allowing one to specify several times the same rust-analyzer.cargo.cfgs including target_arch. This will not make rust-analyzer truly support multiple targets but it will merely enable CFG analysis for the multiple targets and help having partial support for features like code completion, cross-ref jumps and other useful features when one wants to develop several targets in one single IDE instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants