Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createProgarm with @typescript/vfs fails for target ES2021 #47273

Open
dpilch opened this issue Dec 30, 2021 · 3 comments
Open

createProgarm with @typescript/vfs fails for target ES2021 #47273

dpilch opened this issue Dec 30, 2021 · 3 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@dpilch
Copy link

dpilch commented Dec 30, 2021

Bug Report

createProgram fails with internal error when using @typescript/vfs and setting compiler option target to ES2021. This issue only happens for ES2021, all other targets work fine.

🔎 Search Terms

ES2021, createProgram, @typescript/vfs, vfs

🕗 Version & Regression Information

  • This changed between versions 4.4.x and 4.5.x

Full version information:

⏯ Playground Link

Reproduction requires import of typescript and @typescript/vfs. Playground doesn't appear to support this.

💻 Code

import ts, { createProgram, ScriptTarget } from 'typescript';
import { createDefaultMapFromNodeModules, createSystem, createVirtualCompilerHost } from '@typescript/vfs';

const compilerOptions = { target: ScriptTarget.ES2021 };
const code = 'export default "foo"';

const fsMap = createDefaultMapFromNodeModules(compilerOptions, ts);
fsMap.set('index.tsx', code);
const host = createVirtualCompilerHost(createSystem(fsMap), compilerOptions, ts);

const program = createProgram({
  rootNames: [...fsMap.keys()],
  options: compilerOptions,
  host: host.compilerHost,
});

🙁 Actual behavior

Above snippet fails with following error:

/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10460
        scan: while (pos >= 0 && pos < text.length) {
                                            ^
TypeError: Cannot read property 'length' of undefined
    at iterateCommentRanges (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10460:45)
    at reduceEachLeadingCommentRange (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10551:16)
    at Object.getLeadingCommentRanges (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:10566:16)
    at Object.getJSDocCommentRanges (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:15263:16)
    at addJSDocComment (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30878:42)
    at parseSourceFileWorker (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30851:34)
    at Object.parseSourceFile (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30683:26)
    at Object.createSourceFile (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:30481:29)
    at Object.getSourceFile (/local/home/dppilche/typescript-vfs-bug/node_modules/@typescript/vfs/src/index.ts:504:16)
    at findSourceFileWorker (/local/home/dppilche/typescript-vfs-bug/node_modules/typescript/lib/typescript.js:115369:29)

🙂 Expected behavior

createProgram does not crash.

@suin
Copy link

suin commented Jan 10, 2022

This issue occurs because lib.es2021.intl.d.ts is missing in knownLibFilesForCompilerOptions of vfs.

https://github.com/microsoft/TypeScript-Website/blob/c1050864dbc359cfa957aa8b52db624cd379ddcf/packages/typescript-vfs/src/index.ts#L154-L160

We can work around this problem by manually adding lib.es2021.intl.d.ts to the fsMap.

import ts, { createProgram, ScriptTarget } from 'typescript';
import { createDefaultMapFromNodeModules, createSystem, createVirtualCompilerHost } from '@typescript/vfs';

const compilerOptions = { target: ScriptTarget.ES2021 };
const code = 'export default "foo"';

const fsMap = createDefaultMapFromNodeModules(compilerOptions, ts);
+fsMap.set(
+  "/lib.es2021.intl.d.ts",
+  fs.readFileSync(require.resolve("typescript"), "utf8") +
+    "/lib.es2021.intl.d.ts"
+);
fsMap.set('index.tsx', code);
const host = createVirtualCompilerHost(createSystem(fsMap), compilerOptions, ts);

const program = createProgram({
  rootNames: [...fsMap.keys()],
  options: compilerOptions,
  host: host.compilerHost,
});

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jan 11, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 11, 2022
@dpilch
Copy link
Author

dpilch commented Jan 18, 2022

Thanks, this suggestion worked! I just needed to wrap require.resolve('typescript') with path.dirname as done here: https://github.com/microsoft/TypeScript-Website/blob/fa4d8d29eaa2046a9c68234b3728c9cb69078045/packages/typescript-vfs/src/index.ts#L200

@Matsuuu
Copy link

Matsuuu commented Apr 25, 2023

Submitted a relating issue to this to the vfs project microsoft/TypeScript-Website#2801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

4 participants