Skip to content

Commit 51aec1c

Browse files
committed
e2e for the new cache mode
1 parent bec610d commit 51aec1c

File tree

9 files changed

+90
-14
lines changed

9 files changed

+90
-14
lines changed

analysis/bin/main.ml

+13-1
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,22 @@ let main () =
111111
in
112112
match args with
113113
| [_; "cache-project"; rootPath] -> (
114+
Cfg.useProjectConfigCache := false;
114115
let uri = Uri.fromPath rootPath in
115116
match Packages.getPackage ~uri with
116117
| Some package -> Cache.cacheProject package
117-
| None -> ())
118+
| None -> print_endline "\"ERR\"")
119+
| [_; "cache-delete"; rootPath] -> (
120+
Cfg.useProjectConfigCache := false;
121+
let uri = Uri.fromPath rootPath in
122+
match Packages.findRoot ~uri (Hashtbl.create 0) with
123+
| Some (`Bs rootPath) -> (
124+
match BuildSystem.getLibBs rootPath with
125+
| None -> print_endline "\"ERR\""
126+
| Some libBs ->
127+
Cache.deleteCache (Cache.targetFileFromLibBs libBs);
128+
print_endline "\"OK\"")
129+
| _ -> print_endline "\"ERR: Did not find root \"")
118130
| [_; "completion"; path; line; col; currentFile] ->
119131
printHeaderInfo path line col;
120132
Commands.completion ~debug ~path

analysis/src/Cache.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ let readCache filename =
2121
with _ -> None
2222
else None
2323

24+
let deleteCache filename = try Sys.remove filename with _ -> ()
25+
2426
let targetFileFromLibBs libBs = Filename.concat libBs ".project-files-cache"
2527

2628
let cacheProject (package : package) =
@@ -32,8 +34,8 @@ let cacheProject (package : package) =
3234
}
3335
in
3436
match BuildSystem.getLibBs package.rootPath with
35-
| None -> ()
37+
| None -> print_endline "\"ERR\""
3638
| Some libBs ->
3739
let targetFile = targetFileFromLibBs libBs in
3840
writeCache targetFile cached;
39-
print_endline "OK"
41+
print_endline "\"OK\""

analysis/src/Packages.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ let findRoot ~uri packagesByRoot =
231231
let parent = Filename.dirname path in
232232
if parent = path then (* reached root *) None else loop parent
233233
in
234-
loop (Filename.dirname path)
234+
loop (if Sys.is_directory path then path else Filename.dirname path)
235235

236236
let getPackage ~uri =
237237
let open SharedTypes in

client/src/extension.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ export function activate(context: ExtensionContext) {
341341
affectsConfiguration("rescript.settings.inlayHints") ||
342342
affectsConfiguration("rescript.settings.codeLens") ||
343343
affectsConfiguration("rescript.settings.signatureHelp") ||
344-
affectsConfiguration("rescript.settings.incrementalTypechecking")
344+
affectsConfiguration("rescript.settings.incrementalTypechecking") ||
345+
affectsConfiguration("rescript.settings.cache")
345346
) {
346347
commands.executeCommand("rescript-vscode.restart_language_server");
347348
} else {

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@
192192
"default": false,
193193
"description": "(debug) Enable debug logging (ends up in the extension output)."
194194
},
195+
"rescript.settings.cache.projectConfig.enabled": {
196+
"type": "boolean",
197+
"default": false,
198+
"description": "(beta/experimental) Enable project config caching. Can speed up latency dramatically."
199+
},
195200
"rescript.settings.binaryPath": {
196201
"type": [
197202
"string",

server/src/config.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export interface extensionConfiguration {
2121
acrossFiles?: boolean;
2222
debugLogging?: boolean;
2323
};
24+
cache?: {
25+
projectConfig?: {
26+
enabled?: boolean;
27+
};
28+
};
2429
}
2530

2631
// All values here are temporary, and will be overridden as the server is
@@ -43,7 +48,12 @@ let config: { extensionConfiguration: extensionConfiguration } = {
4348
incrementalTypechecking: {
4449
enabled: false,
4550
acrossFiles: false,
46-
debugLogging: true,
51+
debugLogging: false,
52+
},
53+
cache: {
54+
projectConfig: {
55+
enabled: false,
56+
},
4757
},
4858
},
4959
};

server/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export let bsconfigPartialPath = "bsconfig.json";
4444
export let rescriptJsonPartialPath = "rescript.json";
4545
export let compilerDirPartialPath = path.join("lib", "bs");
4646
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");
47+
export let buildNinjaPartialPath = path.join("lib", "bs", "build.ninja");
4748
export let resExt = ".res";
4849
export let resiExt = ".resi";
4950
export let cmiExt = ".cmi";

server/src/server.ts

+49-8
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,49 @@ let sendCompilationFinishedMessage = () => {
205205
send(notification);
206206
};
207207

208+
let debug = false;
209+
210+
let syncProjectConfigCache = (rootPath: string) => {
211+
try {
212+
if (debug) console.log("syncing project config cache for " + rootPath);
213+
utils.runAnalysisAfterSanityCheck(rootPath, ["cache-project", rootPath]);
214+
if (debug) console.log("OK - synced project config cache for " + rootPath);
215+
} catch (e) {
216+
if (debug) console.error(e);
217+
}
218+
};
219+
220+
let deleteProjectConfigCache = (rootPath: string) => {
221+
try {
222+
if (debug) console.log("deleting project config cache for " + rootPath);
223+
utils.runAnalysisAfterSanityCheck(rootPath, ["cache-delete", rootPath]);
224+
if (debug) console.log("OK - deleted project config cache for " + rootPath);
225+
} catch (e) {
226+
if (debug) console.error(e);
227+
}
228+
};
229+
208230
let compilerLogsWatcher = chokidar
209231
.watch([], {
210232
awaitWriteFinish: {
211233
stabilityThreshold: 1,
212234
},
213235
})
214-
.on("all", (_e, _changedPath) => {
215-
sendUpdatedDiagnostics();
216-
sendCompilationFinishedMessage();
217-
if (config.extensionConfiguration.inlayHints?.enable === true) {
218-
sendInlayHintsRefresh();
219-
}
220-
if (config.extensionConfiguration.codeLens === true) {
221-
sendCodeLensRefresh();
236+
.on("all", (_e, changedPath) => {
237+
if (changedPath.includes("build.ninja")) {
238+
let projectRoot = utils.findProjectRootOfFile(changedPath);
239+
if (projectRoot != null) {
240+
syncProjectConfigCache(projectRoot);
241+
}
242+
} else {
243+
sendUpdatedDiagnostics();
244+
sendCompilationFinishedMessage();
245+
if (config.extensionConfiguration.inlayHints?.enable === true) {
246+
sendInlayHintsRefresh();
247+
}
248+
if (config.extensionConfiguration.codeLens === true) {
249+
sendCodeLensRefresh();
250+
}
222251
}
223252
});
224253
let stopWatchingCompilerLog = () => {
@@ -257,6 +286,14 @@ let openedFile = (fileUri: string, fileContent: string) => {
257286
compilerLogsWatcher.add(
258287
path.join(projectRootPath, c.compilerLogPartialPath)
259288
);
289+
if (
290+
config.extensionConfiguration.cache?.projectConfig?.enabled === true
291+
) {
292+
compilerLogsWatcher.add(
293+
path.join(projectRootPath, c.buildNinjaPartialPath)
294+
);
295+
syncProjectConfigCache(projectRootPath);
296+
}
260297
}
261298
let root = projectsFiles.get(projectRootPath)!;
262299
root.openFiles.add(filePath);
@@ -335,6 +372,10 @@ let closedFile = (fileUri: string) => {
335372
compilerLogsWatcher.unwatch(
336373
path.join(projectRootPath, c.compilerLogPartialPath)
337374
);
375+
compilerLogsWatcher.unwatch(
376+
path.join(projectRootPath, c.buildNinjaPartialPath)
377+
);
378+
deleteProjectConfigCache(projectRootPath);
338379
deleteProjectDiagnostics(projectRootPath);
339380
if (root.bsbWatcherByEditor !== null) {
340381
root.bsbWatcherByEditor.kill();

server/src/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ export let runAnalysisAfterSanityCheck = (
190190
config.extensionConfiguration.incrementalTypechecking?.enabled === true
191191
? "true"
192192
: undefined,
193+
RESCRIPT_PROJECT_CONFIG_CACHE:
194+
config.extensionConfiguration.cache?.projectConfig?.enabled === true
195+
? "true"
196+
: undefined,
193197
},
194198
};
195199
let stdout = "";

0 commit comments

Comments
 (0)