@@ -36,6 +36,11 @@ async function exec(command) {
36
36
} )
37
37
}
38
38
39
+ function logErr ( e , logFn = console . warn ) {
40
+ const message = e && '' + e
41
+ message && logFn ( message )
42
+ }
43
+
39
44
function clearIdeRustInfos ( ) {
40
45
for ( const note of atom . notifications . getNotifications ( ) ) {
41
46
if ( note . getOptions ( ) . _src === 'ide-rust' ) {
@@ -119,7 +124,7 @@ async function serverEnv(toolchain) {
119
124
env . RUST_SRC_PATH = path . join ( sysroot , "/lib/rustlib/src/rust/src/" )
120
125
}
121
126
catch ( e ) {
122
- console . error ( "Failed to find sysroot: " + e )
127
+ console . warn ( "Failed to find sysroot: " + e )
123
128
}
124
129
}
125
130
return env
@@ -267,7 +272,7 @@ class RustLanguageClient extends AutoLanguageClient {
267
272
. then ( ( ) => this . _checkToolchain ( ) )
268
273
. then ( ( ) => checkRls ( ) )
269
274
. then ( ( ) => this . _restartLanguageServers ( `Updated Rls toolchain` ) )
270
- . catch ( e => console . error ( e ) )
275
+ . catch ( logErr )
271
276
272
277
if ( this . busySignalService ) {
273
278
this . busySignalService . reportBusyWhile (
@@ -281,115 +286,128 @@ class RustLanguageClient extends AutoLanguageClient {
281
286
}
282
287
283
288
/**
284
- * Checks for rustup and nightly toolchain
285
- * If not found, asks to install. If user declines, throws error
286
- * @param {BusySignalService } [busySignalService]
287
- * @return {Promise<*> } toolchain is ok
289
+ * Checks for rustup, toolchain & rls components
290
+ * If not found prompts to fix & throws error
288
291
*/
289
- _checkToolchain ( busySignalService ) {
290
- return new Promise ( ( resolve , reject ) => {
291
- exec ( `rustup run ${ configToolchain ( ) } rustc --version` )
292
- . then ( resolve )
293
- . catch ( ( ) => checkHasRls ( configToolchain ( ) ) . then ( async hasRls => {
294
- // Toolchain not found, prompt to install
295
- let toolchain = configToolchain ( )
296
- const title = `\`rustup\` missing ${ toolchain } toolchain`
297
-
298
- if ( hasRls ) {
299
- atomPrompt ( title , {
300
- detail : `rustup toolchain install ${ toolchain } ` ,
301
- buttons : [ {
302
- text : 'Install' ,
303
- onDidClick : ( ) => {
304
- clearIdeRustInfos ( )
305
- let installPromise = installCompiler ( )
306
- . then ( ( ) => this . _checkToolchain ( ) )
307
- . then ( ( ) => this . _restartLanguageServers ( `Installed Rls toolchain` ) )
308
- . catch ( e => {
309
- console . warn ( e )
310
- clearIdeRustInfos ( )
311
- let err = ( e + '' ) . split ( '\n' )
312
- err = err . length && err [ 0 ] || `Error installing rust \`${ toolchain } \``
313
- atom . notifications . addError ( err , {
314
- detail : 'Check the toolchain is valid & connection is available' ,
315
- dismissable : true
316
- } )
317
- } )
318
-
319
- if ( busySignalService ) {
320
- busySignalService . reportBusyWhile (
321
- `Installing rust \`${ toolchain } \`` ,
322
- ( ) => installPromise
323
- )
324
- }
325
- }
326
- } ] ,
327
- } )
328
- }
329
- else {
330
- const note = {
331
- description : '**Warning**: This toolchain is unavilable or missing Rls.' ,
332
- buttons : [ {
333
- text : 'Configure' ,
334
- onDidClick : ( ) => atom . workspace . open ( 'atom://config/packages/ide-rust' )
335
- } ] ,
336
- }
292
+ async _checkToolchain ( ) {
293
+ const toolchain = configToolchain ( )
337
294
338
- if ( toolchain === 'nightly' ) {
339
- note . description += ' Try using a previous _dated_ nightly.'
340
- }
341
- else if ( toolchain . startsWith ( 'nightly' ) ) {
342
- note . description += ' Try using another nightly version.'
343
- }
295
+ try {
296
+ await exec ( `rustup run ${ toolchain } rustc --version` )
297
+ clearIdeRustInfos ( )
298
+ }
299
+ catch ( e ) {
300
+ this . _handleMissingToolchain ( toolchain )
301
+ throw e
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Takes appropriate action when missing a toolchain
307
+ * @param {string } toolchain
308
+ */
309
+ async _handleMissingToolchain ( toolchain ) {
310
+ try {
311
+ if ( await checkHasRls ( toolchain ) ) {
312
+ let clicked = await atomPrompt ( `\`rustup\` missing ${ toolchain } toolchain` , {
313
+ detail : `rustup toolchain install ${ toolchain } ` ,
314
+ } , [ 'Install' ] )
344
315
345
- let suggestChannel = toolchain . startsWith ( 'beta' ) && 'beta' ||
346
- toolchain . startsWith ( 'stable' ) && 'stable' ||
347
- 'nightly'
348
-
349
- try {
350
- let suggestedVersion = await suggestChannelOrDated ( suggestChannel )
351
- if ( suggestedVersion ) {
352
- note . buttons . push ( {
353
- text : `Use ${ suggestedVersion } ` ,
354
- className : 'btn-success' ,
355
- onDidClick : ( ) => {
356
- clearIdeRustInfos ( )
357
- atom . config . set ( 'ide-rust.rlsToolchain' , suggestedVersion )
358
- }
359
- } )
360
- }
361
- } catch ( e ) {
316
+ if ( clicked === 'Install' ) {
317
+ clearIdeRustInfos ( )
318
+ const installPromise = installCompiler ( )
319
+ . then ( ( ) => this . _checkToolchain ( ) )
320
+ . then ( ( ) => this . _restartLanguageServers ( `Installed Rls toolchain` ) )
321
+ . catch ( e => {
362
322
console . warn ( e )
363
- }
323
+ clearIdeRustInfos ( )
324
+ let err = ( e + '' ) . split ( '\n' )
325
+ err = err . length && err [ 0 ] || `Error installing rust \`${ toolchain } \``
326
+ atom . notifications . addError ( err , {
327
+ detail : 'Check the toolchain is valid & connection is available' ,
328
+ dismissable : true
329
+ } )
330
+ } )
364
331
365
- atomPrompt ( title , note )
332
+ if ( this . busySignalService ) {
333
+ this . busySignalService . reportBusyWhile (
334
+ `Installing rust \`${ toolchain } \`` ,
335
+ ( ) => installPromise
336
+ )
366
337
}
367
- } ) )
368
- . catch ( e => {
369
- e && console . warn ( e )
370
- // Missing rustup, prompt to install
371
- atomPrompt (
372
- "`rustup` is not available" ,
373
- {
374
- description : "From https://www.rustup.rs/" ,
375
- detail : "curl https://sh.rustup.rs -sSf | sh"
376
- } ,
377
- [ "Install" ]
378
- ) . then ( response => {
379
- if ( response === "Install" ) {
380
- // Install rustup and try again
381
- installRustup ( )
382
- . then ( ( ) => this . _checkToolchain ( ) )
383
- . then ( resolve )
384
- . catch ( reject )
385
- } else {
386
- reject ( )
387
- }
388
- } )
389
- } )
390
- . then ( ( ) => reject ( ) )
391
- } )
392
- . then ( ( ) => clearIdeRustInfos ( ) )
338
+ }
339
+ }
340
+ else {
341
+ this . _handleMissingToolchainMissingRls ( toolchain )
342
+ }
343
+ }
344
+ catch ( e ) {
345
+ logErr ( e )
346
+ this . _handleMissingRustup ( )
347
+ }
348
+ }
349
+
350
+ /**
351
+ * Takes appropriate action when missing a toolchain that itself is missing
352
+ * or missing vital components
353
+ * @param {string } toolchain
354
+ */
355
+ async _handleMissingToolchainMissingRls ( toolchain ) {
356
+ const note = {
357
+ description : '**Warning**: This toolchain is unavilable or missing Rls.' ,
358
+ buttons : [ {
359
+ text : 'Configure' ,
360
+ onDidClick : ( ) => atom . workspace . open ( 'atom://config/packages/ide-rust' )
361
+ } ] ,
362
+ }
363
+
364
+ if ( toolchain === 'nightly' ) {
365
+ note . description += ' Try using a previous _dated_ nightly.'
366
+ }
367
+ else if ( toolchain . startsWith ( 'nightly' ) ) {
368
+ note . description += ' Try using another nightly version.'
369
+ }
370
+
371
+ let suggestChannel = toolchain . startsWith ( 'beta' ) && 'beta' ||
372
+ toolchain . startsWith ( 'stable' ) && 'stable' ||
373
+ 'nightly'
374
+
375
+ try {
376
+ let suggestedVersion = await suggestChannelOrDated ( suggestChannel )
377
+ if ( suggestedVersion ) {
378
+ note . buttons . push ( {
379
+ text : `Use ${ suggestedVersion } ` ,
380
+ className : 'btn-success' ,
381
+ onDidClick : ( ) => {
382
+ clearIdeRustInfos ( )
383
+ atom . config . set ( 'ide-rust.rlsToolchain' , suggestedVersion )
384
+ }
385
+ } )
386
+ }
387
+ } catch ( e ) {
388
+ console . warn ( e )
389
+ }
390
+
391
+ atomPrompt ( `\`rustup\` missing ${ toolchain } toolchain` , note )
392
+ }
393
+
394
+ /** Takes appropriate action when missing rustup */
395
+ async _handleMissingRustup ( ) {
396
+ try {
397
+ let clicked = await atomPrompt ( "`rustup` is not available" , {
398
+ description : "From https://www.rustup.rs/" ,
399
+ detail : "curl https://sh.rustup.rs -sSf | sh"
400
+ } , [ "Install" ] )
401
+
402
+ if ( clicked === "Install" ) {
403
+ // Install rustup and try again
404
+ await installRustup ( )
405
+ this . _checkToolchain ( ) . catch ( logErr )
406
+ }
407
+ }
408
+ catch ( e ) {
409
+ e && console . warn ( e )
410
+ }
393
411
}
394
412
395
413
activate ( ) {
@@ -414,25 +432,25 @@ class RustLanguageClient extends AutoLanguageClient {
414
432
// Watch config toolchain changes -> switch, install & update toolchains, restart servers
415
433
this . disposables . add ( atom . config . onDidChange ( 'ide-rust.rlsToolchain' ,
416
434
_ . debounce ( ( { newValue } ) => {
417
- this . _checkToolchain ( this . busySignalService )
435
+ return this . _checkToolchain ( )
418
436
. then ( ( ) => checkRls ( this . busySignalService ) )
419
437
. then ( ( ) => this . _restartLanguageServers ( `Switched Rls toolchain to \`${ newValue } \`` ) )
420
438
. then ( ( ) => this . _promptToUpdateToolchain ( ) )
421
- . catch ( e => e && console . warn ( e ) )
439
+ . catch ( e => logErr ( e , console . info ) )
422
440
} , 1000 )
423
441
) )
424
442
425
443
// watch config toolchain updates -> check for updates if enabling
426
444
this . disposables . add ( atom . config . onDidChange ( 'ide-rust.checkForToolchainUpdates' ,
427
445
( { newValue : enabled } ) => {
428
- if ( enabled ) this . _promptToUpdateToolchain ( ) . catch ( e => e && console . warn ( e ) )
446
+ if ( enabled ) this . _promptToUpdateToolchain ( ) . catch ( logErr )
429
447
}
430
448
) )
431
449
432
450
// check for updates (if enabled) every so often
433
451
let periodicUpdateTimeoutId
434
452
const periodicUpdate = async ( ) => {
435
- await this . _promptToUpdateToolchain ( ) . catch ( e => e && console . warn ( e ) )
453
+ await this . _promptToUpdateToolchain ( ) . catch ( logErr )
436
454
periodicUpdateTimeoutId = setTimeout ( periodicUpdate , PERIODIC_UPDATE_CHECK_MILLIS )
437
455
}
438
456
this . disposables . add ( new Disposable ( ( ) => {
@@ -509,7 +527,7 @@ class RustLanguageClient extends AutoLanguageClient {
509
527
}
510
528
511
529
try {
512
- await this . _checkToolchain ( this . busySignalService )
530
+ await this . _checkToolchain ( )
513
531
await checkRls ( this . busySignalService )
514
532
let toolchain = configToolchain ( )
515
533
return cp . spawn ( "rustup" , [ "run" , toolchain , "rls" ] , {
@@ -518,7 +536,7 @@ class RustLanguageClient extends AutoLanguageClient {
518
536
} )
519
537
}
520
538
catch ( e ) {
521
- throw e || new Error ( "failed to start server" )
539
+ throw new Error ( "failed to start server: " + e )
522
540
}
523
541
}
524
542
}
0 commit comments