Respect $CARGO_HOME when looking up toolchains.

This commit is contained in:
Tomoki Nakagawa 2023-02-24 15:26:25 +09:00
parent 289208bc9f
commit e4b184a776
2 changed files with 35 additions and 16 deletions

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