Auto merge of #14207 - tomokinat:master, r=lnicola

Respect $CARGO_HOME when looking up toolchains.

Some people set `$CARGO_HOME` to a location other than `~/.cargo` (`$XDG_DATA_DIR/cargo`,  in my case), and I'd be a little nicer if the rust-analyzer extension and server respect that value when looking up toolchains, instead of having us configure all of `$CARGO`, `$RUSTC` ... manually.

The new implementation still defaults to `~/.cargo` if `$CARGO_HOME` is unset, pretty much like cargo itself does (as documented in https://doc.rust-lang.org/cargo/guide/cargo-home.html), so the change is backwards compatible for most people except those who has configured `$CARGO_HOME` explicitly.

I considered using https://crates.io/crates/home as suggested by https://doc.rust-lang.org/cargo/guide/cargo-home.html, but decided to put int on hold because i) we need mirror impl in node, ii) I thought the consistency matters more and iii) the new implementation shouldn't be worse than the current one (i.e. switching to `home` improvement is rather orthogonal and could be done in another PR). If you have any directions on this, please let me know.
This commit is contained in:
bors 2023-02-26 15:45:13 +00:00
commit 4e29820f6d
2 changed files with 35 additions and 16 deletions

View file

@ -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,

View file

@ -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));
}