Skip to content

compiler crashes with allowJs and dynamic import #28014

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

Closed
MirKml opened this issue Oct 20, 2018 · 4 comments · Fixed by #28133
Closed

compiler crashes with allowJs and dynamic import #28014

MirKml opened this issue Oct 20, 2018 · 4 comments · Fixed by #28133
Assignees
Labels
Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Fixed A PR has been merged for this issue

Comments

@MirKml
Copy link

MirKml commented Oct 20, 2018

TypeScript Version: 3.1.3 2.9.2

Code
tsjs-crash.zip

Minimal example is in the attached zip package. Extract it somewhere, go to main directory with tsconfig.json and

install
npm i
compile
node node_modules/typescript/bin/tsc

Expected behavior:
everything works fine - preferred, because with TS 2.8.x there are no errors,
or some reasonable error message for problem solving

Actual behavior:
compiler crashes with

 E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:69365
                throw e;
                ^
TypeError: Cannot read property 'parent' of undefined
    at Object.getDeclarationOfExpando (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:7328:19)
    at getTypeOfFuncClassEnumModule (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:28883:40)
    at getTypeOfSymbol (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:28984:24)
    at checkImportCallExpression (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:40833:96)
    at checkExpressionWorker (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:42356:32)
    at checkExpression (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:42301:42)
    at checkAwaitExpression (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:41525:31)
    at checkExpressionWorker (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:42383:28)
    at checkExpression (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:42301:42)
    at checkExpressionCached (E:\projects\vscode-crash\node_modules\typescript\lib\tsc.js:42182:38)

Problem is with the imported javascript in TestComponent.tsx with

async exportToXlsx(exportSelected: boolean = false) {
    const xlsx = await import('./js-xlsx/xlsx');
}

Problem is only with Typescript 2.9, 3.x, Typescript 2.8 compiles without any errors.

I tried to exclude javascript with tsconfig.json and jsconfig.json - see exclude properties in these files, but nothing works. Javascript is always compiled. So another question - is it possible exclude javascripts in similar situations?

@ghost ghost added the Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output label Oct 22, 2018
@ghost ghost assigned sandersn Oct 22, 2018
@sandersn sandersn added this to the TypeScript 3.2 milestone Oct 22, 2018
@sandersn
Copy link
Member

sandersn commented Oct 24, 2018

The immediate cause is the improved handling of module.exports in Typescript 2.9. The compiler is trying to figure out what the exports of ./js-xlsx/xlsx are, but somehow creates a defective export= symbol from the line module.exports = CFB.

Workaround

Any time you import a file, the compiler will read that file. Excludes will not help you. For a JS file that you do not want to compile, but nonetheless want to import, the easiest workaround is to put a d.ts file next to it. Here's the one I created:

// @Filename: components/js-xlsx/xlsx.d.ts
declare var dummy: any;
export = dummy;

This just exports a dummy variable with the type any so that the compiler doesn't spend any time analysing xlsx.js to figure out what it actually exports.

Edit: You are free to make the type of dummy as accurate as you want; it will probably be more accurate than the type the compiler produces after this bug is fixed.

@sandersn
Copy link
Member

Here's a small repro:

// @Filename: mod1.js
exports.version = 1
module.exports = NotDefined
// @Filename: test.js
import('./test')

@MirKml
Copy link
Author

MirKml commented Oct 25, 2018

Thank you for analysis and workaround. I will try it.

I found another possible workaround. I made another node_modules directory and copy the whole directory js-xlsx inside it. So the directory tree is components/node_modules/js-xlsx`/xlsx.js.
Dynamic import will be import('js-xlsx/xlsx).

It seems that because of module resolution or something like that, the compiler ignores the node_modules even of dynamic import. Final app is build via webpack and babel and it seems it has no problem with it.
What do you think about that?

I definitely waiting for the final typescript fix, thank you.

@sandersn sandersn added the Fixed A PR has been merged for this issue label Oct 25, 2018
@sandersn
Copy link
Member

Duplicating part of the source tree seems hackier than dropping a d.ts in beside the .js file.

Anyway, the fix is simple so it should be in typescript@next in the next day or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants