From e2169cfcd57482c3642ce3f21f36167f8a3a352a Mon Sep 17 00:00:00 2001 From: Cheng Lou Date: Tue, 27 Apr 2021 08:41:03 -0700 Subject: [PATCH] Use `rescript` to build when available Fixes #160 --- server/src/constants.ts | 9 +++++++-- server/src/server.ts | 12 +++++------ server/src/utils.ts | 45 +++++++++++++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/server/src/constants.ts b/server/src/constants.ts index 62875a4e6..a40905db7 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -29,9 +29,14 @@ export let analysisProdPath = path.join( "rescript-editor-analysis.exe" ); -// can't use the native bsb since we might need the watcher -w flag, which is only in the js wrapper -// export let bsbPartialPath = path.join('node_modules', 'bs-platform', process.platform, 'bsb.exe'); +// can't use the native bsb/rescript since we might need the watcher -w flag, which is only in the JS wrapper +export let rescriptNodePartialPath = path.join( + "node_modules", + ".bin", + "rescript" +); export let bsbNodePartialPath = path.join("node_modules", ".bin", "bsb"); + export let bsbLock = ".bsb.lock"; export let bsconfigPartialPath = "bsconfig.json"; export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log"); diff --git a/server/src/server.ts b/server/src/server.ts index 8a9256900..b27501077 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -150,11 +150,10 @@ let openedFile = (fileUri: string, fileContent: string) => { // because otherwise the diagnostics info we'll display might be stale let bsbLockPath = path.join(projectRootPath, c.bsbLock); if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) { - let bsbPath = path.join(projectRootPath, c.bsbNodePartialPath); // TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is // stale. Use that logic // TODO: close watcher when lang-server shuts down - if (fs.existsSync(bsbPath)) { + if (utils.findNodeBuildOfProjectRoot(projectRootPath) != null) { let payload: clientSentBuildAction = { title: c.startBuildAction, projectRootPath: projectRootPath, @@ -540,14 +539,15 @@ function onMessage(msg: m.Message) { ) { let msg_ = msg.result as clientSentBuildAction; let projectRootPath = msg_.projectRootPath; - let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath); // TODO: sometime stale .bsb.lock dangling // TODO: close watcher when lang-server shuts down. However, by Node's // default, these subprocesses are automatically killed when this // language-server process exits - if (fs.existsSync(bsbNodePath)) { - let bsbProcess = utils.runBsbWatcherUsingValidBsbNodePath( - bsbNodePath, + let found = utils.findNodeBuildOfProjectRoot(projectRootPath); + if (found != null) { + let bsbProcess = utils.runBuildWatcherUsingValidBuildPath( + found.buildPath, + found.isReScript, projectRootPath ); let root = projectsFiles.get(projectRootPath)!; diff --git a/server/src/utils.ts b/server/src/utils.ts index dca66941f..07f6a9320 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -54,17 +54,32 @@ export let findBscNativeOfFile = ( let bscNativePath = path.join(dir, c.bscNativePartialPath); if (fs.existsSync(bscNativeReScriptPath)) { - return bscNativeReScriptPath + return bscNativeReScriptPath; } else if (fs.existsSync(bscNativePath)) { - return bscNativePath + return bscNativePath; } else if (dir === source) { // reached the top - return null + return null; } else { return findBscNativeOfFile(dir); } }; +// TODO: this doesn't handle file:/// scheme +export let findNodeBuildOfProjectRoot = ( + projectRootPath: p.DocumentUri +): null | { buildPath: p.DocumentUri; isReScript: boolean } => { + let rescriptNodePath = path.join(projectRootPath, c.rescriptNodePartialPath); + let bsbNodePath = path.join(projectRootPath, c.bsbNodePartialPath); + + if (fs.existsSync(rescriptNodePath)) { + return { buildPath: rescriptNodePath, isReScript: true }; + } else if (fs.existsSync(bsbNodePath)) { + return { buildPath: bsbNodePath, isReScript: false }; + } + return null; +}; + type execResult = | { kind: "success"; @@ -129,10 +144,14 @@ export let runAnalysisAfterSanityCheck = ( return JSON.parse(stdout.toString()); }; -export let runBsbWatcherUsingValidBsbNodePath = ( - bsbNodePath: p.DocumentUri, +export let runBuildWatcherUsingValidBuildPath = ( + buildPath: p.DocumentUri, + isRescript: boolean, projectRootPath: p.DocumentUri ) => { + let cwdEnv = { + cwd: projectRootPath, + }; if (process.platform === "win32") { /* - a node.js script in node_modules/.bin on windows is wrapped in a @@ -146,13 +165,17 @@ export let runBsbWatcherUsingValidBsbNodePath = ( (since the path might have spaces), which `execFile` would have done for you under the hood */ - return childProcess.exec(`"${bsbNodePath}".cmd -w`, { - cwd: projectRootPath, - }); + if (isRescript) { + return childProcess.exec(`"${buildPath}".cmd build -w`, cwdEnv); + } else { + return childProcess.exec(`"${buildPath}".cmd -w`, cwdEnv); + } } else { - return childProcess.execFile(bsbNodePath, ["-w"], { - cwd: projectRootPath, - }); + if (isRescript) { + return childProcess.execFile(buildPath, ["build", "-w"], cwdEnv); + } else { + return childProcess.execFile(buildPath, ["-w"], cwdEnv); + } } };