Skip to content

Commit b3ca0e6

Browse files
committed
Refactor debug() from incrementalCompilation
1 parent a0dd178 commit b3ca0e6

File tree

4 files changed

+74
-102
lines changed

4 files changed

+74
-102
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@
236236
},
237237
"rescript.settings.logLevel": {
238238
"type": "string",
239-
"enum": ["error", "warn", "info", "log", "debug"],
239+
"enum": [
240+
"error",
241+
"warn",
242+
"info",
243+
"log",
244+
"debug"
245+
],
240246
"default": "info",
241247
"description": "Verbosity of ReScript language server logs sent to the Output channel."
242248
}

server/src/bsc-args/rewatch.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import * as utils from "../utils";
33
import * as cp from "node:child_process";
44
import * as p from "vscode-languageserver-protocol";
55
import semver from "semver";
6-
import {
7-
debug,
8-
IncrementallyCompiledFileInfo,
9-
} from "../incrementalCompilation";
6+
import { IncrementallyCompiledFileInfo } from "../incrementalCompilation";
107
import type { projectFiles } from "../projectFiles";
118
import { jsonrpcVersion } from "../constants";
9+
import { getLogger } from "../logger";
1210

1311
export type RewatchCompilerArgs = {
1412
compiler_args: Array<string>;
@@ -68,15 +66,11 @@ export async function getRewatchBscArgs(
6866

6967
if (rescriptRewatchPath != null) {
7068
rewatchPath = rescriptRewatchPath;
71-
if (debug()) {
72-
console.log(
73-
`Found rewatch binary bundled with v12: ${rescriptRewatchPath}`,
74-
);
75-
}
69+
getLogger().log(
70+
`Found rewatch binary bundled with v12: ${rescriptRewatchPath}`,
71+
);
7672
} else {
77-
if (debug()) {
78-
console.log("Did not find rewatch binary bundled with v12");
79-
}
73+
getLogger().log("Did not find rewatch binary bundled with v12");
8074
}
8175

8276
const rewatchArguments = semver.satisfies(

server/src/incrementalCompilation.ts

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import { getRewatchBscArgs, RewatchCompilerArgs } from "./bsc-args/rewatch";
1414
import { BsbCompilerArgs, getBsbBscArgs } from "./bsc-args/bsb";
1515
import { getCurrentCompilerDiagnosticsForFile } from "./server";
1616
import { NormalizedPath } from "./utils";
17-
18-
export function debug() {
19-
return (
20-
config.extensionConfiguration.incrementalTypechecking?.debugLogging ?? false
21-
);
22-
}
17+
import { getLogger } from "./logger";
2318

2419
const INCREMENTAL_FOLDER_NAME = "___incremental";
2520
const INCREMENTAL_FILE_FOLDER_LOCATION = path.join(
@@ -96,13 +91,11 @@ export function incrementalCompilationFileChanged(changedPath: NormalizedPath) {
9691
if (filePath != null) {
9792
const entry = incrementallyCompiledFileInfo.get(filePath);
9893
if (entry != null) {
99-
if (debug()) {
100-
console.log("[watcher] Cleaning up incremental files for " + filePath);
101-
}
94+
getLogger().log(
95+
"[watcher] Cleaning up incremental files for " + filePath,
96+
);
10297
if (entry.compilation != null) {
103-
if (debug()) {
104-
console.log("[watcher] Was compiling, killing");
105-
}
98+
getLogger().log("[watcher] Was compiling, killing");
10699
clearTimeout(entry.compilation.timeout);
107100
entry.killCompilationListeners.forEach((cb) => cb());
108101
entry.compilation = null;
@@ -129,9 +122,7 @@ export function removeIncrementalFileFolder(
129122
}
130123

131124
export function recreateIncrementalFileFolder(projectRootPath: NormalizedPath) {
132-
if (debug()) {
133-
console.log("Recreating incremental file folder");
134-
}
125+
getLogger().log("Recreating incremental file folder");
135126
removeIncrementalFileFolder(projectRootPath, () => {
136127
fs.mkdir(
137128
path.resolve(projectRootPath, INCREMENTAL_FILE_FOLDER_LOCATION),
@@ -153,9 +144,7 @@ export function cleanUpIncrementalFiles(
153144
? `${fileNameNoExt}-${namespace.result}`
154145
: fileNameNoExt;
155146

156-
if (debug()) {
157-
console.log("Cleaning up incremental file assets for: " + fileNameNoExt);
158-
}
147+
getLogger().log("Cleaning up incremental file assets for: " + fileNameNoExt);
159148

160149
fs.unlink(
161150
path.resolve(
@@ -252,15 +241,14 @@ function triggerIncrementalCompilationOfFile(
252241
// New file
253242
const projectRootPath = utils.findProjectRootOfFile(filePath);
254243
if (projectRootPath == null) {
255-
if (debug())
256-
console.log("Did not find project root path for " + filePath);
244+
getLogger().log("Did not find project root path for " + filePath);
257245
return;
258246
}
259247
// projectRootPath is already normalized (NormalizedPath) from findProjectRootOfFile
260248
// Use getProjectFile to verify the project exists
261249
const project = utils.getProjectFile(projectRootPath);
262250
if (project == null) {
263-
if (debug()) console.log("Did not find open project for " + filePath);
251+
getLogger().log("Did not find open project for " + filePath);
264252
return;
265253
}
266254

@@ -279,20 +267,19 @@ function triggerIncrementalCompilationOfFile(
279267
projectRootPath != null &&
280268
utils.findProjectRootOfDir(projectRootPath) != null;
281269

282-
if (foundRewatchLockfileInProjectRoot && debug()) {
283-
console.log(
270+
if (foundRewatchLockfileInProjectRoot) {
271+
getLogger().log(
284272
`Found rewatch/rescript lockfile in project root, treating as local package in workspace`,
285273
);
286-
} else if (!foundRewatchLockfileInProjectRoot && debug()) {
287-
console.log(
274+
} else {
275+
getLogger().log(
288276
`Did not find rewatch/rescript lockfile in project root, assuming bsb`,
289277
);
290278
}
291279

292280
const bscBinaryLocation = project.bscBinaryLocation;
293281
if (bscBinaryLocation == null) {
294-
if (debug())
295-
console.log("Could not find bsc binary location for " + filePath);
282+
getLogger().log("Could not find bsc binary location for " + filePath);
296283
return;
297284
}
298285
const ext = filePath.endsWith(".resi") ? ".resi" : ".res";
@@ -401,12 +388,9 @@ async function figureOutBscArgs(
401388
) {
402389
const project = projectsFiles.get(entry.project.rootPath);
403390
if (project?.rescriptVersion == null) {
404-
if (debug()) {
405-
console.log(
406-
"Found no project (or ReScript version) for " +
407-
entry.file.sourceFilePath,
408-
);
409-
}
391+
getLogger().log(
392+
"Found no project (or ReScript version) for " + entry.file.sourceFilePath,
393+
);
410394
return null;
411395
}
412396
const res = await getBscArgs(send, entry);
@@ -515,11 +499,9 @@ async function compileContents(
515499
callArgs = callArgsRetried;
516500
entry.project.callArgs = Promise.resolve(callArgsRetried);
517501
} else {
518-
if (debug()) {
519-
console.log(
520-
"Could not figure out call args. Maybe build.ninja does not exist yet?",
521-
);
522-
}
502+
getLogger().log(
503+
"Could not figure out call args. Maybe build.ninja does not exist yet?",
504+
);
523505
return;
524506
}
525507
}
@@ -537,41 +519,35 @@ async function compileContents(
537519
entry.buildSystem === "bsb"
538520
? entry.project.rootPath
539521
: path.resolve(entry.project.rootPath, c.compilerDirPartialPath);
540-
if (debug()) {
541-
console.log(
542-
`About to invoke bsc from \"${cwd}\", used ${entry.buildSystem}`,
543-
);
544-
console.log(
545-
`${entry.project.bscBinaryLocation} ${callArgs.map((c) => `"${c}"`).join(" ")}`,
546-
);
547-
}
522+
getLogger().log(
523+
`About to invoke bsc from \"${cwd}\", used ${entry.buildSystem}`,
524+
);
525+
getLogger().log(
526+
`${entry.project.bscBinaryLocation} ${callArgs.map((c) => `"${c}"`).join(" ")}`,
527+
);
548528
const process = cp.execFile(
549529
entry.project.bscBinaryLocation,
550530
callArgs,
551531
{ cwd },
552532
async (error, _stdout, stderr) => {
553533
if (!error?.killed) {
554-
if (debug())
555-
console.log(
556-
`Recompiled ${entry.file.sourceFileName} in ${
557-
(performance.now() - startTime) / 1000
558-
}s`,
559-
);
534+
getLogger().log(
535+
`Recompiled ${entry.file.sourceFileName} in ${
536+
(performance.now() - startTime) / 1000
537+
}s`,
538+
);
560539
} else {
561-
if (debug())
562-
console.log(
563-
`Compilation of ${entry.file.sourceFileName} was killed.`,
564-
);
540+
getLogger().log(
541+
`Compilation of ${entry.file.sourceFileName} was killed.`,
542+
);
565543
}
566544
let hasIgnoredErrorMessages = false;
567545
if (
568546
!error?.killed &&
569547
triggerToken != null &&
570548
verifyTriggerToken(entry.file.sourceFilePath, triggerToken)
571549
) {
572-
if (debug()) {
573-
console.log("Resetting compilation status.");
574-
}
550+
getLogger().log("Resetting compilation status.");
575551
// Reset compilation status as this compilation finished
576552
entry.compilation = null;
577553
const { result, codeActions } = await utils.parseCompilerLogOutput(
@@ -706,9 +682,7 @@ export function handleUpdateOpenedFile(
706682
send: send,
707683
onCompilationFinished?: () => void,
708684
) {
709-
if (debug()) {
710-
console.log("Updated: " + filePath);
711-
}
685+
getLogger().log("Updated: " + filePath);
712686
triggerIncrementalCompilationOfFile(
713687
filePath,
714688
fileContent,
@@ -718,9 +692,7 @@ export function handleUpdateOpenedFile(
718692
}
719693

720694
export function handleClosedFile(filePath: NormalizedPath) {
721-
if (debug()) {
722-
console.log("Closed: " + filePath);
723-
}
695+
getLogger().log("Closed: " + filePath);
724696
const entry = incrementallyCompiledFileInfo.get(filePath);
725697
if (entry == null) return;
726698
cleanUpIncrementalFiles(filePath, entry.project.rootPath);

server/src/server.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ import { projectsFiles } from "./projectFiles";
3131
import { NormalizedPath } from "./utils";
3232
import { initializeLogger, getLogger, setLogLevel, LogLevel } from "./logger";
3333

34+
function applyLogLevel(configuration: extensionConfiguration) {
35+
const debugLoggingEnabled =
36+
configuration.incrementalTypechecking?.debugLogging === true;
37+
38+
if (debugLoggingEnabled) {
39+
// incrementalTypechecking.debugLogging overrides logLevel to retain legacy behavior
40+
setLogLevel("log");
41+
return;
42+
}
43+
44+
const level = configuration.logLevel as LogLevel | undefined;
45+
46+
if (
47+
level === "error" ||
48+
level === "warn" ||
49+
level === "info" ||
50+
level === "log"
51+
) {
52+
setLogLevel(level);
53+
}
54+
}
55+
3456
// Absolute paths to all the workspace folders
3557
// Configured during the initialize request
3658
export const workspaceFolders = new Set<NormalizedPath>();
@@ -1469,19 +1491,7 @@ async function onMessage(msg: p.Message) {
14691491

14701492
if (initialConfiguration != null) {
14711493
config.extensionConfiguration = initialConfiguration;
1472-
1473-
let initialLogLevel = initialConfiguration.logLevel as
1474-
| LogLevel
1475-
| undefined;
1476-
1477-
if (
1478-
initialLogLevel === "error" ||
1479-
initialLogLevel === "warn" ||
1480-
initialLogLevel === "info" ||
1481-
initialLogLevel === "log"
1482-
) {
1483-
setLogLevel(initialLogLevel);
1484-
}
1494+
applyLogLevel(initialConfiguration);
14851495
}
14861496

14871497
// These are static configuration options the client can set to enable certain
@@ -1692,17 +1702,7 @@ async function onMessage(msg: p.Message) {
16921702
];
16931703
if (configuration != null) {
16941704
config.extensionConfiguration = configuration;
1695-
1696-
let updatedLogLevel = configuration.logLevel as LogLevel | undefined;
1697-
1698-
if (
1699-
updatedLogLevel === "error" ||
1700-
updatedLogLevel === "warn" ||
1701-
updatedLogLevel === "info" ||
1702-
updatedLogLevel === "log"
1703-
) {
1704-
setLogLevel(updatedLogLevel);
1705-
}
1705+
applyLogLevel(configuration);
17061706
}
17071707
}
17081708
} else if (

0 commit comments

Comments
 (0)