Skip to content

Commit

Permalink
Auto merge of rust-lang#14207 - tomokinat:master, r=lnicola
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Feb 26, 2023
2 parents 289208b + e4b184a commit 4e29820
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
21 changes: 17 additions & 4 deletions crates/toolchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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,
Expand Down
30 changes: 18 additions & 12 deletions editors/code/src/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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));
}
Expand Down

0 comments on commit 4e29820

Please sign in to comment.