@@ -3,7 +3,7 @@ import * as os from 'os';
33import * as path from 'path' ;
44import * as readline from 'readline' ;
55import * as vscode from 'vscode' ;
6- import { execute , log , memoize } from './util' ;
6+ import { execute , log , memoizeAsync } from './util' ;
77
88interface 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
131132export 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
183189async function isFileAtPath ( path : string ) : Promise < boolean > {
0 commit comments