Skip to content

Commit 4e29820

Browse files
committed
Auto merge of rust-lang#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.
2 parents 289208b + e4b184a commit 4e29820

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

crates/toolchain/src/lib.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
3131
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
3232
// 2) `<executable_name>`
3333
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
34-
// 3) `~/.cargo/bin/<executable_name>`
35-
// example: for cargo, this tries ~/.cargo/bin/cargo
34+
// 3) `$CARGO_HOME/bin/<executable_name>`
35+
// where $CARGO_HOME defaults to ~/.cargo (see https://doc.rust-lang.org/cargo/guide/cargo-home.html)
36+
// example: for cargo, this tries $CARGO_HOME/bin/cargo, or ~/.cargo/bin/cargo if $CARGO_HOME is unset.
3637
// It seems that this is a reasonable place to try for cargo, rustc, and rustup
3738
let env_var = executable_name.to_ascii_uppercase();
3839
if let Some(path) = env::var_os(env_var) {
@@ -43,8 +44,7 @@ fn get_path_for_executable(executable_name: &'static str) -> PathBuf {
4344
return executable_name.into();
4445
}
4546

46-
if let Some(mut path) = home::home_dir() {
47-
path.push(".cargo");
47+
if let Some(mut path) = get_cargo_home() {
4848
path.push("bin");
4949
path.push(executable_name);
5050
if let Some(path) = probe(path) {
@@ -60,6 +60,19 @@ fn lookup_in_path(exec: &str) -> bool {
6060
env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some()
6161
}
6262

63+
fn get_cargo_home() -> Option<PathBuf> {
64+
if let Some(path) = env::var_os("CARGO_HOME") {
65+
return Some(path.into());
66+
}
67+
68+
if let Some(mut path) = home::home_dir() {
69+
path.push(".cargo");
70+
return Some(path);
71+
}
72+
73+
None
74+
}
75+
6376
fn probe(path: PathBuf) -> Option<PathBuf> {
6477
let with_extension = match env::consts::EXE_EXTENSION {
6578
"" => None,

editors/code/src/toolchain.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,10 @@ export const getPathForExecutable = memoizeAsync(
156156

157157
if (await lookupInPath(executableName)) return executableName;
158158

159-
try {
160-
// hmm, `os.homedir()` seems to be infallible
161-
// it is not mentioned in docs and cannot be inferred by the type signature...
162-
const standardPath = vscode.Uri.joinPath(
163-
vscode.Uri.file(os.homedir()),
164-
".cargo",
165-
"bin",
166-
executableName
167-
);
168-
159+
const cargoHome = getCargoHome();
160+
if (cargoHome) {
161+
const standardPath = vscode.Uri.joinPath(cargoHome, "bin", executableName);
169162
if (await isFileAtUri(standardPath)) return standardPath.fsPath;
170-
} catch (err) {
171-
log.error("Failed to read the fs info", err);
172163
}
173164
return executableName;
174165
}
@@ -190,6 +181,21 @@ async function lookupInPath(exec: string): Promise<boolean> {
190181
return false;
191182
}
192183

184+
function getCargoHome(): vscode.Uri | null {
185+
const envVar = process.env["CARGO_HOME"];
186+
if (envVar) return vscode.Uri.file(envVar);
187+
188+
try {
189+
// hmm, `os.homedir()` seems to be infallible
190+
// it is not mentioned in docs and cannot be inferred by the type signature...
191+
return vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".cargo");
192+
} catch (err) {
193+
log.error("Failed to read the fs info", err);
194+
}
195+
196+
return null;
197+
}
198+
193199
async function isFileAtPath(path: string): Promise<boolean> {
194200
return isFileAtUri(vscode.Uri.file(path));
195201
}

0 commit comments

Comments
 (0)