Title: createProgram corrupts updated SourceFile · Issue #26166 · microsoft/TypeScript · GitHub
Open Graph Title: createProgram corrupts updated SourceFile · Issue #26166 · microsoft/TypeScript
X Title: createProgram corrupts updated SourceFile · Issue #26166 · microsoft/TypeScript
Description: TypeScript Version: 3.1.0-dev.20180801 Search Terms: Code For the last two days I'm investigating a crash of my linter when fixing files, updating existing SourceFiles and creating a Program from these SourceFiles: fimbullinter/wotan#361...
Open Graph Description: TypeScript Version: 3.1.0-dev.20180801 Search Terms: Code For the last two days I'm investigating a crash of my linter when fixing files, updating existing SourceFiles and creating a Program from t...
X Description: TypeScript Version: 3.1.0-dev.20180801 Search Terms: Code For the last two days I'm investigating a crash of my linter when fixing files, updating existing SourceFiles and creating a Program fr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26166
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"createProgram corrupts updated SourceFile","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.1.0-dev.20180801\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n\r\n**Code**\r\n\r\nFor the last two days I'm investigating a crash of my linter when fixing files, updating existing SourceFiles and creating a Program from these SourceFiles: https://github.com/fimbullinter/wotan/issues/361\r\n\r\nFirst you need 2 files that are processed by the repro script:\r\n\r\ntsconfig.json\r\n```json\r\n{\r\n \"include\": [\r\n \"test.ts\"\r\n ]\r\n}\r\n```\r\n\r\ntest.ts\r\n```ts\r\nexport class Rule {\r\n private getPropertyInfo() {\r\n return\r\n }\r\n}\r\n\r\n(name) => b;\r\n\r\n```\r\n\r\nNow add the following script (using `__dirname` in the script assumes it's in the same directory as the above files, that can be changed though):\r\n\r\nrepro.js\r\n```ts\r\n\"use strict\";\r\n\r\nconst ts = require(\"typescript\");\r\nconst {strictEqual} = require(\"assert\");\r\n\r\nconst config = ts.parseJsonConfigFileContent(ts.readJsonConfigFile(__dirname + '/tsconfig.json', ts.sys.readFile), ts.sys, __dirname, undefined, __dirname + '/tsconfig.json');\r\nlet program = ts.createProgram(config.fileNames, config.options, ts.createCompilerHost(config.options, true));\r\n\r\nlet sourceFile = program.getSourceFile(__dirname + '/test.ts');\r\n// add a semicolon after `return`\r\nconst newCode = sourceFile.text.slice(0, 66) + ';' + sourceFile.text.slice(66);\r\nsourceFile = ts.updateSourceFile(sourceFile, newCode, { newLength: 1, span: { start: 66, length: 0 } });\r\nstrictEqual(sourceFile.statements[1].getSourceFile(), sourceFile, 'parent reference should be updated')\r\n\r\nconst originalHost = ts.createCompilerHost(config.options, true);\r\nconst host = {\r\n ...originalHost,\r\n getSourceFile(path, version) {\r\n if (path === sourceFile.fileName)\r\n return sourceFile;\r\n return originalHost.getSourceFile(path, version);\r\n },\r\n};\r\nprogram = ts.createProgram(program.getRootFileNames(), program.getCompilerOptions(), host, program);\r\nstrictEqual(program.getSourceFile(__dirname + '/test.ts'), sourceFile, 'new Program should contain the updated SourceFile');\r\nstrictEqual(sourceFile.statements[1].getSourceFile(), sourceFile, 'parent reference should still be the same');\r\n```\r\n\r\nExecute `node repro.js`\r\n\r\n**Expected behavior:**\r\n\r\nAll assertions pass\r\n\r\n**Actual behavior:**\r\n\r\nLast assertion fails.\r\nThere is no error if I use `ts.createSourceFile` instead of `ts.updateSourceFile`\r\n","author":{"url":"https://github.com/ajafff","@type":"Person","name":"ajafff"},"datePublished":"2018-08-02T19:58:47Z","url":"https://github.com/microsoft/TypeScript/issues/26166"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d5291aae-d6ad-21a3-442e-e02a96c64809 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 96C8:215B92:1E10428:27FC97E:6B016FF9 |
| html-safe-nonce | bc28180c4e26aff6a5db231a17f4c46c5a966c4e1e5a6bccc03b5a22745126f1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NkM4OjIxNUI5MjoxRTEwNDI4OjI3RkM5N0U6NkIwMTZGRjkiLCJ2aXNpdG9yX2lkIjoiMjMwNTU0MzEzNjM5MjM0MzU0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 62f15e1304361d3ebee17629363cc0db08fe00fbd9cd3cdfb5dbe57972534c61 |
| hovercard-subject-tag | issue:347153172 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/26166/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9dd2a6d2461ce50d82b399c599d9f2a7f99336f54b00b4b5b998a89f40339ba4/microsoft/TypeScript/issues/26166 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9dd2a6d2461ce50d82b399c599d9f2a7f99336f54b00b4b5b998a89f40339ba4/microsoft/TypeScript/issues/26166 |
| og:image:alt | TypeScript Version: 3.1.0-dev.20180801 Search Terms: Code For the last two days I'm investigating a crash of my linter when fixing files, updating existing SourceFiles and creating a Program from t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ajafff |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width