Skip to content

Commit

Permalink
e2e for the new cache mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jun 1, 2024
1 parent bec610d commit 51aec1c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 14 deletions.
14 changes: 13 additions & 1 deletion analysis/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,22 @@ let main () =
in
match args with
| [_; "cache-project"; rootPath] -> (
Cfg.useProjectConfigCache := false;
let uri = Uri.fromPath rootPath in
match Packages.getPackage ~uri with
| Some package -> Cache.cacheProject package
| None -> ())
| None -> print_endline "\"ERR\"")
| [_; "cache-delete"; rootPath] -> (
Cfg.useProjectConfigCache := false;
let uri = Uri.fromPath rootPath in
match Packages.findRoot ~uri (Hashtbl.create 0) with
| Some (`Bs rootPath) -> (
match BuildSystem.getLibBs rootPath with
| None -> print_endline "\"ERR\""
| Some libBs ->
Cache.deleteCache (Cache.targetFileFromLibBs libBs);
print_endline "\"OK\"")
| _ -> print_endline "\"ERR: Did not find root \"")
| [_; "completion"; path; line; col; currentFile] ->
printHeaderInfo path line col;
Commands.completion ~debug ~path
Expand Down
6 changes: 4 additions & 2 deletions analysis/src/Cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ let readCache filename =
with _ -> None
else None

let deleteCache filename = try Sys.remove filename with _ -> ()

let targetFileFromLibBs libBs = Filename.concat libBs ".project-files-cache"

let cacheProject (package : package) =
Expand All @@ -32,8 +34,8 @@ let cacheProject (package : package) =
}
in
match BuildSystem.getLibBs package.rootPath with
| None -> ()
| None -> print_endline "\"ERR\""
| Some libBs ->
let targetFile = targetFileFromLibBs libBs in
writeCache targetFile cached;
print_endline "OK"
print_endline "\"OK\""
2 changes: 1 addition & 1 deletion analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ let findRoot ~uri packagesByRoot =
let parent = Filename.dirname path in
if parent = path then (* reached root *) None else loop parent
in
loop (Filename.dirname path)
loop (if Sys.is_directory path then path else Filename.dirname path)

let getPackage ~uri =
let open SharedTypes in
Expand Down
3 changes: 2 additions & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ export function activate(context: ExtensionContext) {
affectsConfiguration("rescript.settings.inlayHints") ||
affectsConfiguration("rescript.settings.codeLens") ||
affectsConfiguration("rescript.settings.signatureHelp") ||
affectsConfiguration("rescript.settings.incrementalTypechecking")
affectsConfiguration("rescript.settings.incrementalTypechecking") ||
affectsConfiguration("rescript.settings.cache")
) {
commands.executeCommand("rescript-vscode.restart_language_server");
} else {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@
"default": false,
"description": "(debug) Enable debug logging (ends up in the extension output)."
},
"rescript.settings.cache.projectConfig.enabled": {
"type": "boolean",
"default": false,
"description": "(beta/experimental) Enable project config caching. Can speed up latency dramatically."
},
"rescript.settings.binaryPath": {
"type": [
"string",
Expand Down
12 changes: 11 additions & 1 deletion server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface extensionConfiguration {
acrossFiles?: boolean;
debugLogging?: boolean;
};
cache?: {
projectConfig?: {
enabled?: boolean;
};
};
}

// All values here are temporary, and will be overridden as the server is
Expand All @@ -43,7 +48,12 @@ let config: { extensionConfiguration: extensionConfiguration } = {
incrementalTypechecking: {
enabled: false,
acrossFiles: false,
debugLogging: true,
debugLogging: false,
},
cache: {
projectConfig: {
enabled: false,
},
},
},
};
Expand Down
1 change: 1 addition & 0 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export let bsconfigPartialPath = "bsconfig.json";
export let rescriptJsonPartialPath = "rescript.json";
export let compilerDirPartialPath = path.join("lib", "bs");
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");
export let buildNinjaPartialPath = path.join("lib", "bs", "build.ninja");
export let resExt = ".res";
export let resiExt = ".resi";
export let cmiExt = ".cmi";
Expand Down
57 changes: 49 additions & 8 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,49 @@ let sendCompilationFinishedMessage = () => {
send(notification);
};

let debug = false;

let syncProjectConfigCache = (rootPath: string) => {
try {
if (debug) console.log("syncing project config cache for " + rootPath);
utils.runAnalysisAfterSanityCheck(rootPath, ["cache-project", rootPath]);
if (debug) console.log("OK - synced project config cache for " + rootPath);
} catch (e) {
if (debug) console.error(e);
}
};

let deleteProjectConfigCache = (rootPath: string) => {
try {
if (debug) console.log("deleting project config cache for " + rootPath);
utils.runAnalysisAfterSanityCheck(rootPath, ["cache-delete", rootPath]);
if (debug) console.log("OK - deleted project config cache for " + rootPath);
} catch (e) {
if (debug) console.error(e);
}
};

let compilerLogsWatcher = chokidar
.watch([], {
awaitWriteFinish: {
stabilityThreshold: 1,
},
})
.on("all", (_e, _changedPath) => {
sendUpdatedDiagnostics();
sendCompilationFinishedMessage();
if (config.extensionConfiguration.inlayHints?.enable === true) {
sendInlayHintsRefresh();
}
if (config.extensionConfiguration.codeLens === true) {
sendCodeLensRefresh();
.on("all", (_e, changedPath) => {
if (changedPath.includes("build.ninja")) {
let projectRoot = utils.findProjectRootOfFile(changedPath);
if (projectRoot != null) {
syncProjectConfigCache(projectRoot);
}
} else {
sendUpdatedDiagnostics();
sendCompilationFinishedMessage();
if (config.extensionConfiguration.inlayHints?.enable === true) {
sendInlayHintsRefresh();
}
if (config.extensionConfiguration.codeLens === true) {
sendCodeLensRefresh();
}
}
});
let stopWatchingCompilerLog = () => {
Expand Down Expand Up @@ -257,6 +286,14 @@ let openedFile = (fileUri: string, fileContent: string) => {
compilerLogsWatcher.add(
path.join(projectRootPath, c.compilerLogPartialPath)
);
if (
config.extensionConfiguration.cache?.projectConfig?.enabled === true
) {
compilerLogsWatcher.add(
path.join(projectRootPath, c.buildNinjaPartialPath)
);
syncProjectConfigCache(projectRootPath);
}
}
let root = projectsFiles.get(projectRootPath)!;
root.openFiles.add(filePath);
Expand Down Expand Up @@ -335,6 +372,10 @@ let closedFile = (fileUri: string) => {
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.compilerLogPartialPath)
);
compilerLogsWatcher.unwatch(
path.join(projectRootPath, c.buildNinjaPartialPath)
);
deleteProjectConfigCache(projectRootPath);
deleteProjectDiagnostics(projectRootPath);
if (root.bsbWatcherByEditor !== null) {
root.bsbWatcherByEditor.kill();
Expand Down
4 changes: 4 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ export let runAnalysisAfterSanityCheck = (
config.extensionConfiguration.incrementalTypechecking?.enabled === true
? "true"
: undefined,
RESCRIPT_PROJECT_CONFIG_CACHE:
config.extensionConfiguration.cache?.projectConfig?.enabled === true
? "true"
: undefined,
},
};
let stdout = "";
Expand Down

0 comments on commit 51aec1c

Please sign in to comment.