rust-lang / rust-lang/rust-analyzer
Don't pass `--all-targets` when a target is specified
@alibektas is already working on this.
Since Sep 13, 2024.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
We apply a target in this line:
But then, apply_on_command is called in this line:
apply_on_command adds --all-targets if the config value all_targets is true (which is the default).
Sadly, Cargo currently ignores any target specification like --bin BIN_NAME if --all-targets is specified. Which means that PR https://github.com/rust-lang/rust-analyzer/pull/17912 is ineffective right now.
So what is the solution? Don't pass --all-targets when a specific target is passed? This currently can't be the solution.
The first problem is that when not using --all-targets and without --profile test, Cargo doesn't check tests with #[cfg(test)]. So we should (always?) add --profile test to cargo check?
The second problem is that Rust-Analyzer seems to forget all errors and warnings after an invocation of cargo check. See the video from VS-Code in a Windows VM with the pre-release Rust-Analyzer extension:
Screencast from 2024-09-13 15-47-18.webm
In this video, I disabled all_targets in the extension settings. When Rust-Analyzer starts, it runs cargo check and collects 113 errors and 8 warnings. Then, while still starting up, it forgets all these errors and warnings and runs cargo check again until it reaches the same 113 errors and 8 warnings that it already had!?
But this isn't the main issue here. The issue is that, after saving one file associated with a binary, Rust-Analyzer runs cargo check --bin if1 quickly as expected from https://github.com/rust-lang/rust-analyzer/pull/17912 but it forgets all diagnostics it had before about the other targets!
TL;DR
- We need to omit
--all-targetswhen specifying a specific target like--bin. - We need to pass
--target testto the check command. - Rust-Analyzer shouldn't forget all diagnostics from other targets.
- (Why does Rust-Analyzer run
cargo checktwice on startup?)
CC @alibektas @Veykril from the other PR.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.