allegro / typescript-strict-plugin

Typescript plugin that allows turning on strict mode in specific files or directories.
MIT License
332 stars 29 forks source link

Typescript server crash when using typescript-strict-plugin with Typescript 5.4 #70

Closed guillaumewuip closed 4 months ago

guillaumewuip commented 5 months ago

Hello there!

I'm using this wonderful plugin on a legacy codebase. We've just migrated to the latest TS 5.4 and text editors typescript integrations are not working anymore (no error reported, no suggestions, etc) (Intellij, VSCode, Neovim).

Here is a minimal repo reproducing the issue: https://github.com/guillaumewuip/typescript-strict-plugin-5.4-bug/blob/main/src/index.ts

I've investigated a bit, and found a potential fix.

Ensuring that we use local TS version (and not the VSCode one), we can find the related log on tsserver side:

    Cannot read properties of undefined (reading 'getSourceFile')

    TypeError: Cannot read properties of undefined (reading 'getSourceFile')
        at getValidSourceFile (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:146234:32)
        at Object.getSemanticDiagnostics (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:146498:30)
        at proxy.getSemanticDiagnostics (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript-strict-plugin@2.4.0/node_modules/typescript-strict-plugin/dist/plugin/index.js:15:38)
Bigger log ``` Info 78 [14:38:26.196] TIAdapter:: Scheduling request for: /dev/null/inferredProject1* Info 79 [14:38:26.197] AutoImportProviderProject: found 1 root files in 1 dependencies in 1.1183749996125698 ms Info 80 [14:38:26.197] Starting updateGraphWorker: Project: /dev/null/autoImportProviderProject2* Info 81 [14:38:26.237] Finishing updateGraphWorker: Project: /dev/null/autoImportProviderProject2* projectStateVersion: 1 projectProgramVersion: 0 structureChanged: true structureIsReused:: Not Elapsed: 39.96095800027251ms Info 82 [14:38:26.237] Project '/dev/null/autoImportProviderProject2*' (AutoImportProvider) Info 83 [14:38:26.237] Files (1) /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/typescript.d.ts node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/typescript.d.ts Root file specified for compilation Info 84 [14:38:26.237] ----------------------------------------------- Info 85 [14:38:26.238] Project '/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/tsconfig.json' (Configured) Info 85 [14:38:26.238] Files (20) Info 85 [14:38:26.238] ----------------------------------------------- Info 85 [14:38:26.238] Project '/dev/null/autoImportProviderProject1*' (AutoImportProvider) Info 85 [14:38:26.238] Files (1) Info 85 [14:38:26.238] ----------------------------------------------- Info 85 [14:38:26.238] Project '/dev/null/inferredProject1*' (Inferred) Info 85 [14:38:26.238] Files (55) Info 85 [14:38:26.238] ----------------------------------------------- Info 85 [14:38:26.238] Project '/dev/null/autoImportProviderProject2*' (AutoImportProvider) Info 85 [14:38:26.238] Files (1) Info 85 [14:38:26.238] ----------------------------------------------- Info 85 [14:38:26.238] Open files: Info 85 [14:38:26.238] FileName: /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/src/index.ts ProjectRootPath: /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug Info 85 [14:38:26.238] Projects: /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/tsconfig.json Info 85 [14:38:26.238] FileName: /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript-strict-plugin@2.4.0/node_modules/typescript-strict-plugin/dist/plugin/index.js ProjectRootPath: /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug Info 85 [14:38:26.238] Projects: /dev/null/inferredProject1* Perf 85 [14:38:26.238] 2::updateOpen: elapsed time (in milliseconds) 493.6265 Info 86 [14:38:26.238] response: {"seq":0,"type":"response","command":"updateOpen","request_seq":2,"success":true,"performanceData":{"updateGraphDurationMs":460.96408300101757,"createAutoImportProviderProgramDurationMs":104.01749899610877},"body":true} Info 87 [14:38:26.238] request: { "seq": 3, "type": "request", "command": "geterr", "arguments": { "delay": 0, "files": [ "/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/src/index.ts", "/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript-strict-plugin@2.4.0/node_modules/typescript-strict-plugin/dist/plugin/index.js" ] } } Perf 88 [14:38:26.239] 3::geterr: async elapsed time (in milliseconds) 0.2479 Info 89 [14:38:26.240] event: {"seq":0,"type":"event","event":"syntaxDiag","body":{"file":"/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/src/index.ts","diagnostics":[]}} Err 90 [14:38:26.247] Exception on executing command delayed processing of request 3: Cannot read properties of undefined (reading 'getSourceFile') TypeError: Cannot read properties of undefined (reading 'getSourceFile') at getValidSourceFile (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:146234:32) at Object.getSemanticDiagnostics (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:146498:30) at proxy.getSemanticDiagnostics (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript-strict-plugin@2.4.0/node_modules/typescript-strict-plugin/dist/plugin/index.js:15:38) at IpcIOSession.semanticCheck (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:185769:125) at /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:185830:14 at MultistepOperation.executeAction (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:184479:9) at /Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:184452:71 at IpcIOSession.executeWithRequestId (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:187516:14) at Object.executeWithRequestId (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:185514:57) at Immediate._onImmediate (/Users/g.clochard/Downloads/typescript-strict-plugin-5.4-bug/node_modules/.pnpm/typescript@5.4.2/node_modules/typescript/lib/tsserver.js:184452:28) at process.processImmediate (node:internal/timers:480:21) Info 91 [14:38:26.247] event: {"seq":0,"type":"event","event":"requestCompleted","body":{"request_seq":3}} ```

This validate that typescript-strict-plugin is probably the culprit.

When calling TS getSemanticDiagnostics here, it leads to a error thrown from typescript language service instance itself: Cannot read properties of undefined (reading 'getSourceFile')

Looking inside typescript code, we understand this means that the program variable is undefined here. Looks like it’s not initialized (anymore?)when the language service instance is created, but only when some methods like are called.

Calling getProgram just after language service instance initialization fixes the issue.

 const strictLanguageService = typescript.createLanguageService(strictLanguageServiceHost);
+strictLanguageService.getProgram()

Is this a valid fix from your point of view? I will push a PR for that - please don't hesitate to redirect me to a better fix if needed, thanks!

km-tr commented 4 months ago

@guillaumewuip I feel like this issue is still occurring, what do you think?

kamkry commented 4 months ago

I agree, it seems like the issue is still present

kamkry commented 4 months ago

Could you try version 2.4.2-beta.0? @km-tr

km-tr commented 4 months ago

@kamkry I tried what you suggested, but just like before, no errors seem to be displayed in TS 5.4.

kamkry commented 4 months ago

@km-tr could you please share the minimal reproducible example? The new version seems to fix the problem from this issue.

km-tr commented 4 months ago

@kamkry Sure, I'll prepare it after work, so please give me a little time.

KostkaBrukowa commented 4 months ago

For reproducible example you can take a look at https://github.com/allegro/typescript-strict-plugin/pull/75 this PR. I've bring back e2e tests and they fail on 5.4.5 version

KostkaBrukowa commented 4 months ago

@km-tr can you try 2.4.2-beta.1?

maaaaagda commented 4 months ago

2.4.2-beta.1 fixed the issue for me and my colleagues. Make sure to run TypeScript: Restart TS Server afterwards. Removing node_modules before may be required before update as well. Thank you for providing the fix ❤️

kim3er commented 4 months ago

2.4.2-beta.1 Working for me as well!

KostkaBrukowa commented 4 months ago

Released with 2.4.3 version. Closing the issue. If the issue persists please reopen

km-tr commented 4 months ago

Thank you. Version 2.4.3 worked.