mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Respect $CARGO_HOME when looking up toolchains.
This commit is contained in:
parent
289208bc9f
commit
e4b184a776
2 changed files with 35 additions and 16 deletions
|
@ -31,8 +31,9 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
|
|||
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
|
||||
// 2) `<executable_name>`
|
||||
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
|
||||
// 3) `~/.cargo/bin/<executable_name>`
|
||||
// example: for cargo, this tries ~/.cargo/bin/cargo
|
||||
// 3) `$CARGO_HOME/bin/<executable_name>`
|
||||
// where $CARGO_HOME defaults to ~/.cargo (see https://doc.rust-lang.org/cargo/guide/cargo-home.html)
|
||||
// example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
|
||||
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
|
||||
let env_var = executable_name.to_ascii_uppercase();
|
||||
if let Some(path) = env::var_os(env_var) {
|
||||
|
@ -43,8 +44,7 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
|
|||
return executable_name.into();
|
||||
}
|
||||
|
||||
if let Some(mut path) = home::home_dir() {
|
||||
path.push(".cargo");
|
||||
if let Some(mut path) = get_cargo_home() {
|
||||
path.push("bin");
|
||||
path.push(executable_name);
|
||||
if let Some(path) = probe(path) {
|
||||
|
@ -60,6 +60,19 @@ fn lookup_in_path(exec: &str) -> bool {
|
|||
env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
|
||||
}
|
||||
|
||||
fn get_cargo_home() -> Option<PathBuf> {
|
||||
if let Some(path) = env::var_os("CARGO_HOME") {
|
||||
return Some(path.into());
|
||||
}
|
||||
|
||||
if let Some(mut path) = home::home_dir() {
|
||||
path.push(".cargo");
|
||||
return Some(path);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn probe(path: PathBuf) -> Option<PathBuf> {
|
||||
let with_extension = match env::consts::EXE_EXTENSION {
|
||||
"" => None,
|
||||
|
|
|
@ -156,19 +156,10 @@ export const getPathForExecutable = memoizeAsync(
|
|||
|
||||
if (await lookupInPath(executableName)) return executableName;
|
||||
|
||||
try {
|
||||
// hmm, `os.homedir()` seems to be infallible
|
||||
// it is not mentioned in docs and cannot be inferred by the type signature...
|
||||
const standardPath = vscode.Uri.joinPath(
|
||||
vscode.Uri.file(os.homedir()),
|
||||
".cargo",
|
||||
"bin",
|
||||
executableName
|
||||
);
|
||||
|
||||
const cargoHome = getCargoHome();
|
||||
if (cargoHome) {
|
||||
const standardPath = vscode.Uri.joinPath(cargoHome, "bin", executableName);
|
||||
if (await isFileAtUri(standardPath)) return standardPath.fsPath;
|
||||
} catch (err) {
|
||||
log.error("Failed to read the fs info", err);
|
||||
}
|
||||
return executableName;
|
||||
}
|
||||
|
@ -190,6 +181,21 @@ async function lookupInPath(exec: string): Promise<boolean> {
|
|||
return false;
|
||||
}
|
||||
|
||||
function getCargoHome(): vscode.Uri | null {
|
||||
const envVar = process.env["CARGO_HOME"];
|
||||
if (envVar) return vscode.Uri.file(envVar);
|
||||
|
||||
try {
|
||||
// hmm, `os.homedir()` seems to be infallible
|
||||
// it is not mentioned in docs and cannot be inferred by the type signature...
|
||||
return vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".cargo");
|
||||
} catch (err) {
|
||||
log.error("Failed to read the fs info", err);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function isFileAtPath(path: string): Promise<boolean> {
|
||||
return isFileAtUri(vscode.Uri.file(path));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue