1
+ import { join } from 'path' ;
1
2
import * as grpc from '@grpc/grpc-js' ;
2
3
import {
3
4
inject ,
@@ -30,6 +31,8 @@ import { DisposableCollection } from '@theia/core/lib/common/disposable';
30
31
import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol' ;
31
32
import { v4 } from 'uuid' ;
32
33
import { InstallWithProgress } from './grpc-installable' ;
34
+ import type { DefaultCliConfig } from './cli-config' ;
35
+ import { ServiceError } from './service-error' ;
33
36
34
37
@injectable ( )
35
38
export class CoreClientProvider {
@@ -228,8 +231,12 @@ export class CoreClientProvider {
228
231
}
229
232
230
233
private evaluateErrorStatus ( status : RpcStatus [ ] ) : Error | undefined {
231
- const error = isIndexUpdateRequiredBeforeInit ( status ) ; // put future error matching here
232
- return error ;
234
+ const { cliConfiguration } = this . configService ;
235
+ if ( ! cliConfiguration ) {
236
+ // If the CLI config is not available, do not even try to guess what went wrong.
237
+ return new Error ( `Could not read the CLI configuration file.` ) ;
238
+ }
239
+ return isIndexUpdateRequiredBeforeInit ( status , cliConfiguration ) ; // put future error matching here
233
240
}
234
241
235
242
private async updateIndexes (
@@ -281,10 +288,15 @@ export class CoreClientProvider {
281
288
this . updateLibraryIndex ( client , progressId , onDidProgressEmitter ) ,
282
289
] ) ;
283
290
this . notificationService . notifyIndexDidUpdate ( progressId ) ;
284
- return client ;
291
+ } catch ( err ) {
292
+ this . notificationService . notifyIndexUpdateDidFail ( {
293
+ progressId,
294
+ message : ServiceError . is ( err ) ? err . details : String ( err ) ,
295
+ } ) ;
285
296
} finally {
286
297
toDispose . dispose ( ) ;
287
298
}
299
+ return client ;
288
300
}
289
301
290
302
private async updateIndex (
@@ -425,13 +437,14 @@ ${causes
425
437
}
426
438
427
439
function isIndexUpdateRequiredBeforeInit (
428
- status : RpcStatus [ ]
440
+ status : RpcStatus [ ] ,
441
+ cliConfig : DefaultCliConfig
429
442
) : IndexUpdateRequiredBeforeInitError | undefined {
430
443
const causes = status
431
444
. filter ( ( s ) =>
432
- IndexUpdateRequiredBeforeInit . map ( ( predicate ) => predicate ( s ) ) . some (
433
- Boolean
434
- )
445
+ IndexUpdateRequiredBeforeInit . map ( ( predicate ) =>
446
+ predicate ( s , cliConfig )
447
+ ) . some ( Boolean )
435
448
)
436
449
. map ( ( s ) => RpcStatus . toObject ( false , s ) ) ;
437
450
return causes . length
@@ -442,9 +455,14 @@ const IndexUpdateRequiredBeforeInit = [
442
455
isPackageIndexMissingStatus ,
443
456
isDiscoveryNotFoundStatus ,
444
457
] ;
445
- function isPackageIndexMissingStatus ( status : RpcStatus ) : boolean {
458
+ function isPackageIndexMissingStatus (
459
+ status : RpcStatus ,
460
+ { directories : { data } } : DefaultCliConfig
461
+ ) : boolean {
446
462
const predicate = ( { message } : RpcStatus . AsObject ) =>
447
- message . includes ( 'loading json index file' ) ;
463
+ message . includes ( 'loading json index file' ) &&
464
+ ( message . includes ( join ( data , 'package_index.json' ) ) ||
465
+ message . includes ( join ( data , 'library_index.json' ) ) ) ;
448
466
// https://github.com/arduino/arduino-cli/blob/f0245bc2da6a56fccea7b2c9ea09e85fdcc52cb8/arduino/cores/packagemanager/package_manager.go#L247
449
467
return evaluate ( status , predicate ) ;
450
468
}
@@ -461,5 +479,6 @@ function evaluate(
461
479
predicate : ( error : RpcStatus . AsObject ) => boolean
462
480
) : boolean {
463
481
const status = RpcStatus . toObject ( false , subject ) ;
464
- return predicate ( status ) ;
482
+ const result = predicate ( status ) ;
483
+ return result ;
465
484
}
0 commit comments