1
1
import * as vscode from "vscode" ;
2
2
import * as os from "os" ;
3
3
import type { Config } from "./config" ;
4
- import { type Env , log } from "./util" ;
4
+ import { type Env , log , spawnAsync } from "./util" ;
5
5
import type { PersistentState } from "./persistent_state" ;
6
- import { exec , spawnSync } from "child_process" ;
6
+ import { exec } from "child_process" ;
7
7
import { TextDecoder } from "node:util" ;
8
8
9
9
export async function bootstrap (
@@ -61,13 +61,12 @@ async function getServer(
61
61
// if so, use the rust-analyzer component
62
62
const toolchainUri = vscode . Uri . joinPath ( workspaceFolder . uri , "rust-toolchain.toml" ) ;
63
63
if ( await hasToolchainFileWithRaDeclared ( toolchainUri ) ) {
64
- const res = spawnSync ( "rustup" , [ "which" , "rust-analyzer" ] , {
65
- encoding : "utf8" ,
64
+ const res = await spawnAsync ( "rustup" , [ "which" , "rust-analyzer" ] , {
66
65
env : { ...process . env } ,
67
66
cwd : workspaceFolder . uri . fsPath ,
68
67
} ) ;
69
68
if ( ! res . error && res . status === 0 ) {
70
- toolchainServerPath = earliestToolchainPath (
69
+ toolchainServerPath = await earliestToolchainPath (
71
70
toolchainServerPath ,
72
71
res . stdout . trim ( ) ,
73
72
raVersionResolver ,
@@ -114,10 +113,8 @@ async function getServer(
114
113
}
115
114
116
115
// 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" ] ) ;
121
118
if ( ! res . error && res . status === 0 ) {
122
119
return res . stdout ;
123
120
} else {
@@ -126,13 +123,16 @@ function raVersionResolver(path: string): string | undefined {
126
123
}
127
124
128
125
// Given a path to two rust-analyzer executables, return the earliest one by date.
129
- function earliestToolchainPath (
126
+ async function earliestToolchainPath (
130
127
path0 : string | undefined ,
131
128
path1 : string ,
132
- raVersionResolver : ( path : string ) => string | undefined ,
133
- ) : string {
129
+ raVersionResolver : ( path : string ) => Promise < string | undefined > ,
130
+ ) : Promise < string > {
134
131
if ( path0 ) {
135
- if ( orderFromPath ( path0 , raVersionResolver ) < orderFromPath ( path1 , raVersionResolver ) ) {
132
+ if (
133
+ ( await orderFromPath ( path0 , raVersionResolver ) ) <
134
+ ( await orderFromPath ( path1 , raVersionResolver ) )
135
+ ) {
136
136
return path0 ;
137
137
} else {
138
138
return path1 ;
@@ -150,11 +150,11 @@ function earliestToolchainPath(
150
150
// nightly - /Users/myuser/.rustup/toolchains/nightly-2022-11-22-aarch64-apple-darwin/bin/rust-analyzer
151
151
// versioned - /Users/myuser/.rustup/toolchains/1.72.1-aarch64-apple-darwin/bin/rust-analyzer
152
152
// stable - /Users/myuser/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rust-analyzer
153
- function orderFromPath (
153
+ async function orderFromPath (
154
154
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 ) ;
158
158
const raDate = raVersion ?. match ( / ^ r u s t - a n a l y z e r .* \( .* ( \d { 4 } - \d { 2 } - \d { 2 } ) \) $ / ) ;
159
159
if ( raDate ?. length === 2 ) {
160
160
const precedence = path . includes ( "nightly-" ) ? "0" : "1" ;
@@ -184,11 +184,10 @@ async function hasToolchainFileWithRaDeclared(uri: vscode.Uri): Promise<boolean>
184
184
}
185
185
}
186
186
187
- export function isValidExecutable ( path : string , extraEnv : Env ) : boolean {
187
+ export async function isValidExecutable ( path : string , extraEnv : Env ) : Promise < boolean > {
188
188
log . debug ( "Checking availability of a binary at" , path ) ;
189
189
190
- const res = spawnSync ( path , [ "--version" ] , {
191
- encoding : "utf8" ,
190
+ const res = await spawnAsync ( path , [ "--version" ] , {
192
191
env : { ...process . env , ...extraEnv } ,
193
192
} ) ;
194
193
0 commit comments