Skip to content

Remove chokidar #1096

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 3 commits into from
May 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@
#### :house: Internal

- Find binary paths asynchronously. On `>=12.0.0-alpha.13` we do this by dynamically importing the `@rescript/{target}` package in the project root. https://github.com/rescript-lang/rescript-vscode/pull/1093
- Remove chokidar from LSP server. We expect LSP clients to support [workspace_didChangeWatchedFiles](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_didChangeWatchedFiles). https://github.com/rescript-lang/rescript-vscode/pull/1096

## 1.62.0

577 changes: 1 addition & 576 deletions package-lock.json

Large diffs are not rendered by default.

343 changes: 35 additions & 308 deletions server/package-lock.json
3 changes: 1 addition & 2 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -30,10 +30,9 @@
"url": "https://github.com/rescript-lang/rescript-vscode/issues"
},
"dependencies": {
"chokidar": "^3.5.1",
"semver": "^7.7.2",
"vscode-jsonrpc": "^8.0.1",
"vscode-languageserver": "^8.0.1",
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-protocol": "^3.17.1"
}
}
53 changes: 19 additions & 34 deletions server/src/incrementalCompilation.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import * as cp from "node:child_process";
import semver from "semver";
import config, { send } from "./config";
import * as c from "./constants";
import * as chokidar from "chokidar";
import { fileCodeActions } from "./codeActions";
import { projectsFiles } from "./projectFiles";

@@ -92,38 +91,29 @@ const incrementallyCompiledFileInfo: Map<
const hasReportedFeatureFailedError: Set<string> = new Set();
const originalTypeFileToFilePath: Map<string, string> = new Map();

let incrementalFilesWatcher = chokidar
.watch([], {
awaitWriteFinish: {
stabilityThreshold: 1,
},
})
.on("all", (e, changedPath) => {
if (e !== "change" && e !== "unlink") return;
const filePath = originalTypeFileToFilePath.get(changedPath);
if (filePath != null) {
const entry = incrementallyCompiledFileInfo.get(filePath);
if (entry != null) {
export function incrementalCompilationFileChanged(changedPath: string) {
const filePath = originalTypeFileToFilePath.get(changedPath);
if (filePath != null) {
const entry = incrementallyCompiledFileInfo.get(filePath);
if (entry != null) {
if (debug()) {
console.log("[watcher] Cleaning up incremental files for " + filePath);
}
if (entry.compilation != null) {
if (debug()) {
console.log(
"[watcher] Cleaning up incremental files for " + filePath
);
}
if (entry.compilation != null) {
if (debug()) {
console.log("[watcher] Was compiling, killing");
}
clearTimeout(entry.compilation.timeout);
entry.killCompilationListeners.forEach((cb) => cb());
entry.compilation = null;
console.log("[watcher] Was compiling, killing");
}
cleanUpIncrementalFiles(
entry.file.sourceFilePath,
entry.project.rootPath
);
clearTimeout(entry.compilation.timeout);
entry.killCompilationListeners.forEach((cb) => cb());
entry.compilation = null;
}
cleanUpIncrementalFiles(
entry.file.sourceFilePath,
entry.project.rootPath
);
}
});
}
}

export function removeIncrementalFileFolder(
projectRootPath: string,
@@ -465,10 +455,6 @@ function triggerIncrementalCompilationOfFile(
incrementalFileCacheEntry.project.callArgs = figureOutBscArgs(
incrementalFileCacheEntry
);
// Set up watcher for relevant cmt/cmti
incrementalFilesWatcher.add([
incrementalFileCacheEntry.file.originalTypeFileLocation,
]);
originalTypeFileToFilePath.set(
incrementalFileCacheEntry.file.originalTypeFileLocation,
incrementalFileCacheEntry.file.sourceFilePath
@@ -781,7 +767,6 @@ export function handleClosedFile(filePath: string) {
cleanUpIncrementalFiles(filePath, entry.project.rootPath);
incrementallyCompiledFileInfo.delete(filePath);
originalTypeFileToFilePath.delete(entry.file.originalTypeFileLocation);
incrementalFilesWatcher.unwatch([entry.file.originalTypeFileLocation]);
}

export function getCodeActionsFromIncrementalCompilation(
98 changes: 67 additions & 31 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import * as v from "vscode-languageserver";
import * as rpc from "vscode-jsonrpc/node";
import * as path from "path";
import fs from "fs";
// TODO: check DidChangeWatchedFilesNotification.
import {
DidChangeWatchedFilesNotification,
DidOpenTextDocumentNotification,
DidChangeTextDocumentNotification,
DidCloseTextDocumentNotification,
@@ -14,12 +14,12 @@ import {
InlayHintParams,
CodeLensParams,
SignatureHelpParams,
InitializedNotification,
} from "vscode-languageserver-protocol";
import * as lookup from "./lookup";
import * as utils from "./utils";
import * as codeActions from "./codeActions";
import * as c from "./constants";
import * as chokidar from "chokidar";
import { assert } from "console";
import { fileURLToPath } from "url";
import { WorkspaceEdit } from "vscode-languageserver";
@@ -28,6 +28,10 @@ import * as ic from "./incrementalCompilation";
import config, { extensionConfiguration } from "./config";
import { projectsFiles } from "./projectFiles";

// Absolute paths to all the workspace folders
// Configured during the initialize request
const workspaceFolders = new Set<string>();

// This holds client capabilities specific to our extension, and not necessarily
// related to the LS protocol. It's for enabling/disabling features that might
// work in one client, like VSCode, but perhaps not in others, like vim.
@@ -210,21 +214,18 @@ let deleteProjectConfigCache = async (rootPath: string) => {
}
};

let compilerLogsWatcher = chokidar
.watch([], {
awaitWriteFinish: {
stabilityThreshold: 1,
},
})
.on("all", async (_e, changedPath) => {
if (changedPath.includes("build.ninja")) {
async function onWorkspaceDidChangeWatchedFiles(
params: p.DidChangeWatchedFilesParams
) {
await Promise.all(params.changes.map(async (change) => {
if (change.uri.includes("build.ninja")) {
if (config.extensionConfiguration.cache?.projectConfig?.enable === true) {
let projectRoot = utils.findProjectRootOfFile(changedPath);
let projectRoot = utils.findProjectRootOfFile(change.uri);
if (projectRoot != null) {
await syncProjectConfigCache(projectRoot);
}
}
} else {
} else if (change.uri.includes("compiler.log")) {
try {
await sendUpdatedDiagnostics();
sendCompilationFinishedMessage();
@@ -237,12 +238,11 @@ let compilerLogsWatcher = chokidar
} catch {
console.log("Error while sending updated diagnostics");
}
} else {
ic.incrementalCompilationFileChanged(fileURLToPath(change.uri));
}
});
let stopWatchingCompilerLog = () => {
// TODO: cleanup of compilerLogs?
compilerLogsWatcher.close();
};
}));
}

type clientSentBuildAction = {
title: string;
@@ -280,13 +280,7 @@ let openedFile = async (fileUri: string, fileContent: string) => {
: false,
};
projectsFiles.set(projectRootPath, projectRootState);
compilerLogsWatcher.add(
path.join(projectRootPath, c.compilerLogPartialPath)
);
if (config.extensionConfiguration.cache?.projectConfig?.enable === true) {
compilerLogsWatcher.add(
path.join(projectRootPath, c.buildNinjaPartialPath)
);
await syncProjectConfigCache(projectRootPath);
}
}
@@ -364,12 +358,6 @@ let closedFile = async (fileUri: string) => {
root.openFiles.delete(filePath);
// clear diagnostics too if no open files open in said project
if (root.openFiles.size === 0) {
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.compilerLogPartialPath)
);
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.buildNinjaPartialPath)
);
await deleteProjectConfigCache(projectRootPath);
deleteProjectDiagnostics(projectRootPath);
if (root.bsbWatcherByEditor !== null) {
@@ -1051,6 +1039,52 @@ async function onMessage(msg: p.Message) {
} else {
process.exit(1);
}
} else if (msg.method === InitializedNotification.method) {
/*
The initialized notification is sent from the client to the server after the client received the result of the initialize request
but before the client is sending any other request or notification to the server.
The server can use the initialized notification, for example, to dynamically register capabilities.
We use this to register the file watchers for the project.
The client can watch files for us and send us events via the `workspace/didChangeWatchedFiles`
*/
const watchers = Array.from(workspaceFolders).flatMap(
(projectRootPath) => [
{
globPattern: path.join(projectRootPath, c.compilerLogPartialPath),
kind: p.WatchKind.Change | p.WatchKind.Create | p.WatchKind.Delete,
},
{
globPattern: path.join(projectRootPath, c.buildNinjaPartialPath),
kind: p.WatchKind.Change | p.WatchKind.Create | p.WatchKind.Delete,
},
{
globPattern: `${path.join(projectRootPath, c.compilerDirPartialPath)}/**/*.{cmt,cmi}`,
kind: p.WatchKind.Change | p.WatchKind.Delete,
}
]
);
const registrationParams: p.RegistrationParams = {
registrations: [
{
id: "rescript_file_watcher",
method: DidChangeWatchedFilesNotification.method,
registerOptions: {
watchers,
},
},
],
};
const req: p.RequestMessage = {
jsonrpc: c.jsonrpcVersion,
id: serverSentRequestIdCounter++,
method: p.RegistrationRequest.method,
params: registrationParams,
};
send(req);
} else if (msg.method === DidChangeWatchedFilesNotification.method) {
const params = msg.params as p.DidChangeWatchedFilesParams;
await onWorkspaceDidChangeWatchedFiles(params);
} else if (msg.method === DidOpenTextDocumentNotification.method) {
let params = msg.params as p.DidOpenTextDocumentParams;
await openedFile(params.textDocument.uri, params.textDocument.text);
@@ -1096,6 +1130,10 @@ async function onMessage(msg: p.Message) {
} else if (msg.method === "initialize") {
// Save initial configuration, if present
let initParams = msg.params as InitializeParams;
for (const workspaceFolder of initParams.workspaceFolders || []) {
const workspaceRootPath = fileURLToPath(workspaceFolder.uri);
workspaceFolders.add(workspaceRootPath);
}
let initialConfiguration = initParams.initializationOptions
?.extensionConfiguration as extensionConfiguration | undefined;

@@ -1207,8 +1245,6 @@ async function onMessage(msg: p.Message) {
} else {
shutdownRequestAlreadyReceived = true;
// TODO: recheck logic around init/shutdown...
stopWatchingCompilerLog();
// TODO: delete bsb watchers

if (pullConfigurationPeriodically != null) {
clearInterval(pullConfigurationPeriodically);