From ef9251a728ce6f6f1d2ea7602fb335ed112cef82 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Wed, 27 Jul 2022 11:56:34 +0200 Subject: [PATCH 1/3] Call bsc.exe directly, when no binaryPath is set in settings --- server/src/constants.ts | 9 +++------ server/src/server.ts | 38 ++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/server/src/constants.ts b/server/src/constants.ts index ddce1cf98..e5e7b79ae 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -6,12 +6,8 @@ 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 bscNativeReScriptPartialPath = path.join( - "node_modules", - "rescript", - platformDir, - "bsc.exe" -); +export let platformPath = path.join("node_modules", "rescript", platformDir); +export let bscNativeReScriptPartialPath = path.join(platformPath, "bsc.exe"); export let analysisDevPath = path.join( path.dirname(__dirname), @@ -29,6 +25,7 @@ export let analysisProdPath = path.join( export let rescriptBinName = "rescript"; export let bscBinName = "bsc"; +export let bscExeName = "bsc.exe"; export let nodeModulesBinDir = path.join("node_modules", ".bin"); diff --git a/server/src/server.ts b/server/src/server.ts index 76c78e0b7..e5f05c746 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 = ( - directory: p.DocumentUri // This must be a directory and not a file! +let findBinaryFromProjectDirRec = ( + directory: p.DocumentUri, // This must be a directory and not a file! + targetDir: p.DocumentUri, + binaryName: p.DocumentUri ): null | p.DocumentUri => { - let binaryDirPath = path.join(directory, c.nodeModulesBinDir); - let binaryPath = path.join(binaryDirPath, c.rescriptBinName); + let binaryDirPath = path.join(directory, targetDir); + let binaryPath = path.join(binaryDirPath, binaryName); if (fs.existsSync(binaryPath)) { - return binaryDirPath; + return binaryPath; } let parentDir = path.dirname(directory); @@ -95,19 +97,22 @@ let findBinaryDirPathFromProjectRoot = ( return null; } - return findBinaryDirPathFromProjectRoot(parentDir); + return findBinaryFromProjectDirRec(parentDir, targetDir, binaryName); }; -let getBinaryDirPath = (projectRootPath: p.DocumentUri) => - extensionConfiguration.binaryPath == null - ? findBinaryDirPathFromProjectRoot(projectRootPath) - : extensionConfiguration.binaryPath; - let findRescriptBinary = (projectRootPath: p.DocumentUri) => - utils.findRescriptBinary(getBinaryDirPath(projectRootPath)); + extensionConfiguration.binaryPath == null + ? findBinaryFromProjectDirRec( + projectRootPath, + c.nodeModulesBinDir, + c.rescriptBinName + ) + : utils.findRescriptBinary(extensionConfiguration.binaryPath); let findBscBinary = (projectRootPath: p.DocumentUri) => - utils.findBscBinary(getBinaryDirPath(projectRootPath)); + extensionConfiguration.binaryPath == null + ? findBinaryFromProjectDirRec(projectRootPath, c.platformPath, c.bscExeName) + : utils.findBscBinary(extensionConfiguration.binaryPath); interface CreateInterfaceRequestParams { uri: string; @@ -312,9 +317,10 @@ 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" + : `Can't find ReScript binary in the directory ${extensionConfiguration.binaryPath}`, }, }; send(request); From 6f574d2480d8e45aed92ac14d39da64b63ff48fa Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Sun, 31 Jul 2022 12:10:55 +0200 Subject: [PATCH 2/3] Base bsc.exe lookup on rescript binary lookup --- server/src/constants.ts | 10 +++++++--- server/src/server.ts | 34 ++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/server/src/constants.ts b/server/src/constants.ts index e5e7b79ae..f9c823cf5 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -6,8 +6,13 @@ 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("node_modules", "rescript", platformDir); -export let bscNativeReScriptPartialPath = path.join(platformPath, "bsc.exe"); +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( + nodeModulesPlatformPath, + bscExeName +); export let analysisDevPath = path.join( path.dirname(__dirname), @@ -25,7 +30,6 @@ export let analysisProdPath = path.join( export let rescriptBinName = "rescript"; export let bscBinName = "bsc"; -export let bscExeName = "bsc.exe"; export let nodeModulesBinDir = path.join("node_modules", ".bin"); diff --git a/server/src/server.ts b/server/src/server.ts index e5f05c746..f093816a9 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -79,13 +79,11 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {}; // will be properly defined later depending on the mode (stdio/node-rpc) let send: (msg: p.Message) => void = (_) => {}; -let findBinaryFromProjectDirRec = ( - directory: p.DocumentUri, // This must be a directory and not a file! - targetDir: p.DocumentUri, - binaryName: p.DocumentUri +let findBinaryPathFromProjectRoot = ( + directory: p.DocumentUri // This must be a directory and not a file! ): null | p.DocumentUri => { - let binaryDirPath = path.join(directory, targetDir); - let binaryPath = path.join(binaryDirPath, binaryName); + let binaryDirPath = path.join(directory, c.nodeModulesBinDir); + let binaryPath = path.join(binaryDirPath, c.rescriptBinName); if (fs.existsSync(binaryPath)) { return binaryPath; @@ -97,22 +95,26 @@ let findBinaryFromProjectDirRec = ( return null; } - return findBinaryFromProjectDirRec(parentDir, targetDir, binaryName); + return findBinaryPathFromProjectRoot(parentDir); }; let findRescriptBinary = (projectRootPath: p.DocumentUri) => extensionConfiguration.binaryPath == null - ? findBinaryFromProjectDirRec( - projectRootPath, - c.nodeModulesBinDir, - c.rescriptBinName - ) + ? findBinaryPathFromProjectRoot(projectRootPath) : utils.findRescriptBinary(extensionConfiguration.binaryPath); -let findBscBinary = (projectRootPath: p.DocumentUri) => - extensionConfiguration.binaryPath == null - ? findBinaryFromProjectDirRec(projectRootPath, c.platformPath, c.bscExeName) - : utils.findBscBinary(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; From ccd51309a3dbebdf457907d86eb930374ebf609b Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Sun, 31 Jul 2022 16:50:44 +0200 Subject: [PATCH 3/3] Add changes according review --- server/src/server.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/src/server.ts b/server/src/server.ts index f093816a9..838641421 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -79,6 +79,8 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {}; // will be properly defined later depending on the mode (stdio/node-rpc) let send: (msg: p.Message) => void = (_) => {}; +// 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 => { @@ -321,7 +323,10 @@ let openedFile = (fileUri: string, fileContent: string) => { type: p.MessageType.Error, message: extensionConfiguration.binaryPath == null - ? "Can't find ReScript binary" + ? `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}`, }, };