Skip to content

Commit 5aa1bba

Browse files
committed
more generic, find rustc as well
1 parent ffaef1b commit 5aa1bba

File tree

4 files changed

+71
-54
lines changed

4 files changed

+71
-54
lines changed

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! FIXME: write short doc here
22
33
use std::{
4-
env,
54
ffi::OsStr,
65
ops,
76
path::{Path, PathBuf},
87
process::Command,
98
};
109

11-
use anyhow::{Context, Error, Result};
10+
use super::find_executables::get_path_for_executable;
11+
use anyhow::{Context, Result};
1212
use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
1313
use ra_arena::{Arena, Idx};
1414
use ra_db::Edition;
@@ -146,7 +146,7 @@ impl CargoWorkspace {
146146
cargo_features: &CargoConfig,
147147
) -> Result<CargoWorkspace> {
148148
let mut meta = MetadataCommand::new();
149-
meta.cargo_path(cargo_binary()?);
149+
meta.cargo_path(get_path_for_executable("cargo")?);
150150
meta.manifest_path(cargo_toml);
151151
if cargo_features.all_features {
152152
meta.features(CargoOpt::AllFeatures);
@@ -284,7 +284,7 @@ pub fn load_extern_resources(
284284
cargo_toml: &Path,
285285
cargo_features: &CargoConfig,
286286
) -> Result<ExternResources> {
287-
let mut cmd = Command::new(cargo_binary()?);
287+
let mut cmd = Command::new(get_path_for_executable("cargo")?);
288288
cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
289289
if cargo_features.all_features {
290290
cmd.arg("--all-features");
@@ -332,52 +332,3 @@ fn is_dylib(path: &Path) -> bool {
332332
Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
333333
}
334334
}
335-
336-
/// Return a `String` to use for executable `cargo`.
337-
///
338-
/// E.g., this may just be `cargo` if that gives a valid Cargo executable; or it
339-
/// may be a full path to a valid Cargo.
340-
fn cargo_binary() -> Result<String> {
341-
// The current implementation checks three places for a `cargo` to use:
342-
// 1) $CARGO environment variable (erroring if this is set but not a usable Cargo)
343-
// 2) `cargo`
344-
// 3) `~/.cargo/bin/cargo`
345-
if let Ok(path) = env::var("CARGO") {
346-
if is_valid_cargo(&path) {
347-
Ok(path)
348-
} else {
349-
Err(Error::msg("`CARGO` environment variable points to something that's not a valid Cargo executable"))
350-
}
351-
} else {
352-
let final_path: Option<String> = if is_valid_cargo("cargo") {
353-
Some("cargo".to_owned())
354-
} else {
355-
if let Some(mut path) = dirs::home_dir() {
356-
path.push(".cargo");
357-
path.push("bin");
358-
path.push("cargo");
359-
if is_valid_cargo(&path) {
360-
Some(path.into_os_string().into_string().expect("Invalid Unicode in path"))
361-
} else {
362-
None
363-
}
364-
} else {
365-
None
366-
}
367-
};
368-
final_path.ok_or(
369-
// This error message may also be caused by $PATH or $CARGO not being set correctly for VSCode,
370-
// even if they are set correctly in a terminal.
371-
// On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
372-
// to inherit environment variables including $PATH and $CARGO from that terminal; but
373-
// launching VSCode from Dock does not inherit environment variables from a terminal.
374-
// For more discussion, see #3118.
375-
Error::msg("Failed to find `cargo` executable. Make sure `cargo` is in `$PATH`, or set `$CARGO` to point to a valid Cargo executable.")
376-
)
377-
}
378-
}
379-
380-
/// Does the given `Path` point to a usable `Cargo`?
381-
fn is_valid_cargo(p: impl AsRef<Path>) -> bool {
382-
Command::new(p.as_ref()).arg("--version").output().is_ok()
383-
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use anyhow::{Error, Result};
2+
use std::env;
3+
use std::path::Path;
4+
use std::process::Command;
5+
6+
/// Return a `String` to use for the given executable.
7+
///
8+
/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
9+
/// gives a valid Cargo executable; or it may return a full path to a valid
10+
/// Cargo.
11+
pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<String> {
12+
// The current implementation checks three places for an executable to use:
13+
// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
14+
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
15+
// 2) `<executable_name>`
16+
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` in on the $PATH
17+
// 3) `~/.cargo/bin/<executable_name>`
18+
// example: for cargo, this tries ~/.cargo/bin/cargo
19+
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
20+
let executable_name = executable_name.as_ref();
21+
let env_var = executable_name.to_ascii_uppercase();
22+
if let Ok(path) = env::var(&env_var) {
23+
if is_valid_executable(&path) {
24+
Ok(path)
25+
} else {
26+
Err(Error::msg(format!("`{}` environment variable points to something that's not a valid executable", env_var)))
27+
}
28+
} else {
29+
let final_path: Option<String> = if is_valid_executable(executable_name) {
30+
Some(executable_name.to_owned())
31+
} else {
32+
if let Some(mut path) = dirs::home_dir() {
33+
path.push(".cargo");
34+
path.push("bin");
35+
path.push(executable_name);
36+
if is_valid_executable(&path) {
37+
Some(path.into_os_string().into_string().expect("Invalid Unicode in path"))
38+
} else {
39+
None
40+
}
41+
} else {
42+
None
43+
}
44+
};
45+
final_path.ok_or(
46+
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
47+
// for VSCode, even if they are set correctly in a terminal.
48+
// On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
49+
// to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
50+
// but launching VSCode from Dock does not inherit environment variables from a terminal.
51+
// For more discussion, see #3118.
52+
Error::msg(format!("Failed to find `{}` executable. Make sure `{}` is in `$PATH`, or set `${}` to point to a valid executable.", executable_name, executable_name, env_var))
53+
)
54+
}
55+
}
56+
57+
/// Does the given `Path` point to a usable executable?
58+
///
59+
/// (assumes the executable takes a `--version` switch and writes to stdout,
60+
/// which is true for `cargo`, `rustc`, and `rustup`)
61+
fn is_valid_executable(p: impl AsRef<Path>) -> bool {
62+
Command::new(p.as_ref()).arg("--version").output().is_ok()
63+
}

crates/ra_project_model/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! FIXME: write short doc here
22
33
mod cargo_workspace;
4+
mod find_executables;
45
mod json_project;
56
mod sysroot;
67

crates/ra_project_model/src/sysroot.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! FIXME: write short doc here
22
3+
use super::find_executables::get_path_for_executable;
34
use anyhow::{bail, Context, Result};
45
use std::{
56
env, ops,
@@ -114,7 +115,8 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
114115
if let Ok(path) = env::var("RUST_SRC_PATH") {
115116
return Ok(path.into());
116117
}
117-
let rustc_output = run_command_in_cargo_dir(cargo_toml, "rustc", &["--print", "sysroot"])?;
118+
let rustc = get_path_for_executable("rustc")?;
119+
let rustc_output = run_command_in_cargo_dir(cargo_toml, &rustc, &["--print", "sysroot"])?;
118120
let stdout = String::from_utf8(rustc_output.stdout)?;
119121
let sysroot_path = Path::new(stdout.trim());
120122
let src_path = sysroot_path.join("lib/rustlib/src/rust/src");

0 commit comments

Comments
 (0)