microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.19k stars 12.38k forks source link

Incorrect code formatting result in untitled files #46263

Open yume-chan opened 2 years ago

yume-chan commented 2 years ago

TS Template added by @mjbvz

TypeScript Version: 4.5.0-dev.20210929

Search Terms


Issue Type: Bug

  1. Set "files.defaultLanguage": "javascript"
  2. Close all editors, close Code, and restart Code
  3. Run File: New Untitled File command
  4. Run Change Language Mode command and select TypeScript
  5. Paste this code: Promise.resolve<number>(42)
  6. Run Format Document command

Expected result: nothing changes

Actual result: incorrect format result

image

If save this untitled file somewhere, and run Format Document again, it produces the correct result.

VS Code version: Code 1.60.2 (7f6ab5485bbc008386c4386d08766667e155244e, 2021-09-22T12:00:31.514Z) OS version: Windows_NT x64 10.0.19043 Restricted Mode: No

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 x 3193)| |GPU Status|2d_canvas: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: enabled
opengl: enabled_on
rasterization: enabled
skia_renderer: enabled_on
video_decode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled| |Load (avg)|undefined| |Memory (System)|15.80GB (1.73GB free)| |Process Argv|--disable-extensions --crash-reporter-id 12b48bde-d2e1-4ed1-bea5-c909d40e482f| |Screen Reader|no| |VM|0%|
Extensions disabled
A/B Experiments ``` vsliv368cf:30146710 vsreu685:30147344 python383cf:30185419 pythonvspyt602:30300191 vspor879:30202332 vspor708:30202333 vspor363:30204092 vstes516:30244333 pythonvspyt639:30300192 pythontb:30283811 pythonptprofiler:30281270 vshan820:30294714 vstes263:30335439 vscorecescf:30358481 pythondataviewer:30285071 pythonvsuse255:30340121 vscod805:30301674 pythonvspyt200:30340761 binariesv615:30325510 vsccppwtct:30364498 pythonvssor306:30344512 bridge0708:30335490 pygetstartedt2:30371810 dockerwalkthrucf:30370837 bridge0723:30353136 pythonrunftest32:30373476 pythonf5test824:30373475 javagetstartedt:30364666 pythonvspyt187:30373474 pydsgst2:30361792 ```

Also reproducible on latest insiders build:

Version: 1.61.0-insider (user setup)
Commit: 5991e3c4b97eca5568069bf3e0910ab693ad3549
Date: 2021-09-27T05:16:11.989Z
Electron: 13.4.0
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.19043
yume-chan commented 2 years ago

Found a simpler repro steps without the setting:

  1. New Untitled File
  2. Change Language Mode -> JavaScript
  3. Change Language Mode -> TypeScript
  4. Paste code
  5. Format Document

https://user-images.githubusercontent.com/1330321/135034362-08df5ecc-2c43-4736-80a4-9d822e55e3ca.mp4

As if it's still being formatted in JavaScript mode

mjbvz commented 2 years ago

Here's the tsserver log from this case: tsserver.log

When the language mode changes from JS to TS, we call updateOpen:

    {"seq":18,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":["^/untitled/Untitled-1"],"openFiles":[]}}
...
    {"seq":19,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":[],"openFiles":[{"file":"^/untitled/Untitled-1","fileContent":"Promise.resolve<number>(42)","projectRootPath":"/Users/matb/projects/san","scriptKindName":"TS"}]}}

I think this call pattern is correct but the language mode doesn't end up being changed

andrewbranch commented 2 years ago

@mjbvz pretty sure formatting is done on the syntax server. Would you possibly need to duplicate the updateOpen call between both the semantic and syntax servers? I could be way off here, not really sure how that coordination is supposed to work.

mjbvz commented 2 years ago

Possibly similar issue : #42613

@andrewbranch Oh yes good point, here's the syntax server logs:

``` Info 0 [16:13:28.969] Starting TS Server Info 1 [16:13:28.970] Version: 4.5.0-dev.20210929 Info 2 [16:13:28.970] Arguments: /Applications/Visual Studio Code - Insiders.app/Contents/Frameworks/Code - Insiders Helper (Renderer).app/Contents/MacOS/Code - Insiders Helper (Renderer) /Users/matb/projects/san/node_modules/typescript/lib/tsserver.js --serverMode partialSemantic --useInferredProjectPerProjectRoot --disableAutomaticTypingAcquisition --cancellationPipeName /var/folders/5d/nlg411_s0570wmycdwkgqrf40000gn/T/vscode-typescript501/9943632dca6c92c5d13f/tscancellation-64027595d92ad9d605d1.tmp* --logVerbosity verbose --logFile /Users/matb/Library/Application Support/Code - Insiders/logs/20211006T113239/exthost3/vscode.typescript-language-features/tsserver-log-mqDrqB/tsserver.log --globalPlugins typescript-vscode-sh-plugin,@vsintellicode/typescript-intellicode-plugin --pluginProbeLocations /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features,/Users/matb/.vscode-insiders/extensions/visualstudioexptteam.vscodeintellicode-1.2.14 --locale en --noGetErrOnBackgroundUpdate --validateDefaultNpmLocation Info 3 [16:13:28.970] Platform: darwin NodeVersion: 14 CaseSensitive: false Info 4 [16:13:28.970] ServerMode: 1 syntaxOnly: false hasUnknownServerMode: undefined Info 5 [16:13:28.977] request: {"seq":0,"type":"request","command":"configure","arguments":{"hostInfo":"vscode","preferences":{"providePrefixAndSuffixTextForRename":true,"allowRenameOfImportPath":true,"includePackageJsonAutoImports":"auto"},"watchOptions":{"watchFile":"useFsEvents"}}} Info 6 [16:13:28.977] Host information vscode Info 7 [16:13:28.977] Host watch options changed to {"watchFile":4}, it will be take effect for next watches. Info 8 [16:13:28.978] response: {"seq":0,"type":"response","command":"configure","request_seq":0,"success":true} Perf 9 [16:13:28.978] 0::configure: async elapsed time (in milliseconds) 1.1159 Info 10 [16:13:28.978] request: {"seq":1,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":[],"openFiles":[{"file":"^/untitled/Untitled-1","fileContent":"Promise.resolve(42)","projectRootPath":"/Users/matb/projects/san","scriptKindName":"JS"}]}} Info 11 [16:13:28.981] Loading global plugin typescript-vscode-sh-plugin Info 12 [16:13:28.981] Enabling plugin typescript-vscode-sh-plugin from candidate paths: /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features,/Users/matb/.vscode-insiders/extensions/visualstudioexptteam.vscodeintellicode-1.2.14,/Users/matb/projects/san/node_modules/typescript/lib/tsserver.js/../../.. Info 13 [16:13:28.981] Loading typescript-vscode-sh-plugin from /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features (resolved to /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features/node_modules) Info 14 [16:13:28.990] typescript-vscode-sh-plugin active for version < 4.2 and JS/JSX files. Current version : 4.5.0-dev.20210929 Info 15 [16:13:29.007] Intercepting getEncodedSemanticClassifications and getEncodedSyntacticClassifications. Info 16 [16:13:29.007] Plugin validation succeeded Info 17 [16:13:29.007] Loading global plugin @vsintellicode/typescript-intellicode-plugin Info 18 [16:13:29.007] Enabling plugin @vsintellicode/typescript-intellicode-plugin from candidate paths: /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features,/Users/matb/.vscode-insiders/extensions/visualstudioexptteam.vscodeintellicode-1.2.14,/Users/matb/projects/san/node_modules/typescript/lib/tsserver.js/../../.. Info 19 [16:13:29.008] Loading @vsintellicode/typescript-intellicode-plugin from /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features (resolved to /Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/typescript-language-features/node_modules) Info 20 [16:13:29.022] Loading @vsintellicode/typescript-intellicode-plugin from /Users/matb/.vscode-insiders/extensions/visualstudioexptteam.vscodeintellicode-1.2.14 (resolved to /Users/matb/.vscode-insiders/extensions/visualstudioexptteam.vscodeintellicode-1.2.14/node_modules) Info 21 [16:13:29.030] IntelliCode plugin: starting configuration: {"name":"@vsintellicode/typescript-intellicode-plugin","global":true} Info 22 [16:13:29.030] Plugin validation succeeded Info 23 [16:13:29.030] Starting updateGraphWorker: Project: /dev/null/inferredProject1* Info 24 [16:13:29.647] Finishing updateGraphWorker: Project: /dev/null/inferredProject1* Version: 1 structureChanged: true structureIsReused:: Not Elapsed: 616.7657300233841ms Info 25 [16:13:29.648] Project '/dev/null/inferredProject1*' (Inferred) Info 26 [16:13:29.649] Files (6) /Users/matb/projects/san/node_modules/typescript/lib/lib.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.es5.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.dom.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.webworker.importscripts.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.scripthost.d.ts ^/untitled/Untitled-1 node_modules/typescript/lib/lib.d.ts Default library for target 'es5' node_modules/typescript/lib/lib.es5.d.ts Library referenced via 'es5' from file 'node_modules/typescript/lib/lib.d.ts' node_modules/typescript/lib/lib.dom.d.ts Library referenced via 'dom' from file 'node_modules/typescript/lib/lib.d.ts' node_modules/typescript/lib/lib.webworker.importscripts.d.ts Library referenced via 'webworker.importscripts' from file 'node_modules/typescript/lib/lib.d.ts' node_modules/typescript/lib/lib.scripthost.d.ts Library referenced via 'scripthost' from file 'node_modules/typescript/lib/lib.d.ts' ^/untitled/Untitled-1 Root file specified for compilation Info 27 [16:13:29.649] ----------------------------------------------- Info 28 [16:13:29.649] Project '/dev/null/inferredProject1*' (Inferred) Info 28 [16:13:29.649] Files (6) Info 28 [16:13:29.649] ----------------------------------------------- Info 28 [16:13:29.649] Open files: Info 28 [16:13:29.649] FileName: ^/untitled/Untitled-1 ProjectRootPath: /Users/matb/projects/san Info 28 [16:13:29.649] Projects: /dev/null/inferredProject1* Perf 28 [16:13:29.649] 1::updateOpen: elapsed time (in milliseconds) 670.8291 Info 29 [16:13:29.649] response: {"seq":0,"type":"response","command":"updateOpen","request_seq":1,"success":true,"performanceData":{"updateGraphDurationMs":616.7657300233841},"body":true} Info 30 [16:13:30.187] request: {"seq":2,"type":"request","command":"configure","arguments":{"file":"^/untitled/Untitled-1","formatOptions":{"tabSize":4,"indentSize":4,"convertTabsToSpaces":true,"newLineCharacter":"\n","insertSpaceAfterCommaDelimiter":true,"insertSpaceAfterConstructor":false,"insertSpaceAfterSemicolonInForStatements":true,"insertSpaceBeforeAndAfterBinaryOperators":true,"insertSpaceAfterKeywordsInControlFlowStatements":true,"insertSpaceAfterFunctionKeywordForAnonymousFunctions":true,"insertSpaceBeforeFunctionParenthesis":false,"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis":false,"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets":false,"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces":true,"insertSpaceAfterOpeningAndBeforeClosingEmptyBraces":true,"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces":false,"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces":false,"placeOpenBraceOnNewLineForFunctions":false,"placeOpenBraceOnNewLineForControlBlocks":false,"semicolons":"ignore"},"preferences":{"quotePreference":"single","importModuleSpecifierEnding":"auto","jsxAttributeCompletionStyle":"auto","allowTextChangesInNewFiles":false,"providePrefixAndSuffixTextForRename":true,"allowRenameOfImportPath":true,"includeAutomaticOptionalChainCompletions":true,"provideRefactorNotApplicableReason":true,"generateReturnInDocTemplate":true,"includeCompletionsForImportStatements":true,"includeCompletionsWithSnippetText":true,"allowIncompleteCompletions":true,"displayPartsForJSDoc":true,"includeInlayParameterNameHints":"none","includeInlayParameterNameHintsWhenArgumentMatchesName":false,"includeInlayFunctionParameterTypeHints":false,"includeInlayVariableTypeHints":false,"includeInlayPropertyDeclarationTypeHints":true,"includeInlayFunctionLikeReturnTypeHints":true,"includeInlayEnumMemberValueHints":false}}} Info 31 [16:13:30.187] Host configuration update for file ^/untitled/Untitled-1 Info 32 [16:13:30.187] response: {"seq":0,"type":"response","command":"configure","request_seq":2,"success":true} Perf 33 [16:13:30.187] 2::configure: async elapsed time (in milliseconds) 0.5521 Info 34 [16:13:30.190] request: {"seq":3,"type":"request","command":"getOutliningSpans","arguments":{"file":"^/untitled/Untitled-1"}} Perf 35 [16:13:30.192] 3::getOutliningSpans: elapsed time (in milliseconds) 1.8784 Info 36 [16:13:30.192] response: {"seq":0,"type":"response","command":"getOutliningSpans","request_seq":3,"success":true,"body":[]} Info 37 [16:13:30.205] request: {"seq":4,"type":"request","command":"navtree","arguments":{"file":"^/untitled/Untitled-1"}} Perf 38 [16:13:30.207] 4::navtree: elapsed time (in milliseconds) 1.4204 Info 39 [16:13:30.207] response: {"seq":0,"type":"response","command":"navtree","request_seq":4,"success":true,"body":{"text":"","kind":"script","kindModifiers":"","spans":[{"start":{"line":1,"offset":1},"end":{"line":1,"offset":28}}]}} Info 40 [16:13:33.957] request: {"seq":5,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":["^/untitled/Untitled-1"],"openFiles":[]}} Info 41 [16:13:33.958] Project '/dev/null/inferredProject1*' (Inferred) Info 41 [16:13:33.958] Files (6) Info 41 [16:13:33.958] ----------------------------------------------- Info 41 [16:13:33.958] Open files: Perf 41 [16:13:33.958] 5::updateOpen: elapsed time (in milliseconds) 0.9991 Info 42 [16:13:33.958] response: {"seq":0,"type":"response","command":"updateOpen","request_seq":5,"success":true,"body":true} Info 43 [16:13:33.964] request: {"seq":6,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":[],"openFiles":[{"file":"^/untitled/Untitled-1","fileContent":"Promise.resolve(42)","projectRootPath":"/Users/matb/projects/san","scriptKindName":"TS"}]}} Info 44 [16:13:33.964] Starting updateGraphWorker: Project: /dev/null/inferredProject1* Info 45 [16:13:34.239] Finishing updateGraphWorker: Project: /dev/null/inferredProject1* Version: 2 structureChanged: true structureIsReused:: Not Elapsed: 275.16661298274994ms Info 46 [16:13:34.239] Project '/dev/null/inferredProject1*' (Inferred) Info 47 [16:13:34.239] Files (6) /Users/matb/projects/san/node_modules/typescript/lib/lib.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.es5.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.dom.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.webworker.importscripts.d.ts /Users/matb/projects/san/node_modules/typescript/lib/lib.scripthost.d.ts ^/untitled/Untitled-1 node_modules/typescript/lib/lib.d.ts Default library for target 'es5' node_modules/typescript/lib/lib.es5.d.ts Library referenced via 'es5' from file 'node_modules/typescript/lib/lib.d.ts' node_modules/typescript/lib/lib.dom.d.ts Library referenced via 'dom' from file 'node_modules/typescript/lib/lib.d.ts' node_modules/typescript/lib/lib.webworker.importscripts.d.ts Library referenced via 'webworker.importscripts' from file 'node_modules/typescript/lib/lib.d.ts' node_modules/typescript/lib/lib.scripthost.d.ts Library referenced via 'scripthost' from file 'node_modules/typescript/lib/lib.d.ts' ^/untitled/Untitled-1 Root file specified for compilation Info 48 [16:13:34.239] ----------------------------------------------- Info 49 [16:13:34.239] Project '/dev/null/inferredProject1*' (Inferred) Info 49 [16:13:34.239] Files (6) Info 49 [16:13:34.239] ----------------------------------------------- Info 49 [16:13:34.239] Open files: Info 49 [16:13:34.239] FileName: ^/untitled/Untitled-1 ProjectRootPath: /Users/matb/projects/san Info 49 [16:13:34.239] Projects: /dev/null/inferredProject1* Perf 49 [16:13:34.239] 6::updateOpen: elapsed time (in milliseconds) 275.9492 Info 50 [16:13:34.240] response: {"seq":0,"type":"response","command":"updateOpen","request_seq":6,"success":true,"performanceData":{"updateGraphDurationMs":275.16661298274994},"body":true} Info 51 [16:13:34.240] request: {"seq":7,"type":"request","command":"configure","arguments":{"file":"^/untitled/Untitled-1","formatOptions":{"tabSize":4,"indentSize":4,"convertTabsToSpaces":true,"newLineCharacter":"\n","insertSpaceAfterCommaDelimiter":true,"insertSpaceAfterConstructor":false,"insertSpaceAfterSemicolonInForStatements":true,"insertSpaceBeforeAndAfterBinaryOperators":true,"insertSpaceAfterKeywordsInControlFlowStatements":true,"insertSpaceAfterFunctionKeywordForAnonymousFunctions":true,"insertSpaceBeforeFunctionParenthesis":false,"insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis":false,"insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets":false,"insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces":true,"insertSpaceAfterOpeningAndBeforeClosingEmptyBraces":true,"insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces":false,"insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces":false,"insertSpaceAfterTypeAssertion":false,"placeOpenBraceOnNewLineForFunctions":false,"placeOpenBraceOnNewLineForControlBlocks":false,"semicolons":"ignore"},"preferences":{"quotePreference":"auto","importModuleSpecifierEnding":"auto","jsxAttributeCompletionStyle":"auto","allowTextChangesInNewFiles":false,"providePrefixAndSuffixTextForRename":true,"allowRenameOfImportPath":true,"includeAutomaticOptionalChainCompletions":true,"provideRefactorNotApplicableReason":true,"generateReturnInDocTemplate":false,"includeCompletionsForImportStatements":true,"includeCompletionsWithSnippetText":true,"allowIncompleteCompletions":true,"displayPartsForJSDoc":true,"includeInlayParameterNameHints":"all","includeInlayParameterNameHintsWhenArgumentMatchesName":false,"includeInlayFunctionParameterTypeHints":false,"includeInlayVariableTypeHints":false,"includeInlayPropertyDeclarationTypeHints":false,"includeInlayFunctionLikeReturnTypeHints":true,"includeInlayEnumMemberValueHints":false}}} Info 52 [16:13:34.240] Host configuration update for file ^/untitled/Untitled-1 Info 53 [16:13:34.240] response: {"seq":0,"type":"response","command":"configure","request_seq":7,"success":true} Perf 54 [16:13:34.240] 7::configure: async elapsed time (in milliseconds) 0.2564 Info 55 [16:13:34.301] request: {"seq":8,"type":"request","command":"getOutliningSpans","arguments":{"file":"^/untitled/Untitled-1"}} Perf 56 [16:13:34.302] 8::getOutliningSpans: elapsed time (in milliseconds) 0.6615 Info 57 [16:13:34.302] response: {"seq":0,"type":"response","command":"getOutliningSpans","request_seq":8,"success":true,"body":[]} Info 58 [16:13:34.686] request: {"seq":9,"type":"request","command":"format","arguments":{"file":"^/untitled/Untitled-1","line":1,"offset":1,"endLine":1,"endOffset":28}} Perf 59 [16:13:34.705] 9::format: elapsed time (in milliseconds) 18.7167 Info 60 [16:13:34.705] response: {"seq":0,"type":"response","command":"format","request_seq":9,"success":true,"body":[{"start":{"line":1,"offset":16},"end":{"line":1,"offset":16},"newText":" "},{"start":{"line":1,"offset":17},"end":{"line":1,"offset":17},"newText":" "},{"start":{"line":1,"offset":23},"end":{"line":1,"offset":23},"newText":" "},{"start":{"line":1,"offset":24},"end":{"line":1,"offset":24},"newText":" "}]} Info 61 [16:13:34.811] request: {"seq":10,"type":"request","command":"updateOpen","arguments":{"changedFiles":[{"fileName":"^/untitled/Untitled-1","textChanges":[{"newText":" ","start":{"line":1,"offset":16},"end":{"line":1,"offset":16}},{"newText":" ","start":{"line":1,"offset":17},"end":{"line":1,"offset":17}},{"newText":" ","start":{"line":1,"offset":23},"end":{"line":1,"offset":23}},{"newText":" ","start":{"line":1,"offset":24},"end":{"line":1,"offset":24}}]}],"closedFiles":[],"openFiles":[]}} Perf 62 [16:13:34.812] 10::updateOpen: elapsed time (in milliseconds) 0.8733 Info 63 [16:13:34.812] response: {"seq":0,"type":"response","command":"updateOpen","request_seq":10,"success":true,"body":true} Info 64 [16:13:34.911] request: {"seq":11,"type":"request","command":"getOutliningSpans","arguments":{"file":"^/untitled/Untitled-1"}} Perf 65 [16:13:34.912] 11::getOutliningSpans: elapsed time (in milliseconds) 1.4451 Info 66 [16:13:34.912] response: {"seq":0,"type":"response","command":"getOutliningSpans","request_seq":11,"success":true,"body":[]} ```

When the language mode changes, we do try closing and then re-opening the file on the syntax server as well