diff --git a/server/src/constants.ts b/server/src/constants.ts index ddce1cf98..f9c823cf5 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -6,11 +6,12 @@ let platformDir = // See https://microsoft.github.io/language-server-protocol/specification Abstract Message // version is fixed to 2.0 export let jsonrpcVersion = "2.0"; +export let platformPath = path.join("rescript", platformDir); +export let nodeModulesPlatformPath = path.join("node_modules", platformPath); +export let bscExeName = "bsc.exe"; export let bscNativeReScriptPartialPath = path.join( - "node_modules", - "rescript", - platformDir, - "bsc.exe" + nodeModulesPlatformPath, + bscExeName ); export let analysisDevPath = path.join( diff --git a/server/src/server.ts b/server/src/server.ts index 76c78e0b7..838641421 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -79,14 +79,16 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {}; // will be properly defined later depending on the mode (stdio/node-rpc) let send: (msg: p.Message) => void = (_) => {}; -let findBinaryDirPathFromProjectRoot = ( +// Check if the rescript binary is available at node_modules/.bin/rescript, +// otherwise recursively check parent directories for it. +let findBinaryPathFromProjectRoot = ( directory: p.DocumentUri // This must be a directory and not a file! ): null | p.DocumentUri => { let binaryDirPath = path.join(directory, c.nodeModulesBinDir); let binaryPath = path.join(binaryDirPath, c.rescriptBinName); if (fs.existsSync(binaryPath)) { - return binaryDirPath; + return binaryPath; } let parentDir = path.dirname(directory); @@ -95,19 +97,26 @@ let findBinaryDirPathFromProjectRoot = ( return null; } - return findBinaryDirPathFromProjectRoot(parentDir); + return findBinaryPathFromProjectRoot(parentDir); }; -let getBinaryDirPath = (projectRootPath: p.DocumentUri) => - extensionConfiguration.binaryPath == null - ? findBinaryDirPathFromProjectRoot(projectRootPath) - : extensionConfiguration.binaryPath; - let findRescriptBinary = (projectRootPath: p.DocumentUri) => - utils.findRescriptBinary(getBinaryDirPath(projectRootPath)); - -let findBscBinary = (projectRootPath: p.DocumentUri) => - utils.findBscBinary(getBinaryDirPath(projectRootPath)); + extensionConfiguration.binaryPath == null + ? findBinaryPathFromProjectRoot(projectRootPath) + : utils.findRescriptBinary(extensionConfiguration.binaryPath); + +let findBscBinary = (projectRootPath: p.DocumentUri) => { + let rescriptBinaryPath = findRescriptBinary(projectRootPath); + if (rescriptBinaryPath !== null) { + return path.join( + path.dirname(rescriptBinaryPath), + "..", + c.platformPath, + c.bscExeName + ); + } + return null; +}; interface CreateInterfaceRequestParams { uri: string; @@ -312,9 +321,13 @@ let openedFile = (fileUri: string, fileContent: string) => { method: "window/showMessage", params: { type: p.MessageType.Error, - message: `Can't find ReScript binary in the directory ${getBinaryDirPath( - projectRootPath - )}`, + message: + extensionConfiguration.binaryPath == null + ? `Can't find ReScript binary in ${path.join( + projectRootPath, + c.nodeModulesBinDir + )} or parent directories. Did you install it? It's required to use "rescript" > 9.1` + : `Can't find ReScript binary in the directory ${extensionConfiguration.binaryPath}`, }, }; send(request);