Skip to content

Commit

Permalink
run analysis from compiler package if high enough version
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Nov 11, 2024
1 parent 2837e08 commit 3b57c41
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
9 changes: 5 additions & 4 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ export let jsonrpcVersion = "2.0";
export let platformPath = path.join("rescript", platformDir);
export let nodeModulesPlatformPath = path.join("node_modules", platformPath);
export let bscExeName = "bsc.exe";
export let editorAnalysisName = "rescript-editor-analysis.exe";
export let bscNativeReScriptPartialPath = path.join(
nodeModulesPlatformPath,
bscExeName
);

export let analysisDevPath = path.join(
export let builtinAnalysisDevPath = path.join(
path.dirname(__dirname),
"..",
"rescript-editor-analysis.exe"
editorAnalysisName
);
export let analysisProdPath = path.join(
export let builtinAnalysisProdPath = path.join(
path.dirname(__dirname),
"analysis_binaries",
platformDir,
"rescript-editor-analysis.exe"
editorAnalysisName
);

export let rescriptBinName = "rescript";
Expand Down
1 change: 1 addition & 0 deletions server/src/projectFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface projectFiles {
filesDiagnostics: filesDiagnostics;
rescriptVersion: string | undefined;
bscBinaryLocation: string | null;
editorAnalysisLocation: string | null;
namespaceName: string | null;

bsbWatcherByEditor: null | cp.ChildProcess;
Expand Down
1 change: 1 addition & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
rescriptVersion: utils.findReScriptVersion(projectRootPath),
bsbWatcherByEditor: null,
bscBinaryLocation: utils.findBscExeBinary(projectRootPath),
editorAnalysisLocation: utils.findEditorAnalysisBinary(projectRootPath),
hasPromptedToStartBuild: /(\/|\\)node_modules(\/|\\)/.test(
projectRootPath
)
Expand Down
53 changes: 43 additions & 10 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,19 @@ export let findReScriptVersion = (
}
};

let binaryPath: string | null = null;
if (fs.existsSync(c.analysisDevPath)) {
binaryPath = c.analysisDevPath;
} else if (fs.existsSync(c.analysisProdPath)) {
binaryPath = c.analysisProdPath;
} else {
// This is the path for the _builtin_ legacy analysis, that works for versions 11 and below.
let builtinBinaryPath: string | null = null;
if (fs.existsSync(c.builtinAnalysisDevPath)) {
builtinBinaryPath = c.builtinAnalysisDevPath;
} else if (fs.existsSync(c.builtinAnalysisProdPath)) {
builtinBinaryPath = c.builtinAnalysisProdPath;
}

export let runAnalysisAfterSanityCheck = (
filePath: p.DocumentUri,
args: Array<any>,
projectRequired = false
) => {
if (binaryPath == null) {
return null;
}

let projectRootPath = findProjectRootOfFile(filePath);
if (projectRootPath == null && projectRequired) {
return null;
Expand All @@ -217,6 +213,35 @@ export let runAnalysisAfterSanityCheck = (
projectsFiles.get(projectRootPath ?? "")?.rescriptVersion ??
findReScriptVersion(filePath);

let binaryPath = builtinBinaryPath;

let project = projectRootPath ? projectsFiles.get(projectRootPath) : null;

/**
* All versions including 12.0.0-alpha.5 and above should use the analysis binary
* that now ships with the compiler. Previous versions use the legacy one we ship
* with the extension itself.
*/
let shouldUseBuiltinAnalysis =
rescriptVersion?.startsWith("9.") ||
rescriptVersion?.startsWith("10.") ||
rescriptVersion?.startsWith("11.") ||
[
"12.0.0-alpha.1",
"12.0.0-alpha.2",
"12.0.0-alpha.3",
"12.0.0-alpha.4",
].includes(rescriptVersion ?? "");

if (!shouldUseBuiltinAnalysis && project != null) {
binaryPath = project.editorAnalysisLocation;
} else if (!shouldUseBuiltinAnalysis && project == null) {
// TODO: Warn user about broken state?
return null;
} else {
binaryPath = builtinBinaryPath;
}

let options: childProcess.ExecFileSyncOptions = {
cwd: projectRootPath || undefined,
maxBuffer: Infinity,
Expand All @@ -233,6 +258,11 @@ export let runAnalysisAfterSanityCheck = (
: undefined,
},
};

if (binaryPath == null) {
return null;
}

let stdout = "";
try {
stdout = childProcess.execFileSync(binaryPath, args, options).toString();
Expand Down Expand Up @@ -737,3 +767,6 @@ let findPlatformPath = (projectRootPath: p.DocumentUri | null) => {

export let findBscExeBinary = (projectRootPath: p.DocumentUri | null) =>
findBinary(findPlatformPath(projectRootPath), c.bscExeName);

export let findEditorAnalysisBinary = (projectRootPath: p.DocumentUri | null) =>
findBinary(findPlatformPath(projectRootPath), c.editorAnalysisName);

0 comments on commit 3b57c41

Please sign in to comment.