Skip to content

Commit 6915ebb

Browse files
committed
Don't try to read if file doesn't exist (nushell#134)
1 parent 7caaad1 commit 6915ebb

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

package-lock.json

+16-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/src/server.ts

+27-24
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ function createLabel(name: string): string {
117117
}
118118
async function durationLogWrapper<T>(
119119
name: string,
120-
fn: () => Promise<T>
120+
fn: (label: string) => Promise<T>
121121
): Promise<T> {
122122
console.log("Triggered " + name + ": ...");
123123
const label = createLabel(name);
124124
console.time(label);
125-
const result = await fn();
125+
const result = await fn(label);
126126

127127
// This purposefully has the same prefix length as the "Triggered " log above,
128128
// also does not add a newline at the end.
@@ -244,7 +244,7 @@ async function validateTextDocument(
244244
): Promise<void> {
245245
return await durationLogWrapper(
246246
`validateTextDocument ${textDocument.uri}`,
247-
async () => {
247+
async (label) => {
248248
if (!hasDiagnosticRelatedInformationCapability) {
249249
console.error(
250250
"Trying to validate a document with no diagnostic capability"
@@ -263,7 +263,8 @@ async function validateTextDocument(
263263
text,
264264
"--ide-check",
265265
settings,
266-
textDocument.uri
266+
textDocument.uri,
267+
{ label: label }
267268
);
268269

269270
textDocument.nuInlayHints = [];
@@ -403,7 +404,7 @@ async function runCompiler(
403404
flags: string,
404405
settings: NushellIDESettings,
405406
uri: string,
406-
options: { allowErrors?: boolean } = {}
407+
options: { allowErrors?: boolean, label: string } = { label: "runCompiler" },
407408
): Promise<string> {
408409
const allowErrors =
409410
options.allowErrors === undefined ? true : options.allowErrors;
@@ -412,7 +413,7 @@ async function runCompiler(
412413
fs.writeFileSync(tmpFile.name, text);
413414
// eslint-disable-next-line @typescript-eslint/no-explicit-any
414415
} catch (e: any) {
415-
// connection.console.log(e);
416+
connection.console.error(`[${options.label}] error writing to tmp file: ${e}`);
416417
}
417418

418419
let stdout = "";
@@ -430,8 +431,7 @@ async function runCompiler(
430431
}
431432

432433
connection.console.log(
433-
"running: " +
434-
`${settings.nushellExecutablePath} ${flags} ${script_path_flag} ${tmpFile.name}`
434+
`[${options.label}] running: ${settings.nushellExecutablePath} ${flags} ${script_path_flag} ${tmpFile.name}`
435435
);
436436

437437
const output = await exec(
@@ -441,17 +441,12 @@ async function runCompiler(
441441
}
442442
);
443443
stdout = output.stdout;
444-
console.log("stdout: " + stdout);
444+
console.log(`[${options.label}] stdout: ${stdout}`);
445445
// eslint-disable-next-line @typescript-eslint/no-explicit-any
446446
} catch (e: any) {
447447
stdout = e.stdout;
448+
connection.console.log(`[${options.label}] compile failed: ` + e);
448449
if (!allowErrors) {
449-
if (e.signal != null) {
450-
connection.console.log("compile failed: ");
451-
connection.console.log(e);
452-
} else {
453-
connection.console.log("Error:" + e);
454-
}
455450
throw e;
456451
}
457452
}
@@ -568,21 +563,21 @@ connection.onCompletion(
568563
);
569564

570565
connection.onDefinition(async (request) => {
571-
return await durationLogWrapper(`onDefinition`, async () => {
566+
return await durationLogWrapper(`onDefinition`, async (label) => {
572567
const document = documents.get(request.textDocument.uri);
573568
if (!document) return;
574569
const settings = await getDocumentSettings(request.textDocument.uri);
575570

576571
const text = document.getText();
577572

578-
// connection.console.log("request: ");
579-
// connection.console.log(request.textDocument.uri);
573+
// connection.console.log(`[${label}] request: ${request.textDocument.uri}`);
580574
// connection.console.log("index: " + convertPosition(request.position, text));
581575
const stdout = await runCompiler(
582576
text,
583577
"--ide-goto-def " + convertPosition(request.position, text),
584578
settings,
585-
request.textDocument.uri
579+
request.textDocument.uri,
580+
{ label: label }
586581
);
587582
return goToDefinition(document, stdout);
588583
});
@@ -605,11 +600,19 @@ async function goToDefinition(
605600
// connection.console.log(obj);
606601
if (obj.file === "" || obj.file === "__prelude__") return;
607602

608-
const lineBreaks = findLineBreaks(
609-
obj.file
610-
? (await fs.promises.readFile(obj.file)).toString()
611-
: document.getText() ?? ""
612-
);
603+
let documentText: string;
604+
if (obj.file) {
605+
if (fs.existsSync(obj.file)) {
606+
documentText = await fs.promises.readFile(obj.file).then((b) => b.toString());
607+
} else {
608+
connection.console.log(`File ${obj.file} does not exist`);
609+
return;
610+
}
611+
} else {
612+
documentText = document.getText();
613+
}
614+
615+
const lineBreaks: number[] = findLineBreaks(documentText);
613616

614617
let uri = "";
615618
if (obj.file == tmpFile.name) {

0 commit comments

Comments
 (0)