diff --git a/server/src/incrementalCompilation.ts b/server/src/incrementalCompilation.ts index 285b6a5b5..3684f9d92 100644 --- a/server/src/incrementalCompilation.ts +++ b/server/src/incrementalCompilation.ts @@ -10,6 +10,7 @@ import config, { send } from "./config"; import * as c from "./constants"; import * as chokidar from "chokidar"; import { fileCodeActions } from "./codeActions"; +import { projectsFiles } from "./projectFiles"; function debug() { return ( @@ -75,8 +76,6 @@ type IncrementallyCompiledFileInfo = { callArgs: Promise | null>; /** The location of the incremental folder for this project. */ incrementalFolderPath: string; - /** The ReScript version. */ - rescriptVersion: string; }; /** Any code actions for this incremental file. */ codeActions: Array; @@ -284,6 +283,8 @@ function getBscArgs( }); } else if (buildSystem === "rewatch") { try { + const project = projectsFiles.get(entry.project.rootPath); + if (project?.rescriptVersion == null) return; let rewatchPath = path.resolve( entry.project.workspaceRootPath, "node_modules/@rolandpeelen/rewatch/rewatch" @@ -292,7 +293,7 @@ function getBscArgs( cp .execFileSync(rewatchPath, [ "--rescript-version", - entry.project.rescriptVersion, + project.rescriptVersion, "--compiler-args", entry.file.sourceFilePath, ]) @@ -364,21 +365,21 @@ function triggerIncrementalCompilationOfFile( if (incrementalFileCacheEntry == null) { // New file const projectRootPath = utils.findProjectRootOfFile(filePath); - const workspaceRootPath = projectRootPath - ? utils.findProjectRootOfFile(projectRootPath) - : null; if (projectRootPath == null) { if (debug()) console.log("Did not find project root path for " + filePath); return; } - const namespaceName = utils.getNamespaceNameFromConfigFile(projectRootPath); - if (namespaceName.kind === "error") { - if (debug()) - console.log("Getting namespace config errored for " + filePath); + const project = projectsFiles.get(projectRootPath); + if (project == null) { + if (debug()) console.log("Did not find open project for " + filePath); return; } - const bscBinaryLocation = utils.findBscExeBinary(projectRootPath); + const workspaceRootPath = projectRootPath + ? utils.findProjectRootOfFile(projectRootPath) + : null; + + const bscBinaryLocation = project.bscBinaryLocation; if (bscBinaryLocation == null) { if (debug()) console.log("Could not find bsc binary location for " + filePath); @@ -387,8 +388,8 @@ function triggerIncrementalCompilationOfFile( const ext = filePath.endsWith(".resi") ? ".resi" : ".res"; const moduleName = path.basename(filePath, ext); const moduleNameNamespaced = - namespaceName.result !== "" - ? `${moduleName}-${namespaceName.result}` + project.namespaceName != null + ? `${moduleName}-${project.namespaceName}` : moduleName; const incrementalFolderPath = path.join( @@ -396,19 +397,6 @@ function triggerIncrementalCompilationOfFile( INCREMENTAL_FILE_FOLDER_LOCATION ); - let rescriptVersion = ""; - try { - rescriptVersion = cp - .execFileSync(bscBinaryLocation, ["-version"]) - .toString() - .trim(); - } catch (e) { - console.error(e); - } - if (rescriptVersion.startsWith("ReScript ")) { - rescriptVersion = rescriptVersion.replace("ReScript ", ""); - } - let originalTypeFileLocation = path.resolve( projectRootPath, "lib/bs", @@ -436,7 +424,6 @@ function triggerIncrementalCompilationOfFile( callArgs: Promise.resolve([]), bscBinaryLocation, incrementalFolderPath, - rescriptVersion, }, buildRewatch: null, buildNinja: null, @@ -488,6 +475,16 @@ function verifyTriggerToken(filePath: string, triggerToken: number): boolean { ); } async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) { + const project = projectsFiles.get(entry.project.rootPath); + if (project?.rescriptVersion == null) { + if (debug()) { + console.log( + "Found no project (or ReScript version) for " + + entry.file.sourceFilePath + ); + } + return null; + } const res = await getBscArgs(entry); if (res == null) return null; let astArgs: Array> = []; @@ -547,7 +544,7 @@ async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) { }); callArgs.push("-color", "never"); - if (parseInt(entry.project.rescriptVersion.split(".")[0] ?? "10") >= 11) { + if (parseInt(project.rescriptVersion.split(".")[0] ?? "10") >= 11) { // Only available in v11+ callArgs.push("-ignore-parse-errors"); } diff --git a/server/src/projectFiles.ts b/server/src/projectFiles.ts index a4cdcf913..5520a479b 100644 --- a/server/src/projectFiles.ts +++ b/server/src/projectFiles.ts @@ -10,6 +10,8 @@ interface projectFiles { filesWithDiagnostics: Set; filesDiagnostics: filesDiagnostics; rescriptVersion: string | undefined; + bscBinaryLocation: string | null; + namespaceName: string | null; bsbWatcherByEditor: null | cp.ChildProcess; diff --git a/server/src/server.ts b/server/src/server.ts index 6949a1c05..9a925a695 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -258,12 +258,18 @@ let openedFile = (fileUri: string, fileContent: string) => { if (config.extensionConfiguration.incrementalTypechecking?.enabled) { ic.recreateIncrementalFileFolder(projectRootPath); } + const namespaceName = + utils.getNamespaceNameFromConfigFile(projectRootPath); + projectRootState = { openFiles: new Set(), filesWithDiagnostics: new Set(), filesDiagnostics: {}, + namespaceName: + namespaceName.kind === "success" ? namespaceName.result : null, rescriptVersion: utils.findReScriptVersion(projectRootPath), bsbWatcherByEditor: null, + bscBinaryLocation: utils.findBscExeBinary(projectRootPath), hasPromptedToStartBuild: /(\/|\\)node_modules(\/|\\)/.test( projectRootPath ) @@ -795,7 +801,9 @@ function format(msg: p.RequestMessage): Array { let code = getOpenedFileContent(params.textDocument.uri); let projectRootPath = utils.findProjectRootOfFile(filePath); - let bscExeBinaryPath = utils.findBscExeBinary(projectRootPath); + let project = + projectRootPath != null ? projectsFiles.get(projectRootPath) : null; + let bscExeBinaryPath = project?.bscBinaryLocation ?? null; let formattedResult = utils.formatCode( bscExeBinaryPath, diff --git a/server/src/utils.ts b/server/src/utils.ts index 3cfb45440..d8df66cd8 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -58,15 +58,19 @@ export let findProjectRootOfFile = ( if ( foundRootFromProjectFiles == null || rootPath.length > foundRootFromProjectFiles.length - ) + ) { foundRootFromProjectFiles = rootPath; + } } } if (foundRootFromProjectFiles != null) { return foundRootFromProjectFiles; } else { - return findProjectRootOfFileInDir(source); + const isDir = path.extname(source) === ""; + return findProjectRootOfFileInDir( + isDir ? path.join(source, "dummy.res") : source + ); } };