Skip to content

Commit 786e57a

Browse files
committed
Fix monorepo build detection: watch only root .compiler.log; run reanalyze from workspace root
- Server: restrict .compiler.log watching to ROOT/lib/bs/.compiler.log per workspace folder to avoid event floods from packages/** and node_modules/**.\n- Server: fix Start Build spawning for v12 layouts by preferring rescript.exe and falling back to rescript (also respects rescript.settings.binaryPath).\n- Client: anchor Start Code Analysis to the VSCode workspace folder root rather than the active file directory.\n- Client: resolve code-analysis binary relative to the chosen cwd (workspace root) to avoid subpackage-anchored tool lookup.\n\nFiles: server/src/server.ts, client/src/extension.ts, client/src/commands/code_analysis.ts
1 parent 92dae41 commit 786e57a

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

client/src/commands/code_analysis.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,14 @@ export const runCodeAnalysisWithReanalyze = (
213213
let currentDocument = window.activeTextEditor.document;
214214
let cwd = targetDir ?? path.dirname(currentDocument.uri.fsPath);
215215

216+
// Resolve the project root from `cwd` (which is the workspace root when code analysis is started),
217+
// rather than from the currently-open file (which may be in a subpackage).
216218
let projectRootPath: NormalizedPath | null = findProjectRootOfFileInDir(
217-
currentDocument.uri.fsPath,
219+
path.join(cwd, "bsconfig.json"),
218220
);
221+
if (projectRootPath == null) {
222+
projectRootPath = findProjectRootOfFileInDir(currentDocument.uri.fsPath);
223+
}
219224

220225
// Try v12+ path first: @rescript/{platform}-{arch}/bin/rescript-tools.exe
221226
// Then fall back to legacy paths via getBinaryPath

client/src/extension.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,11 @@ export function activate(context: ExtensionContext) {
442442

443443
inCodeAnalysisState.active = true;
444444

445-
// Pointing reanalyze to the dir of the current file path is fine, because
446-
// reanalyze will walk upwards looking for a bsconfig.json in order to find
447-
// the correct project root.
448-
inCodeAnalysisState.activatedFromDirectory = path.dirname(
449-
currentDocument.uri.fsPath,
450-
);
445+
// Run reanalyze from the workspace root (so monorepos consistently analyze the root project),
446+
// instead of from whatever file happened to be active when analysis was started.
447+
const wsFolder = workspace.getWorkspaceFolder(currentDocument.uri);
448+
inCodeAnalysisState.activatedFromDirectory =
449+
wsFolder?.uri.fsPath ?? path.dirname(currentDocument.uri.fsPath);
451450

452451
codeAnalysisRunningStatusBarItem.command =
453452
"rescript-vscode.stop_code_analysis";

server/src/server.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,28 @@ let findRescriptBinary = async (
118118
): Promise<utils.NormalizedPath | null> => {
119119
if (
120120
config.extensionConfiguration.binaryPath != null &&
121-
fs.existsSync(
122-
path.join(config.extensionConfiguration.binaryPath, "rescript"),
123-
)
121+
(fs.existsSync(
122+
path.join(config.extensionConfiguration.binaryPath, "rescript.exe"),
123+
) ||
124+
fs.existsSync(
125+
path.join(config.extensionConfiguration.binaryPath, "rescript"),
126+
))
124127
) {
125128
return utils.normalizePath(
126-
path.join(config.extensionConfiguration.binaryPath, "rescript"),
129+
fs.existsSync(
130+
path.join(config.extensionConfiguration.binaryPath, "rescript.exe"),
131+
)
132+
? path.join(config.extensionConfiguration.binaryPath, "rescript.exe")
133+
: path.join(config.extensionConfiguration.binaryPath, "rescript"),
127134
);
128135
}
129136

130-
return utils.findRescriptBinary(projectRootPath);
137+
// Prefer the native rescript.exe (v12+) for spawning `build -w`.
138+
// Fall back to the legacy/JS wrapper `rescript` path if needed.
139+
return (
140+
(await utils.findRescriptExeBinary(projectRootPath)) ??
141+
(await utils.findRescriptBinary(projectRootPath))
142+
);
131143
};
132144

133145
let createInterfaceRequest = new v.RequestType<
@@ -1388,11 +1400,10 @@ async function onMessage(msg: p.Message) {
13881400
const watchers = Array.from(workspaceFolders).flatMap(
13891401
(projectRootPath) => [
13901402
{
1391-
globPattern: path.join(
1392-
projectRootPath,
1393-
"**",
1394-
c.compilerLogPartialPath,
1395-
),
1403+
// Only watch the root compiler log for each workspace folder.
1404+
// In monorepos, `**/lib/bs/.compiler.log` matches every package and dependency,
1405+
// causing a burst of events per save.
1406+
globPattern: path.join(projectRootPath, c.compilerLogPartialPath),
13961407
kind: p.WatchKind.Change | p.WatchKind.Create | p.WatchKind.Delete,
13971408
},
13981409
{

0 commit comments

Comments
 (0)