rust/crates
bors[bot] a485c9b21d
Merge #7071
7071: Pass --all-targets to "cargo check" when discovering external resources r=matklad a=WasabiFan

There is a repro case and background in the linked issue.

In short, the goal of this MR is to allow rust-analyzer to discover proc-macros which come from your tests (including, most importantly, dev-dependencies).

By default, `cargo check` implies the equivalent of `--lib --bins`, meaning it'll check your libraries and binaries -- but not tests! This means proc-macros (or, I guess, build scripts as well) weren't discovered by rust-analyzer if they came from tests.

One solution would be to manually add `--lib --bins --tests` (i.e., just augment the effective options to include tests). However, in this MR, I threw in `--all-targets`, which [according to the docs](https://doc.rust-lang.org/cargo/commands/cargo-check.html#target-selection) implies `--benches --examples` too. I have absolutely no idea what repercussions that will have on rust-analyzer for other projects, nor do I know if it's a problem that build scripts will now be discovered for tests/examples/benches. But I am not aware of a reason you _wouldn't_ want to discover these things in your examples too.

I think the main drawback of this change is that it will likely slow down the `cargo check`. At a minimum, it'll now be checking your tests _and_ their dependencies. The `cargo check` docs also say that including `--tests` as I have here may cause your lib crate to be built _twice_, once for the normal target and again for unit tests. My reading of that caveat suggests that "building twice" means it's built once for the tests _inside_ your lib, with a test profile, and again for any consumers of your lib, now using a normal release profile or similar. This doesn't seem surprising.

Very minor caveat: `--tests`  will not include tests within a binary if it has `test = false` set in `Cargo.toml`. (I discovered this manually by trial-and-error, but hey, it actually says that in the docs!) This is likely not an issue, but _does_ mean that if you are -- for whatever reason -- disabling tests like that and then manually specifying `cargo test --package <...> --bin <...>` to run them, rust-analyzer will remain unaware of proc-macros in your tests.

I have confirmed this fixes the original issue in my sandbox example linked in #7034 and in my own project in which I originally discovered this. I've left it configured as my default RA language server and will report back if I notice any unexpected side-effects.

Fixes #7034


Co-authored-by: Kaelin Laundry <wasabifan@outlook.com>
2020-12-31 15:29:26 +00:00
..
arena Add TBD description to arena 2020-08-24 13:29:10 +02:00
assists Update crates 2020-12-30 10:39:50 -05:00
base_db Pass crate environment to proc macros 2020-12-27 15:29:47 +01:00
cfg Remove some redundant allocations 2020-12-12 12:27:09 -05:00
completion Remove some unneeded string allocations 2020-12-31 10:13:14 +01:00
flycheck Pin cargo_metadata 2020-11-25 10:11:53 -05:00
hir Update crates 2020-12-30 10:39:50 -05:00
hir_def Remove some unneeded string allocations 2020-12-31 10:13:14 +01:00
hir_expand Fix == in in format causes mismatched-arg-count 2020-12-31 17:59:29 +08:00
hir_ty Update crates 2020-12-30 10:39:50 -05:00
ide Update crates 2020-12-30 10:39:50 -05:00
ide_db Update crates 2020-12-30 10:39:50 -05:00
mbe Remove unused clones in mbe 2020-12-31 02:58:41 +08:00
parser Fix mbe fail to pass expr with attr 2020-12-28 19:53:00 +08:00
paths Add description for crates that will be published 2020-08-24 13:07:22 +02:00
proc_macro_api Pass crate environment to proc macros 2020-12-27 15:29:47 +01:00
proc_macro_srv Fix spacing in proc-macro tokens to_string 2020-12-31 13:36:19 +08:00
proc_macro_test Add description for crates that will be published 2020-08-24 13:07:22 +02:00
profile Deny unreachable-pub 2020-11-02 14:07:08 +01:00
project_model Merge #7071 2020-12-31 15:29:26 +00:00
rust-analyzer Merge #7106 2020-12-31 15:11:14 +00:00
ssr Remove some unneeded string allocations 2020-12-31 10:13:14 +01:00
stdx add open Cargo.toml action 2020-11-12 17:48:07 -08:00
syntax Update crates 2020-12-30 10:39:50 -05:00
test_utils Make config.rs a single source of truth for configuration. 2020-12-08 19:25:41 +03:00
text_edit Avoid turning completion objects into builders 2020-11-16 23:16:41 +02:00
toolchain Add description for crates that will be published 2020-08-24 13:07:22 +02:00
tt Move TokenExpander to base_db and rename it 2020-12-27 15:29:47 +01:00
vfs Merge #6785 2020-12-09 16:32:03 +00:00
vfs-notify Follow symlinks when walking project trees 2020-10-15 14:22:36 -04:00