Skip to content

Commit 19348bc

Browse files
bors[bot]gfreezy
andauthored
Merge #9908
9908: fix check of the toolchain's path r=lnicola a=gfreezy fixed #9907 Co-authored-by: Alex.F <gfreezy@gmail.com>
2 parents 3f4c515 + 8e4039c commit 19348bc

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

editors/code/src/tasks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ export async function buildCargoTask(
107107
// Check whether we must use a user-defined substitute for cargo.
108108
// Split on spaces to allow overrides like "wrapper cargo".
109109
const overrideCargo = definition.overrideCargo ?? definition.overrideCargo;
110-
const cargoCommand = overrideCargo?.split(" ") ?? [toolchain.cargoPath()];
110+
const cargoPath = await toolchain.cargoPath();
111+
const cargoCommand = overrideCargo?.split(" ") ?? [cargoPath];
111112

112113
const fullCommand = [...cargoCommand, ...args];
113114

editors/code/src/toolchain.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as os from 'os';
33
import * as path from 'path';
44
import * as readline from 'readline';
55
import * as vscode from 'vscode';
6-
import { execute, log, memoize } from './util';
6+
import { execute, log, memoizeAsync } from './util';
77

88
interface CompilationArtifact {
99
fileName: string;
@@ -89,13 +89,14 @@ export class Cargo {
8989
return artifacts[0].fileName;
9090
}
9191

92-
private runCargo(
92+
private async runCargo(
9393
cargoArgs: string[],
9494
onStdoutJson: (obj: any) => void,
9595
onStderrString: (data: string) => void
9696
): Promise<number> {
97-
return new Promise((resolve, reject) => {
98-
const cargo = cp.spawn(cargoPath(), cargoArgs, {
97+
const path = await cargoPath();
98+
return await new Promise((resolve, reject) => {
99+
const cargo = cp.spawn(path, cargoArgs, {
99100
stdio: ['ignore', 'pipe', 'pipe'],
100101
cwd: this.rootFolder
101102
});
@@ -121,15 +122,15 @@ export class Cargo {
121122
}
122123

123124
/** Mirrors `project_model::sysroot::discover_sysroot_dir()` implementation*/
124-
export function getSysroot(dir: string): Promise<string> {
125-
const rustcPath = getPathForExecutable("rustc");
125+
export async function getSysroot(dir: string): Promise<string> {
126+
const rustcPath = await getPathForExecutable("rustc");
126127

127128
// do not memoize the result because the toolchain may change between runs
128-
return execute(`${rustcPath} --print sysroot`, { cwd: dir });
129+
return await execute(`${rustcPath} --print sysroot`, { cwd: dir });
129130
}
130131

131132
export async function getRustcId(dir: string): Promise<string> {
132-
const rustcPath = getPathForExecutable("rustc");
133+
const rustcPath = await getPathForExecutable("rustc");
133134

134135
// do not memoize the result because the toolchain may change between runs
135136
const data = await execute(`${rustcPath} -V -v`, { cwd: dir });
@@ -139,35 +140,35 @@ export async function getRustcId(dir: string): Promise<string> {
139140
}
140141

141142
/** Mirrors `toolchain::cargo()` implementation */
142-
export function cargoPath(): string {
143+
export function cargoPath(): Promise<string> {
143144
return getPathForExecutable("cargo");
144145
}
145146

146147
/** Mirrors `toolchain::get_path_for_executable()` implementation */
147-
export const getPathForExecutable = memoize(
148+
export const getPathForExecutable = memoizeAsync(
148149
// We apply caching to decrease file-system interactions
149-
(executableName: "cargo" | "rustc" | "rustup"): string => {
150+
async (executableName: "cargo" | "rustc" | "rustup"): Promise<string> => {
150151
{
151152
const envVar = process.env[executableName.toUpperCase()];
152153
if (envVar) return envVar;
153154
}
154155

155-
if (lookupInPath(executableName)) return executableName;
156+
if (await lookupInPath(executableName)) return executableName;
156157

157158
try {
158159
// hmm, `os.homedir()` seems to be infallible
159160
// it is not mentioned in docs and cannot be infered by the type signature...
160161
const standardPath = vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".cargo", "bin", executableName);
161162

162-
if (isFileAtUri(standardPath)) return standardPath.fsPath;
163+
if (await isFileAtUri(standardPath)) return standardPath.fsPath;
163164
} catch (err) {
164165
log.error("Failed to read the fs info", err);
165166
}
166167
return executableName;
167168
}
168169
);
169170

170-
function lookupInPath(exec: string): boolean {
171+
async function lookupInPath(exec: string): Promise<boolean> {
171172
const paths = process.env.PATH ?? "";;
172173

173174
const candidates = paths.split(path.delimiter).flatMap(dirInPath => {
@@ -177,7 +178,12 @@ function lookupInPath(exec: string): boolean {
177178
: [candidate];
178179
});
179180

180-
return candidates.some(isFileAtPath);
181+
for await (const isFile of candidates.map(isFileAtPath)) {
182+
if (isFile) {
183+
return true;
184+
}
185+
}
186+
return false;
181187
}
182188

183189
async function isFileAtPath(path: string): Promise<boolean> {

editors/code/src/util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ export function setContextValue(key: string, value: any): Thenable<void> {
126126

127127
/**
128128
* Returns a higher-order function that caches the results of invoking the
129-
* underlying function.
129+
* underlying async function.
130130
*/
131-
export function memoize<Ret, TThis, Param extends string>(func: (this: TThis, arg: Param) => Ret) {
131+
export function memoizeAsync<Ret, TThis, Param extends string>(func: (this: TThis, arg: Param) => Promise<Ret>) {
132132
const cache = new Map<string, Ret>();
133133

134-
return function(this: TThis, arg: Param) {
134+
return async function(this: TThis, arg: Param) {
135135
const cached = cache.get(arg);
136136
if (cached) return cached;
137137

138-
const result = func.call(this, arg);
138+
const result = await func.call(this, arg);
139139
cache.set(arg, result);
140140

141141
return result;

0 commit comments

Comments
 (0)