diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-08 11:11:19 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-08 11:11:19 +0100 |
commit | 8295a9340c1fbda805497035054ead0b10c0d88e (patch) | |
tree | ccab3f149b9633ae95570d78fa5d6b8d3b3392e6 /crates/ra_project_model/src/cargo_workspace.rs | |
parent | 363c1f2f493d206f2cc10c348a02f3efadd8c77a (diff) | |
parent | 3077eae2a61f97c28c0d4e3456f6ab873126e5b8 (diff) |
Merge #4329
4329: Look for `cargo`, `rustc`, and `rustup` in standard installation path r=matklad a=cdisselkoen
Discussed in #3118. This is approximately a 90% fix for the issue described there.
This PR creates a new crate `ra_env` with a function `get_path_for_executable()`; see docs there. `get_path_for_executable()` improves and generalizes the function `cargo_binary()` which was previously duplicated in the `ra_project_model` and `ra_flycheck` crates. (Both of those crates now depend on the new `ra_env` crate.) The new function checks (e.g.) `$CARGO` and `$PATH`, but also falls back on `~/.cargo/bin` manually before erroring out. This should allow most users to not have to worry about setting the `$CARGO` or `$PATH` variables for VSCode, which can be difficult e.g. on macOS as discussed in #3118.
I've attempted to replace all calls to `cargo`, `rustc`, and `rustup` in rust-analyzer with appropriate invocations of `get_path_for_executable()`; I don't think I've missed any in Rust code, but there is at least one invocation in TypeScript code which I haven't fixed. (I'm not sure whether it's affected by the same problem or not.) https://github.com/rust-analyzer/rust-analyzer/blob/a4778ddb7a00f552a8e653bbf56ae9fd69cfe1d3/editors/code/src/cargo.ts#L79
I'm sure this PR could be improved a bunch, so I'm happy to take feedback/suggestions on how to solve this problem better, or just bikeshedding variable/function/crate names etc.
cc @Veetaha
Fixes #3118.
Co-authored-by: Craig Disselkoen <[email protected]>
Co-authored-by: veetaha <[email protected]>
Diffstat (limited to 'crates/ra_project_model/src/cargo_workspace.rs')
-rw-r--r-- | crates/ra_project_model/src/cargo_workspace.rs | 14 |
1 files changed, 3 insertions, 11 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 16fff9f1c..eb9f33ee8 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::{ | 3 | use std::{ |
4 | env, | ||
5 | ffi::OsStr, | 4 | ffi::OsStr, |
6 | ops, | 5 | ops, |
7 | path::{Path, PathBuf}, | 6 | path::{Path, PathBuf}, |
@@ -12,6 +11,7 @@ use anyhow::{Context, Result}; | |||
12 | use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; | 11 | use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; |
13 | use ra_arena::{Arena, Idx}; | 12 | use ra_arena::{Arena, Idx}; |
14 | use ra_db::Edition; | 13 | use ra_db::Edition; |
14 | use ra_env::get_path_for_executable; | ||
15 | use rustc_hash::FxHashMap; | 15 | use rustc_hash::FxHashMap; |
16 | 16 | ||
17 | /// `CargoWorkspace` represents the logical structure of, well, a Cargo | 17 | /// `CargoWorkspace` represents the logical structure of, well, a Cargo |
@@ -146,12 +146,8 @@ impl CargoWorkspace { | |||
146 | cargo_toml: &Path, | 146 | cargo_toml: &Path, |
147 | cargo_features: &CargoConfig, | 147 | cargo_features: &CargoConfig, |
148 | ) -> Result<CargoWorkspace> { | 148 | ) -> Result<CargoWorkspace> { |
149 | let _ = Command::new(cargo_binary()) | ||
150 | .arg("--version") | ||
151 | .output() | ||
152 | .context("failed to run `cargo --version`, is `cargo` in PATH?")?; | ||
153 | |||
154 | let mut meta = MetadataCommand::new(); | 149 | let mut meta = MetadataCommand::new(); |
150 | meta.cargo_path(get_path_for_executable("cargo")?); | ||
155 | meta.manifest_path(cargo_toml); | 151 | meta.manifest_path(cargo_toml); |
156 | if cargo_features.all_features { | 152 | if cargo_features.all_features { |
157 | meta.features(CargoOpt::AllFeatures); | 153 | meta.features(CargoOpt::AllFeatures); |
@@ -293,7 +289,7 @@ pub fn load_extern_resources( | |||
293 | cargo_toml: &Path, | 289 | cargo_toml: &Path, |
294 | cargo_features: &CargoConfig, | 290 | cargo_features: &CargoConfig, |
295 | ) -> Result<ExternResources> { | 291 | ) -> Result<ExternResources> { |
296 | let mut cmd = Command::new(cargo_binary()); | 292 | let mut cmd = Command::new(get_path_for_executable("cargo")?); |
297 | cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); | 293 | cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); |
298 | if cargo_features.all_features { | 294 | if cargo_features.all_features { |
299 | cmd.arg("--all-features"); | 295 | cmd.arg("--all-features"); |
@@ -347,7 +343,3 @@ fn is_dylib(path: &Path) -> bool { | |||
347 | Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), | 343 | Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), |
348 | } | 344 | } |
349 | } | 345 | } |
350 | |||
351 | fn cargo_binary() -> String { | ||
352 | env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()) | ||
353 | } | ||