@@ -3,7 +3,7 @@ import * as os from 'os';
3
3
import * as path from 'path' ;
4
4
import * as readline from 'readline' ;
5
5
import * as vscode from 'vscode' ;
6
- import { execute , log , memoize } from './util' ;
6
+ import { execute , log , memoizeAsync } from './util' ;
7
7
8
8
interface CompilationArtifact {
9
9
fileName : string ;
@@ -89,13 +89,14 @@ export class Cargo {
89
89
return artifacts [ 0 ] . fileName ;
90
90
}
91
91
92
- private runCargo (
92
+ private async runCargo (
93
93
cargoArgs : string [ ] ,
94
94
onStdoutJson : ( obj : any ) => void ,
95
95
onStderrString : ( data : string ) => void
96
96
) : 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 , {
99
100
stdio : [ 'ignore' , 'pipe' , 'pipe' ] ,
100
101
cwd : this . rootFolder
101
102
} ) ;
@@ -121,15 +122,15 @@ export class Cargo {
121
122
}
122
123
123
124
/** 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" ) ;
126
127
127
128
// 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 } ) ;
129
130
}
130
131
131
132
export async function getRustcId ( dir : string ) : Promise < string > {
132
- const rustcPath = getPathForExecutable ( "rustc" ) ;
133
+ const rustcPath = await getPathForExecutable ( "rustc" ) ;
133
134
134
135
// do not memoize the result because the toolchain may change between runs
135
136
const data = await execute ( `${ rustcPath } -V -v` , { cwd : dir } ) ;
@@ -139,35 +140,35 @@ export async function getRustcId(dir: string): Promise<string> {
139
140
}
140
141
141
142
/** Mirrors `toolchain::cargo()` implementation */
142
- export function cargoPath ( ) : string {
143
+ export function cargoPath ( ) : Promise < string > {
143
144
return getPathForExecutable ( "cargo" ) ;
144
145
}
145
146
146
147
/** Mirrors `toolchain::get_path_for_executable()` implementation */
147
- export const getPathForExecutable = memoize (
148
+ export const getPathForExecutable = memoizeAsync (
148
149
// We apply caching to decrease file-system interactions
149
- ( executableName : "cargo" | "rustc" | "rustup" ) : string => {
150
+ async ( executableName : "cargo" | "rustc" | "rustup" ) : Promise < string > => {
150
151
{
151
152
const envVar = process . env [ executableName . toUpperCase ( ) ] ;
152
153
if ( envVar ) return envVar ;
153
154
}
154
155
155
- if ( lookupInPath ( executableName ) ) return executableName ;
156
+ if ( await lookupInPath ( executableName ) ) return executableName ;
156
157
157
158
try {
158
159
// hmm, `os.homedir()` seems to be infallible
159
160
// it is not mentioned in docs and cannot be infered by the type signature...
160
161
const standardPath = vscode . Uri . joinPath ( vscode . Uri . file ( os . homedir ( ) ) , ".cargo" , "bin" , executableName ) ;
161
162
162
- if ( isFileAtUri ( standardPath ) ) return standardPath . fsPath ;
163
+ if ( await isFileAtUri ( standardPath ) ) return standardPath . fsPath ;
163
164
} catch ( err ) {
164
165
log . error ( "Failed to read the fs info" , err ) ;
165
166
}
166
167
return executableName ;
167
168
}
168
169
) ;
169
170
170
- function lookupInPath ( exec : string ) : boolean {
171
+ async function lookupInPath ( exec : string ) : Promise < boolean > {
171
172
const paths = process . env . PATH ?? "" ; ;
172
173
173
174
const candidates = paths . split ( path . delimiter ) . flatMap ( dirInPath => {
@@ -177,7 +178,12 @@ function lookupInPath(exec: string): boolean {
177
178
: [ candidate ] ;
178
179
} ) ;
179
180
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 ;
181
187
}
182
188
183
189
async function isFileAtPath ( path : string ) : Promise < boolean > {
0 commit comments