rust-lang / rust-lang/rust-analyzer

Don't pass `--all-targets` when a target is specified

Open
#18,110 0 comments 2 reactions 1 assignee View on GitHub

@alibektas is already working on this.

Since Sep 13, 2024.

C-bug
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:

https://github.com/rust-lang/rust-analyzer/blob/4221354a8fe90ea8218d3757d14735eac08d3e81/crates/rust-analyzer/src/flycheck.rs#L411

But then, apply_on_command is called in this line:

https://github.com/rust-lang/rust-analyzer/blob/4221354a8fe90ea8218d3757d14735eac08d3e81/crates/rust-analyzer/src/flycheck.rs#L435

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-targets when specifying a specific target like --bin.
  • We need to pass --target test to the check command.
  • Rust-Analyzer shouldn't forget all diagnostics from other targets.
  • (Why does Rust-Analyzer run cargo check twice on startup?)

CC @alibektas @Veykril from the other PR.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.