-
Notifications
You must be signed in to change notification settings - Fork 60
Add @rescript/runtime when finding bsc arguments #1125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8c02a74
Add helper to find runtime
nojaf b108dfb
Bump used node version to 20
nojaf 718e986
Extract bsc arg collection for different systems. Apply RESCRIPT_RUNT…
nojaf 959c336
Add bsb remark
nojaf 9943ab3
Check both lock files
nojaf 821ce10
Add caching todo
nojaf c50424d
Add script to test finding runtime
nojaf b0ec8de
Cache runtime results
nojaf ff032c8
bsb already has the runtime argument in ninja file
nojaf c843ad2
Use rescript runtime from configuration if provided
nojaf 39b7ed2
Include version check before adding runtime
nojaf 3d1700f
Send message to lsp client when runtime was not found.
nojaf 44a91c8
Remove as any
nojaf 82b656a
Add changelog
nojaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
server/out | ||
analysis/examples | ||
analysis/reanalyze/examples | ||
tools/tests | ||
tools/tests | ||
.history/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,14 @@ | |
"default": null, | ||
"description": "Path to the directory where platform-specific ReScript binaries are. You can use it if you haven't or don't want to use the installed ReScript from node_modules in your project." | ||
}, | ||
"rescript.settings.runtimePath": { | ||
"type": [ | ||
"string", | ||
"null" | ||
], | ||
"default": null, | ||
"description": "Optional path to the directory containing the @rescript/runtime package. Set this if your tooling is unable to automatically locate the package in your project." | ||
}, | ||
"rescript.settings.compileStatus.enable": { | ||
"type": "boolean", | ||
"default": true, | ||
|
@@ -259,7 +267,7 @@ | |
"bundle": "npm run bundle-server && npm run bundle-client" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.14.41", | ||
"@types/node": "^20.19.13", | ||
"@types/semver": "^7.7.0", | ||
"@types/vscode": "1.68.0", | ||
"esbuild": "^0.20.1", | ||
|
@@ -268,5 +276,6 @@ | |
}, | ||
"dependencies": { | ||
"semver": "^7.7.2" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// benchmark | ||
const start = process.hrtime.bigint(); | ||
|
||
// start code | ||
const args = process.argv.slice(2); | ||
|
||
if (args.length === 0) { | ||
console.log(` | ||
Usage: node find-runtime.mjs <project-folder> | ||
Find @rescript/runtime directories in a project's node_modules. | ||
Arguments: | ||
project-folder Path to the project directory to search | ||
Examples: | ||
node find-runtime.mjs /path/to/project | ||
node find-runtime.mjs . | ||
`); | ||
process.exit(1); | ||
} | ||
|
||
const project = args[args.length - 1]; | ||
|
||
import { findRescriptRuntimesInProject } from "../server/src/find-runtime.ts"; | ||
|
||
const runtimes = await findRescriptRuntimesInProject(project); | ||
|
||
console.log("Found @rescript/runtime directories:", runtimes); | ||
|
||
// end code | ||
const end = process.hrtime.bigint(); | ||
const durationMs = Number(end - start) / 1e6; // convert ns → ms | ||
|
||
console.log(`Script took ${durationMs.toFixed(3)}ms`); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as path from "path"; | ||
import fs from "fs"; | ||
import { IncrementallyCompiledFileInfo } from "../incrementalCompilation"; | ||
import { buildNinjaPartialPath } from "../constants"; | ||
|
||
export type BsbCompilerArgs = string[]; | ||
|
||
export async function getBsbBscArgs( | ||
entry: IncrementallyCompiledFileInfo, | ||
): Promise<BsbCompilerArgs | null> { | ||
const buildNinjaPath = path.resolve( | ||
entry.project.rootPath, | ||
buildNinjaPartialPath, | ||
); | ||
|
||
let stat: fs.Stats; | ||
try { | ||
stat = await fs.promises.stat(buildNinjaPath); | ||
} catch { | ||
return null; | ||
} | ||
|
||
const cache = entry.buildNinja; | ||
if (cache && cache.fileMtime >= stat.mtimeMs) { | ||
return cache.rawExtracted; | ||
} | ||
|
||
const fh = await fs.promises.open(buildNinjaPath, "r"); | ||
try { | ||
let captureNext = false; | ||
let haveAst = false; | ||
const captured: string[] = []; | ||
|
||
for await (const rawLine of fh.readLines()) { | ||
const line = String(rawLine).trim(); | ||
if (captureNext) { | ||
captured.push(line); | ||
captureNext = false; | ||
if (haveAst && captured.length === 2) break; // got ast + mij | ||
} | ||
if (line.startsWith("rule astj")) { | ||
captureNext = true; | ||
haveAst = true; | ||
} else if (line.startsWith("rule mij")) { | ||
captureNext = true; | ||
} | ||
} | ||
|
||
if (captured.length !== 2) return null; | ||
|
||
entry.buildNinja = { | ||
fileMtime: stat.mtimeMs, | ||
rawExtracted: captured, | ||
}; | ||
return captured; | ||
} finally { | ||
await fh.close(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import * as path from "path"; | ||
import * as utils from "../utils"; | ||
import * as cp from "node:child_process"; | ||
import * as p from "vscode-languageserver-protocol"; | ||
import semver from "semver"; | ||
import { | ||
debug, | ||
IncrementallyCompiledFileInfo, | ||
} from "../incrementalCompilation"; | ||
import type { projectFiles } from "../projectFiles"; | ||
import config from "../config"; | ||
import { findRescriptRuntimesInProject } from "../find-runtime"; | ||
import { jsonrpcVersion } from "../constants"; | ||
|
||
export type RewatchCompilerArgs = { | ||
compiler_args: Array<string>; | ||
parser_args: Array<string>; | ||
}; | ||
|
||
async function getRuntimePath( | ||
entry: IncrementallyCompiledFileInfo, | ||
): Promise<string | null> { | ||
let rescriptRuntime: string | null = | ||
config.extensionConfiguration.runtimePath ?? null; | ||
|
||
if (rescriptRuntime !== null) { | ||
if (debug()) { | ||
console.log( | ||
`Using configured runtime path as RESCRIPT_RUNTIME: ${rescriptRuntime}`, | ||
); | ||
} | ||
return rescriptRuntime; | ||
} | ||
|
||
const rescriptRuntimes = await findRescriptRuntimesInProject( | ||
entry.project.workspaceRootPath, | ||
); | ||
|
||
if (debug()) { | ||
if (rescriptRuntimes.length === 0) { | ||
console.log( | ||
`Did not find @rescript/runtime directory for ${entry.project.workspaceRootPath}`, | ||
); | ||
} else if (rescriptRuntimes.length > 1) { | ||
console.warn( | ||
`Found multiple @rescript/runtime directories, using the first one as RESCRIPT_RUNTIME: ${rescriptRuntimes.join(", ")}`, | ||
); | ||
} else { | ||
console.log( | ||
`Found @rescript/runtime directory: ${rescriptRuntimes.join(", ")}`, | ||
); | ||
zth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return rescriptRuntimes.at(0) ?? null; | ||
} | ||
|
||
export async function getRewatchBscArgs( | ||
send: (msg: p.Message) => void, | ||
projectsFiles: Map<string, projectFiles>, | ||
entry: IncrementallyCompiledFileInfo, | ||
): Promise<RewatchCompilerArgs | null> { | ||
const rewatchCacheEntry = entry.buildRewatch; | ||
|
||
if ( | ||
rewatchCacheEntry != null && | ||
rewatchCacheEntry.lastFile === entry.file.sourceFilePath | ||
) { | ||
return Promise.resolve(rewatchCacheEntry.compilerArgs); | ||
} | ||
|
||
try { | ||
const project = projectsFiles.get(entry.project.rootPath); | ||
if (project?.rescriptVersion == null) return null; | ||
let rewatchPath = path.resolve( | ||
entry.project.workspaceRootPath, | ||
"node_modules/@rolandpeelen/rewatch/rewatch", | ||
); | ||
let rescriptRewatchPath = null; | ||
if ( | ||
semver.valid(project.rescriptVersion) && | ||
semver.satisfies(project.rescriptVersion as string, ">11", { | ||
includePrerelease: true, | ||
}) | ||
) { | ||
rescriptRewatchPath = await utils.findRewatchBinary( | ||
entry.project.workspaceRootPath, | ||
); | ||
} | ||
|
||
if ( | ||
semver.valid(project.rescriptVersion) && | ||
semver.satisfies(project.rescriptVersion as string, ">=12.0.0-beta.1", { | ||
includePrerelease: true, | ||
}) | ||
) { | ||
rescriptRewatchPath = await utils.findRescriptExeBinary( | ||
entry.project.workspaceRootPath, | ||
); | ||
} | ||
|
||
if (rescriptRewatchPath != null) { | ||
rewatchPath = rescriptRewatchPath; | ||
if (debug()) { | ||
console.log( | ||
`Found rewatch binary bundled with v12: ${rescriptRewatchPath}`, | ||
); | ||
} | ||
} else { | ||
if (debug()) { | ||
console.log("Did not find rewatch binary bundled with v12"); | ||
} | ||
} | ||
|
||
const rewatchArguments = semver.satisfies( | ||
project.rescriptVersion, | ||
">=12.0.0-beta.2", | ||
{ includePrerelease: true }, | ||
) | ||
? ["compiler-args", entry.file.sourceFilePath] | ||
: [ | ||
"--rescript-version", | ||
project.rescriptVersion, | ||
"--compiler-args", | ||
entry.file.sourceFilePath, | ||
]; | ||
const bscExe = await utils.findBscExeBinary( | ||
entry.project.workspaceRootPath, | ||
); | ||
const env: NodeJS.ProcessEnv = {}; | ||
if (bscExe != null) { | ||
env["RESCRIPT_BSC_EXE"] = bscExe; | ||
} | ||
|
||
// For ReScript >= 12.0.0-beta.11 we need to set RESCRIPT_RUNTIME | ||
if ( | ||
semver.satisfies(project.rescriptVersion, ">=12.0.0-beta.11", { | ||
includePrerelease: true, | ||
}) | ||
) { | ||
let rescriptRuntime: string | null = await getRuntimePath(entry); | ||
|
||
if (rescriptRuntime !== null) { | ||
env["RESCRIPT_RUNTIME"] = rescriptRuntime; | ||
} else { | ||
// If no runtime was found, we should let the user know. | ||
let params: p.ShowMessageParams = { | ||
type: p.MessageType.Error, | ||
message: | ||
`[Incremental type checking] The @rescript/runtime package was not found in your project. ` + | ||
`It is normally included with ReScript, but either it's missing or could not be detected. ` + | ||
`Check that it exists in your dependencies, or configure 'rescript.settings.runtimePath' to point to it. ` + | ||
`Without this package, incremental type checking may not work as expected.`, | ||
}; | ||
let message: p.NotificationMessage = { | ||
jsonrpc: jsonrpcVersion, | ||
method: "window/showMessage", | ||
params: params, | ||
}; | ||
send(message); | ||
} | ||
} | ||
|
||
const compilerArgs = JSON.parse( | ||
cp.execFileSync(rewatchPath, rewatchArguments, { env }).toString().trim(), | ||
) as RewatchCompilerArgs; | ||
|
||
entry.buildRewatch = { | ||
lastFile: entry.file.sourceFilePath, | ||
compilerArgs: compilerArgs, | ||
}; | ||
|
||
return compilerArgs; | ||
} catch (e) { | ||
console.error(e); | ||
return null; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.