Skip to content

Commit

Permalink
more precomputation
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jun 5, 2024
1 parent 3b1d2a9 commit 369f9e5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
53 changes: 25 additions & 28 deletions server/src/incrementalCompilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import config, { send } from "./config";
import * as c from "./constants";
import * as chokidar from "chokidar";
import { fileCodeActions } from "./codeActions";
import { projectsFiles } from "./projectFiles";

function debug() {
return (
Expand Down Expand Up @@ -75,8 +76,6 @@ type IncrementallyCompiledFileInfo = {
callArgs: Promise<Array<string> | null>;
/** The location of the incremental folder for this project. */
incrementalFolderPath: string;
/** The ReScript version. */
rescriptVersion: string;
};
/** Any code actions for this incremental file. */
codeActions: Array<fileCodeActions>;
Expand Down Expand Up @@ -284,6 +283,8 @@ function getBscArgs(
});
} else if (buildSystem === "rewatch") {
try {
const project = projectsFiles.get(entry.project.rootPath);
if (project?.rescriptVersion == null) return;
let rewatchPath = path.resolve(
entry.project.workspaceRootPath,
"node_modules/@rolandpeelen/rewatch/rewatch"
Expand All @@ -292,7 +293,7 @@ function getBscArgs(
cp
.execFileSync(rewatchPath, [
"--rescript-version",
entry.project.rescriptVersion,
project.rescriptVersion,
"--compiler-args",
entry.file.sourceFilePath,
])
Expand Down Expand Up @@ -364,21 +365,21 @@ function triggerIncrementalCompilationOfFile(
if (incrementalFileCacheEntry == null) {
// New file
const projectRootPath = utils.findProjectRootOfFile(filePath);
const workspaceRootPath = projectRootPath
? utils.findProjectRootOfFile(projectRootPath)
: null;
if (projectRootPath == null) {
if (debug())
console.log("Did not find project root path for " + filePath);
return;
}
const namespaceName = utils.getNamespaceNameFromConfigFile(projectRootPath);
if (namespaceName.kind === "error") {
if (debug())
console.log("Getting namespace config errored for " + filePath);
const project = projectsFiles.get(projectRootPath);
if (project == null) {
if (debug()) console.log("Did not find open project for " + filePath);
return;
}
const bscBinaryLocation = utils.findBscExeBinary(projectRootPath);
const workspaceRootPath = projectRootPath
? utils.findProjectRootOfFile(projectRootPath)
: null;

const bscBinaryLocation = project.bscBinaryLocation;
if (bscBinaryLocation == null) {
if (debug())
console.log("Could not find bsc binary location for " + filePath);
Expand All @@ -387,28 +388,15 @@ function triggerIncrementalCompilationOfFile(
const ext = filePath.endsWith(".resi") ? ".resi" : ".res";
const moduleName = path.basename(filePath, ext);
const moduleNameNamespaced =
namespaceName.result !== ""
? `${moduleName}-${namespaceName.result}`
project.namespaceName != null
? `${moduleName}-${project.namespaceName}`
: moduleName;

const incrementalFolderPath = path.join(
projectRootPath,
INCREMENTAL_FILE_FOLDER_LOCATION
);

let rescriptVersion = "";
try {
rescriptVersion = cp
.execFileSync(bscBinaryLocation, ["-version"])
.toString()
.trim();
} catch (e) {
console.error(e);
}
if (rescriptVersion.startsWith("ReScript ")) {
rescriptVersion = rescriptVersion.replace("ReScript ", "");
}

let originalTypeFileLocation = path.resolve(
projectRootPath,
"lib/bs",
Expand Down Expand Up @@ -436,7 +424,6 @@ function triggerIncrementalCompilationOfFile(
callArgs: Promise.resolve([]),
bscBinaryLocation,
incrementalFolderPath,
rescriptVersion,
},
buildRewatch: null,
buildNinja: null,
Expand Down Expand Up @@ -488,6 +475,16 @@ function verifyTriggerToken(filePath: string, triggerToken: number): boolean {
);
}
async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) {
const project = projectsFiles.get(entry.project.rootPath);
if (project?.rescriptVersion == null) {
if (debug()) {
console.log(
"Found no project (or ReScript version) for " +
entry.file.sourceFilePath
);
}
return null;
}
const res = await getBscArgs(entry);
if (res == null) return null;
let astArgs: Array<Array<string>> = [];
Expand Down Expand Up @@ -547,7 +544,7 @@ async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) {
});

callArgs.push("-color", "never");
if (parseInt(entry.project.rescriptVersion.split(".")[0] ?? "10") >= 11) {
if (parseInt(project.rescriptVersion.split(".")[0] ?? "10") >= 11) {
// Only available in v11+
callArgs.push("-ignore-parse-errors");
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/projectFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface projectFiles {
filesWithDiagnostics: Set<string>;
filesDiagnostics: filesDiagnostics;
rescriptVersion: string | undefined;
bscBinaryLocation: string | null;
namespaceName: string | null;

bsbWatcherByEditor: null | cp.ChildProcess;

Expand Down
10 changes: 9 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,18 @@ let openedFile = (fileUri: string, fileContent: string) => {
if (config.extensionConfiguration.incrementalTypechecking?.enabled) {
ic.recreateIncrementalFileFolder(projectRootPath);
}
const namespaceName =
utils.getNamespaceNameFromConfigFile(projectRootPath);

projectRootState = {
openFiles: new Set(),
filesWithDiagnostics: new Set(),
filesDiagnostics: {},
namespaceName:
namespaceName.kind === "success" ? namespaceName.result : null,
rescriptVersion: utils.findReScriptVersion(projectRootPath),
bsbWatcherByEditor: null,
bscBinaryLocation: utils.findBscExeBinary(projectRootPath),
hasPromptedToStartBuild: /(\/|\\)node_modules(\/|\\)/.test(
projectRootPath
)
Expand Down Expand Up @@ -795,7 +801,9 @@ function format(msg: p.RequestMessage): Array<p.Message> {
let code = getOpenedFileContent(params.textDocument.uri);

let projectRootPath = utils.findProjectRootOfFile(filePath);
let bscExeBinaryPath = utils.findBscExeBinary(projectRootPath);
let project =
projectRootPath != null ? projectsFiles.get(projectRootPath) : null;
let bscExeBinaryPath = project?.bscBinaryLocation ?? null;

let formattedResult = utils.formatCode(
bscExeBinaryPath,
Expand Down
8 changes: 6 additions & 2 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ export let findProjectRootOfFile = (
if (
foundRootFromProjectFiles == null ||
rootPath.length > foundRootFromProjectFiles.length
)
) {
foundRootFromProjectFiles = rootPath;
}
}
}

if (foundRootFromProjectFiles != null) {
return foundRootFromProjectFiles;
} else {
return findProjectRootOfFileInDir(source);
const isDir = path.extname(source) === "";
return findProjectRootOfFileInDir(
isDir ? path.join(source, "dummy.res") : source
);
}
};

Expand Down

0 comments on commit 369f9e5

Please sign in to comment.