|
| 1 | +import * as path from "path"; |
| 2 | +import * as utils from "../utils"; |
| 3 | +import * as cp from "node:child_process"; |
| 4 | +import * as p from "vscode-languageserver-protocol"; |
| 5 | +import semver from "semver"; |
| 6 | +import { |
| 7 | + debug, |
| 8 | + IncrementallyCompiledFileInfo, |
| 9 | +} from "../incrementalCompilation"; |
| 10 | +import type { projectFiles } from "../projectFiles"; |
| 11 | +import config from "../config"; |
| 12 | +import { findRescriptRuntimesInProject } from "../find-runtime"; |
| 13 | +import { jsonrpcVersion } from "../constants"; |
| 14 | + |
| 15 | +export type RewatchCompilerArgs = { |
| 16 | + compiler_args: Array<string>; |
| 17 | + parser_args: Array<string>; |
| 18 | +}; |
| 19 | + |
| 20 | +async function getRuntimePath( |
| 21 | + entry: IncrementallyCompiledFileInfo, |
| 22 | +): Promise<string | null> { |
| 23 | + let rescriptRuntime: string | null = |
| 24 | + config.extensionConfiguration.runtimePath ?? null; |
| 25 | + |
| 26 | + if (rescriptRuntime !== null) { |
| 27 | + if (debug()) { |
| 28 | + console.log( |
| 29 | + `Using configured runtime path as RESCRIPT_RUNTIME: ${rescriptRuntime}`, |
| 30 | + ); |
| 31 | + } |
| 32 | + return rescriptRuntime; |
| 33 | + } |
| 34 | + |
| 35 | + const rescriptRuntimes = await findRescriptRuntimesInProject( |
| 36 | + entry.project.workspaceRootPath, |
| 37 | + ); |
| 38 | + |
| 39 | + if (debug()) { |
| 40 | + if (rescriptRuntimes.length === 0) { |
| 41 | + console.log( |
| 42 | + `Did not find @rescript/runtime directory for ${entry.project.workspaceRootPath}`, |
| 43 | + ); |
| 44 | + } else if (rescriptRuntimes.length > 1) { |
| 45 | + console.warn( |
| 46 | + `Found multiple @rescript/runtime directories, using the first one as RESCRIPT_RUNTIME: ${rescriptRuntimes.join(", ")}`, |
| 47 | + ); |
| 48 | + } else { |
| 49 | + console.log( |
| 50 | + `Found @rescript/runtime directory: ${rescriptRuntimes.join(", ")}`, |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return rescriptRuntimes.at(0) ?? null; |
| 56 | +} |
| 57 | + |
| 58 | +export async function getRewatchBscArgs( |
| 59 | + send: (msg: p.Message) => void, |
| 60 | + projectsFiles: Map<string, projectFiles>, |
| 61 | + entry: IncrementallyCompiledFileInfo, |
| 62 | +): Promise<RewatchCompilerArgs | null> { |
| 63 | + const rewatchCacheEntry = entry.buildRewatch; |
| 64 | + |
| 65 | + if ( |
| 66 | + rewatchCacheEntry != null && |
| 67 | + rewatchCacheEntry.lastFile === entry.file.sourceFilePath |
| 68 | + ) { |
| 69 | + return Promise.resolve(rewatchCacheEntry.compilerArgs); |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + const project = projectsFiles.get(entry.project.rootPath); |
| 74 | + if (project?.rescriptVersion == null) return null; |
| 75 | + let rewatchPath = path.resolve( |
| 76 | + entry.project.workspaceRootPath, |
| 77 | + "node_modules/@rolandpeelen/rewatch/rewatch", |
| 78 | + ); |
| 79 | + let rescriptRewatchPath = null; |
| 80 | + if ( |
| 81 | + semver.valid(project.rescriptVersion) && |
| 82 | + semver.satisfies(project.rescriptVersion as string, ">11", { |
| 83 | + includePrerelease: true, |
| 84 | + }) |
| 85 | + ) { |
| 86 | + rescriptRewatchPath = await utils.findRewatchBinary( |
| 87 | + entry.project.workspaceRootPath, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + if ( |
| 92 | + semver.valid(project.rescriptVersion) && |
| 93 | + semver.satisfies(project.rescriptVersion as string, ">=12.0.0-beta.1", { |
| 94 | + includePrerelease: true, |
| 95 | + }) |
| 96 | + ) { |
| 97 | + rescriptRewatchPath = await utils.findRescriptExeBinary( |
| 98 | + entry.project.workspaceRootPath, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if (rescriptRewatchPath != null) { |
| 103 | + rewatchPath = rescriptRewatchPath; |
| 104 | + if (debug()) { |
| 105 | + console.log( |
| 106 | + `Found rewatch binary bundled with v12: ${rescriptRewatchPath}`, |
| 107 | + ); |
| 108 | + } |
| 109 | + } else { |
| 110 | + if (debug()) { |
| 111 | + console.log("Did not find rewatch binary bundled with v12"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + const rewatchArguments = semver.satisfies( |
| 116 | + project.rescriptVersion, |
| 117 | + ">=12.0.0-beta.2", |
| 118 | + { includePrerelease: true }, |
| 119 | + ) |
| 120 | + ? ["compiler-args", entry.file.sourceFilePath] |
| 121 | + : [ |
| 122 | + "--rescript-version", |
| 123 | + project.rescriptVersion, |
| 124 | + "--compiler-args", |
| 125 | + entry.file.sourceFilePath, |
| 126 | + ]; |
| 127 | + const bscExe = await utils.findBscExeBinary( |
| 128 | + entry.project.workspaceRootPath, |
| 129 | + ); |
| 130 | + const env: NodeJS.ProcessEnv = {}; |
| 131 | + if (bscExe != null) { |
| 132 | + env["RESCRIPT_BSC_EXE"] = bscExe; |
| 133 | + } |
| 134 | + |
| 135 | + // For ReScript >= 12.0.0-beta.11 we need to set RESCRIPT_RUNTIME |
| 136 | + if ( |
| 137 | + semver.satisfies(project.rescriptVersion, ">=12.0.0-beta.11", { |
| 138 | + includePrerelease: true, |
| 139 | + }) |
| 140 | + ) { |
| 141 | + let rescriptRuntime: string | null = await getRuntimePath(entry); |
| 142 | + |
| 143 | + if (rescriptRuntime !== null) { |
| 144 | + env["RESCRIPT_RUNTIME"] = rescriptRuntime; |
| 145 | + } else { |
| 146 | + // If no runtime was found, we should let the user know. |
| 147 | + let params: p.ShowMessageParams = { |
| 148 | + type: p.MessageType.Error, |
| 149 | + message: |
| 150 | + `[Incremental type checking] The @rescript/runtime package was not found in your project. ` + |
| 151 | + `It is normally included with ReScript, but either it's missing or could not be detected. ` + |
| 152 | + `Check that it exists in your dependencies, or configure 'rescript.settings.runtimePath' to point to it. ` + |
| 153 | + `Without this package, incremental type checking may not work as expected.`, |
| 154 | + }; |
| 155 | + let message: p.NotificationMessage = { |
| 156 | + jsonrpc: jsonrpcVersion, |
| 157 | + method: "window/showMessage", |
| 158 | + params: params, |
| 159 | + }; |
| 160 | + send(message); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + const compilerArgs = JSON.parse( |
| 165 | + cp.execFileSync(rewatchPath, rewatchArguments, { env }).toString().trim(), |
| 166 | + ) as RewatchCompilerArgs; |
| 167 | + |
| 168 | + entry.buildRewatch = { |
| 169 | + lastFile: entry.file.sourceFilePath, |
| 170 | + compilerArgs: compilerArgs, |
| 171 | + }; |
| 172 | + |
| 173 | + return compilerArgs; |
| 174 | + } catch (e) { |
| 175 | + console.error(e); |
| 176 | + return null; |
| 177 | + } |
| 178 | +} |
0 commit comments