Skip to content

Commit 6a3da22

Browse files
authored
add null check for curSourceFile and log error if curSourceFile is null (#75)
Fixes [ENT-226](https://linear.app/hasura/issue/ENT-226/connector-crash-reported-by-ngs-super) An attempt at fixing the following error: ``` [02:42:41.897] FATAL (1): Cannot set properties of undefined (setting 'fileContent') err: { "type": "TypeError", "message": "Cannot set properties of undefined (setting 'fileContent')", "stack": TypeError: Cannot set properties of undefined (setting 'fileContent') at Object.fixImports (/app/dist/app/parser/typescript/cleanup.js:48:26) at cleanupAndFormat (/app/dist/app/writer/functions-ts-writer.js:49:13) at writeContentToFile (/app/dist/app/writer/functions-ts-writer.js:45:11) at Object.writeToFileSystem (/app/dist/app/writer/functions-ts-writer.js:35:15) at Object.writeToFileSystem (/app/dist/app/writer/index.js:39:31) at Object.runApp (/app/dist/app/index.js:45:18) at async main (/app/dist/cli/update.js:73:9) } ``` **NOTE** I have not been able to reproduce this issue, this fix is a best case attempt by studying the crash logs
1 parent 48aed4d commit 6a3da22

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/app/parser/typescript/cleanup.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as ts from "ts-morph";
22
import * as types from "../../types";
33
import * as context from "../../context";
4+
import * as logger from "../../../util/logger";
45
import * as fs from "fs";
6+
import * as path from "path";
57

68
export function fixImports(generatedCodeList: types.GeneratedCode[]) {
79
let project: ts.Project;
@@ -23,12 +25,31 @@ export function fixImports(generatedCodeList: types.GeneratedCode[]) {
2325
);
2426
}
2527

26-
for (const sourceFile of project.getSourceFiles()) {
27-
sourceFile.fixMissingImports().organizeImports();
28+
try {
29+
for (const sourceFile of project.getSourceFiles()) {
30+
sourceFile.fixMissingImports().organizeImports();
2831

29-
const code = generatedCodeList.filter(
30-
(item) => item.filePath === sourceFile.getFilePath(),
31-
)[0]!;
32-
code.fileContent = sourceFile.getFullText();
32+
// find the source file in the generated code file list
33+
let curSourceFile =
34+
generatedCodeList.filter(
35+
(item) => item.filePath === sourceFile.getFilePath(),
36+
)[0] ??
37+
// if the source file is not found by path comparison, try to find it by comparing the file name
38+
generatedCodeList.filter(
39+
(item) =>
40+
path.basename(item.filePath) ===
41+
path.basename(sourceFile.getFilePath()),
42+
)[0];
43+
44+
if (curSourceFile) {
45+
curSourceFile!.fileContent = sourceFile.getFullText();
46+
} else {
47+
logger.error(
48+
`Error while fixing imports: Unable to find the source file for ${sourceFile.getFilePath()}\n\nSkipping import fixing and cleanup for this file`,
49+
);
50+
}
51+
}
52+
} catch (error) {
53+
logger.error("Error while fixing imports. Skipping", error);
3354
}
3455
}

0 commit comments

Comments
 (0)