Skip to content

Commit 5caa099

Browse files
committed
Tweak 1beaaf8
1 parent c51eaf6 commit 5caa099

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

server/src/server.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -425,21 +425,30 @@ process.on("message", (msg: m.Message) => {
425425
process.send!(fakeSuccessResponse);
426426
process.send!(response);
427427
} 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) {
440449
let params: p.ShowMessageParams = {
441450
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.`,
443452
};
444453
let response: m.NotificationMessage = {
445454
jsonrpc: c.jsonrpcVersion,
@@ -453,7 +462,7 @@ process.on("message", (msg: m.Message) => {
453462
let code = getOpenedFileContent(params.textDocument.uri);
454463
let formattedResult = utils.formatUsingValidBscPath(
455464
code,
456-
bscPath,
465+
resolvedBscPath,
457466
extension === c.resiExt
458467
);
459468
if (formattedResult.kind === "success") {

0 commit comments

Comments
 (0)