Repository: rust-analyzer/rust-analyzer
Pull request #5563 (2020-07-28 23:26:45)

Check all targets for package-level tasks
When invoking "Select Runnable" with the caret on a runnable with a specific target (test, bench, binary), append the corresponding argument for the `cargo check -p` module runnable.

matklad: We check the whole package by design. There's usually little benefit in checking only some things (as check is fast), and it's convenient if everything is checked. Does blanket `cargo check -p` checks example&benches though? I think it should.  
SomeoneToIgnore: No, that's why I've created this PR.

Simple to check: make a compiler error in the tests, run the "-p" check command, there will be no errors.
SomeoneToIgnore: Oh, sorry, not sure about examples & benches for sure, but tests errors are not checked by the current command.

Currently, the only way to verify that the tests compile is to actually run them, there's no check runnable RA provides for them.
matklad: Ah, so it seems like we should pass `--all-targets` then! That was the
original design, didn't realise that it doesn't work :D

On Wed, 29 Jul 2020 at 10:45, Kirill Bulatov <notifications@github.com>
wrote:

> Oh, sorry, not sure about examples & benches for sure, but tests errors
> are not checked by the current command.
>
> Currently, the only way to verify that the tests compile is to actually
> run them, there's no check runnable RA provides for them.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/rust-analyzer/rust-analyzer/pull/5563#issuecomment-665527065>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AANB3MYOG7VSARSZZAC7MX3R57ORJANCNFSM4PLAP75A>
> .
>


Patch:
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 1350bd4002..87a1367ac2 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -469,12 +469,12 @@ pub(crate) fn handle_runnables(
         res.push(runnable);
     }
 
-    // Add `cargo check` and `cargo test` for the whole package
+    // Add `cargo check` and `cargo test` for all targets of the whole package
     match cargo_spec {
         Some(spec) => {
             for &cmd in ["check", "test"].iter() {
                 res.push(lsp_ext::Runnable {
-                    label: format!("cargo {} -p {}", cmd, spec.package),
+                    label: format!("cargo {} -p {} --all-targets", cmd, spec.package),
                     location: None,
                     kind: lsp_ext::RunnableKind::Cargo,
                     args: lsp_ext::CargoRunnable {
@@ -483,6 +483,7 @@ pub(crate) fn handle_runnables(
                             cmd.to_string(),
                             "--package".to_string(),
                             spec.package.clone(),
+                            "--all-targets".to_string(),
                         ],
                         executable_args: Vec::new(),
                         expect_test: None,


Test patch:
diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs
index 28e8966487..7370505f8b 100644
--- a/crates/rust-analyzer/tests/heavy_tests/main.rs
+++ b/crates/rust-analyzer/tests/heavy_tests/main.rs
@@ -115,21 +115,21 @@ fn main() {}
           },
           {
             "args": {
-              "cargoArgs": ["check", "--package", "foo"],
+              "cargoArgs": ["check", "--package", "foo", "--all-targets"],
               "executableArgs": [],
               "workspaceRoot": server.path().join("foo")
             },
             "kind": "cargo",
-            "label": "cargo check -p foo"
+            "label": "cargo check -p foo --all-targets"
           },
           {
             "args": {
-              "cargoArgs": ["test", "--package", "foo"],
+              "cargoArgs": ["test", "--package", "foo", "--all-targets"],
               "executableArgs": [],
               "workspaceRoot": server.path().join("foo")
             },
             "kind": "cargo",
-            "label": "cargo test -p foo"
+            "label": "cargo test -p foo --all-targets"
           }
         ]),
     );