11import * as vscode from "vscode" ;
22import * as os from "os" ;
33import type { Config } from "./config" ;
4- import { type Env , log } from "./util" ;
4+ import { type Env , log , spawnAsync } from "./util" ;
55import type { PersistentState } from "./persistent_state" ;
6- import { exec , spawnSync } from "child_process" ;
6+ import { exec } from "child_process" ;
77import { TextDecoder } from "node:util" ;
88
99export async function bootstrap (
@@ -61,13 +61,12 @@ async function getServer(
6161 // if so, use the rust-analyzer component
6262 const toolchainUri = vscode . Uri . joinPath ( workspaceFolder . uri , "rust-toolchain.toml" ) ;
6363 if ( await hasToolchainFileWithRaDeclared ( toolchainUri ) ) {
64- const res = spawnSync ( "rustup" , [ "which" , "rust-analyzer" ] , {
65- encoding : "utf8" ,
64+ const res = await spawnAsync ( "rustup" , [ "which" , "rust-analyzer" ] , {
6665 env : { ...process . env } ,
6766 cwd : workspaceFolder . uri . fsPath ,
6867 } ) ;
6968 if ( ! res . error && res . status === 0 ) {
70- toolchainServerPath = earliestToolchainPath (
69+ toolchainServerPath = await earliestToolchainPath (
7170 toolchainServerPath ,
7271 res . stdout . trim ( ) ,
7372 raVersionResolver ,
@@ -114,10 +113,8 @@ async function getServer(
114113}
115114
116115// Given a path to a rust-analyzer executable, resolve its version and return it.
117- function raVersionResolver ( path : string ) : string | undefined {
118- const res = spawnSync ( path , [ "--version" ] , {
119- encoding : "utf8" ,
120- } ) ;
116+ async function raVersionResolver ( path : string ) : Promise < string | undefined > {
117+ const res = await spawnAsync ( path , [ "--version" ] ) ;
121118 if ( ! res . error && res . status === 0 ) {
122119 return res . stdout ;
123120 } else {
@@ -126,13 +123,16 @@ function raVersionResolver(path: string): string | undefined {
126123}
127124
128125// Given a path to two rust-analyzer executables, return the earliest one by date.
129- function earliestToolchainPath (
126+ async function earliestToolchainPath (
130127 path0 : string | undefined ,
131128 path1 : string ,
132- raVersionResolver : ( path : string ) => string | undefined ,
133- ) : string {
129+ raVersionResolver : ( path : string ) => Promise < string | undefined > ,
130+ ) : Promise < string > {
134131 if ( path0 ) {
135- if ( orderFromPath ( path0 , raVersionResolver ) < orderFromPath ( path1 , raVersionResolver ) ) {
132+ if (
133+ ( await orderFromPath ( path0 , raVersionResolver ) ) <
134+ ( await orderFromPath ( path1 , raVersionResolver ) )
135+ ) {
136136 return path0 ;
137137 } else {
138138 return path1 ;
@@ -150,11 +150,11 @@ function earliestToolchainPath(
150150// nightly - /Users/myuser/.rustup/toolchains/nightly-2022-11-22-aarch64-apple-darwin/bin/rust-analyzer
151151// versioned - /Users/myuser/.rustup/toolchains/1.72.1-aarch64-apple-darwin/bin/rust-analyzer
152152// stable - /Users/myuser/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rust-analyzer
153- function orderFromPath (
153+ async function orderFromPath (
154154 path : string ,
155- raVersionResolver : ( path : string ) => string | undefined ,
156- ) : string {
157- const raVersion = raVersionResolver ( path ) ;
155+ raVersionResolver : ( path : string ) => Promise < string | undefined > ,
156+ ) : Promise < string > {
157+ const raVersion = await raVersionResolver ( path ) ;
158158 const raDate = raVersion ?. match ( / ^ r u s t - a n a l y z e r .* \( .* ( \d { 4 } - \d { 2 } - \d { 2 } ) \) $ / ) ;
159159 if ( raDate ?. length === 2 ) {
160160 const precedence = path . includes ( "nightly-" ) ? "0" : "1" ;
@@ -184,11 +184,10 @@ async function hasToolchainFileWithRaDeclared(uri: vscode.Uri): Promise<boolean>
184184 }
185185}
186186
187- export function isValidExecutable ( path : string , extraEnv : Env ) : boolean {
187+ export async function isValidExecutable ( path : string , extraEnv : Env ) : Promise < boolean > {
188188 log . debug ( "Checking availability of a binary at" , path ) ;
189189
190- const res = spawnSync ( path , [ "--version" ] , {
191- encoding : "utf8" ,
190+ const res = await spawnAsync ( path , [ "--version" ] , {
192191 env : { ...process . env , ...extraEnv } ,
193192 } ) ;
194193
0 commit comments