Skip to content

Commit 41b620f

Browse files
authored
Merge pull request #1162 from nojaf/logger
Add LSP logger
2 parents a21c7d8 + 74430e0 commit 41b620f

File tree

7 files changed

+209
-119
lines changed

7 files changed

+209
-119
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#### :bug: Bug fix
1616

1717
- Fix rewatch lockfile detection on Windows. https://github.com/rescript-lang/rescript-vscode/pull/1160
18+
- Override default `initialConfiguration` with user specific config. https://github.com/rescript-lang/rescript-vscode/pull/1162
1819

1920
#### :nail_care: Polish
2021

2122
- Resolve symlinks when finding platform binaries. https://github.com/rescript-lang/rescript-vscode/pull/1154
23+
- Use `window/logMessage` in LSP Server for logging. https://github.com/rescript-lang/rescript-vscode/pull/1162
2224

2325
## 1.70.0
2426

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,6 @@
195195
"default": false,
196196
"description": "(beta/experimental) Enable incremental type checking across files, so that unsaved file A gets access to unsaved file B."
197197
},
198-
"rescript.settings.incrementalTypechecking.debugLogging": {
199-
"type": "boolean",
200-
"default": false,
201-
"description": "(debug) Enable debug logging (ends up in the extension output)."
202-
},
203198
"rescript.settings.cache.projectConfig.enable": {
204199
"type": "boolean",
205200
"default": true,
@@ -233,6 +228,17 @@
233228
"type": "boolean",
234229
"default": true,
235230
"description": "Show compile status in the status bar (compiling/errors/warnings/success)."
231+
},
232+
"rescript.settings.logLevel": {
233+
"type": "string",
234+
"enum": [
235+
"error",
236+
"warn",
237+
"info",
238+
"log"
239+
],
240+
"default": "info",
241+
"description": "Verbosity of ReScript language server logs sent to the Output channel."
236242
}
237243
}
238244
},

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/config.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type send = (msg: Message) => void;
44

55
export interface extensionConfiguration {
66
askToStartBuild?: boolean;
7+
logLevel?: "error" | "warn" | "info" | "log";
78
inlayHints?: {
89
enable?: boolean;
910
maxLength?: number | null;
@@ -19,7 +20,6 @@ export interface extensionConfiguration {
1920
incrementalTypechecking?: {
2021
enable?: boolean;
2122
acrossFiles?: boolean;
22-
debugLogging?: boolean;
2323
};
2424
cache?: {
2525
projectConfig?: {
@@ -28,33 +28,35 @@ export interface extensionConfiguration {
2828
};
2929
}
3030

31-
// All values here are temporary, and will be overridden as the server is
32-
// initialized, and the current config is received from the client.
33-
let config: { extensionConfiguration: extensionConfiguration } = {
34-
extensionConfiguration: {
35-
askToStartBuild: true,
36-
inlayHints: {
37-
enable: false,
38-
maxLength: 25,
39-
},
40-
codeLens: false,
41-
binaryPath: null,
42-
platformPath: null,
43-
signatureHelp: {
44-
enabled: true,
45-
forConstructorPayloads: true,
46-
},
47-
incrementalTypechecking: {
31+
export const initialConfiguration: extensionConfiguration = {
32+
askToStartBuild: true,
33+
logLevel: "info",
34+
inlayHints: {
35+
enable: false,
36+
maxLength: 25,
37+
},
38+
codeLens: false,
39+
binaryPath: null,
40+
platformPath: null,
41+
signatureHelp: {
42+
enabled: true,
43+
forConstructorPayloads: true,
44+
},
45+
incrementalTypechecking: {
46+
enable: true,
47+
acrossFiles: false,
48+
},
49+
cache: {
50+
projectConfig: {
4851
enable: true,
49-
acrossFiles: false,
50-
debugLogging: false,
51-
},
52-
cache: {
53-
projectConfig: {
54-
enable: true,
55-
},
5652
},
5753
},
5854
};
5955

56+
// All values here are temporary, and will be overridden as the server is
57+
// initialized, and the current config is received from the client.
58+
let config: { extensionConfiguration: extensionConfiguration } = {
59+
extensionConfiguration: initialConfiguration,
60+
};
61+
6062
export default config;

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);

0 commit comments

Comments
 (0)