@@ -425,21 +425,30 @@ process.on("message", (msg: m.Message) => {
425
425
process . send ! ( fakeSuccessResponse ) ;
426
426
process . send ! ( response ) ;
427
427
} else {
428
- let bscExists = false ;
429
- let bscPath = path . join ( projectRootPath , c . bscExePartialPath ) ;
430
- bscExists = fs . existsSync ( bscPath ) ;
431
- if ( ! bscExists ) {
432
- // In certain cases the native bsc binaries might be put in an unknown location
433
- // on the file system. Example: yarn workspaces.
434
- // The only other guarantee we have is that the node bsc should be available in the
435
- // project root + ./node_modules/.bin cf. package.json
436
- bscPath = path . join ( projectRootPath , c . bscNodePartialPath ) ;
437
- bscExists = fs . existsSync ( bscPath ) ;
438
- }
439
- if ( ! bscExists ) {
428
+ let bscExePath = path . join ( projectRootPath , c . bscExePartialPath ) ;
429
+ let bscNodePath = path . join ( projectRootPath , c . bscNodePartialPath ) ;
430
+ // There are the native bsc.exe binaries the bs-platform package, and
431
+ // the javascript bsc wrapper within node_modules/.bin. The JS wrapper
432
+ // starts and finds the right platform-specific native binary to call.
433
+ // This has nontrivial overhead each formatting, so we try to directly
434
+ // invoke the platform-specific native binary (inside bs-platform).
435
+
436
+ // However, we won't always be able to find the bs-platform package;
437
+ // for example yarn workspace lifts up the dependencies packages to a
438
+ // directory higher (grumbles); that setup only leaves us to find
439
+ // node_modules/.bin (which it doesn't touch, thankfully). So for
440
+ // quirky npm setups, we need to have a graceful degradation and find
441
+ // the JS bsc wrapper. If we can't even find node_modules/.bin then we
442
+ // give up
443
+ let resolvedBscPath = fs . existsSync ( bscExePath )
444
+ ? bscExePath
445
+ : fs . existsSync ( bscNodePath )
446
+ ? bscNodePath
447
+ : null ;
448
+ if ( resolvedBscPath === null ) {
440
449
let params : p . ShowMessageParams = {
441
450
type : p . MessageType . Error ,
442
- message : `Cannot find a nearby ${ c . bscNodePartialPath } . It's needed for formatting.` ,
451
+ message : `Cannot find a nearby ${ c . bscExePartialPath } nor ${ c . bscNodePartialPath } . It's needed for formatting.` ,
443
452
} ;
444
453
let response : m . NotificationMessage = {
445
454
jsonrpc : c . jsonrpcVersion ,
@@ -453,7 +462,7 @@ process.on("message", (msg: m.Message) => {
453
462
let code = getOpenedFileContent ( params . textDocument . uri ) ;
454
463
let formattedResult = utils . formatUsingValidBscPath (
455
464
code ,
456
- bscPath ,
465
+ resolvedBscPath ,
457
466
extension === c . resiExt
458
467
) ;
459
468
if ( formattedResult . kind === "success" ) {
0 commit comments