Skip to content

tsc 2.5.1 crashes with error: TypeError: Cannot read property '240' of undefined. #17982

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
svstanev opened this issue Aug 23, 2017 · 6 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@svstanev
Copy link

The problem appears to be invoking the getDeclarationSpaces (tsc.js:33049) function with a node of kind 240 (NamespaceImport). For some reason the ts.SyntaxKind enum is missing thus the error. In this case (d.kind === 240) the default switch clause is triggered which either way appears to be an error. The same source code compiles just fine with tsc v2.4.2 though. At first glance the getDeclarationSpaces function seems to be changed in the latest version while the node its been called with appears pretty much the same.

As of now I have no clue which part of my source code is related to problem. What I've found so far is that the error appears while processing a .ts source file that has these two imports:

...

import * as Header from './header'
import * as Signal from './signal'

...

and the node in question (NamespaceImport) has a symbol named 'Signal' so I can only guess that the issue may have be related to the 2nd line from the snippet above.

TypeScript Version: 2.5.1

Code

tsc.js v2.5.1:

            function getDeclarationSpaces(d) {
                switch (d.kind) {
                    case 230:
                    case 231:
                        return 2;
                    case 233:
                        return ts.isAmbientModule(d) || ts.getModuleInstanceState(d) !== 0
                            ? 4 | 1
                            : 4;
                    case 229:
                    case 232:
                        return 2 | 1;
                    case 237:
                        var result_3 = 0;
                        var target = resolveAlias(getSymbolOfNode(d));
                        ts.forEach(target.declarations, function (d) { result_3 |= getDeclarationSpaces(d); });
                        return result_3;
                    case 226:
                    case 176:
                    case 228:
                    case 242:
                        return 1;
                    default:
                        ts.Debug.fail(ts.SyntaxKind[d.kind]); // TypeError: Cannot read property '240' of undefined
                }
            }

Expected behavior:

Actual behavior:

TypeError: Cannot read property '240' of undefined
    at getDeclarationSpaces (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:33072:52)
    at checkExportsOnMergedDeclarations (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:33019:41)
    at checkInterfaceDeclaration (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:34711:17)
    at checkSourceElement (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:35450:28)
    at Object.forEach (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:275:30)
    at checkSourceFileWorker (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:35517:20)
    at checkSourceFile (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:35502:13)
    at getDiagnosticsWorker (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:35555:17)
    at Object.getDiagnostics (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:35544:24)
    at /Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56140:85
    at runWithCancellationToken (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56118:24)
    at getSemanticDiagnosticsForFileNoCache (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56132:20)
    at getAndCacheDiagnostics (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56340:26)
    at getSemanticDiagnosticsForFile (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56129:20)
    at /Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56086:24
    at Object.flatMap (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:507:25)
    at getDiagnosticsHelper (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56082:56)
    at Object.getSemanticDiagnostics (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:56093:20)
    at compileProgram (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:59083:43)
    at compile (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:59040:26)
    at performCompilation (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:58929:33)
    at Object.executeCommandLine (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:58872:9)
    at Object.<anonymous> (/Users/svstanev/work/myproj/node_modules/typescript/lib/tsc.js:59230:4)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/svstanev/work/myproj/node_modules/typescript/bin/tsc:2:1)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)
    at bootstrap_node.js:540:3
@mhegazy
Copy link
Contributor

mhegazy commented Aug 23, 2017

can you share a sample that causes this to happen?

@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 23, 2017
@mhegazy mhegazy added this to the TypeScript 2.5.2 milestone Aug 23, 2017
@weswigham
Copy link
Member

weswigham commented Aug 23, 2017

So I can guess that this change is due to changes made in #16766, however I am unsure what, exactly to do to reproduce this. As far as I can tell, only difference is that when checkExportsOnMergedDeclarations used SymbolFlags.Export instead of symbol.exportSymbol to check for export presence, SymbolFlags.Export implicitly excluded anything created from a SymbolFlags.Signature or SymbolFlags.Constructor, as that pair was excluded from being mapped to a SymbolFlags.ExportValue, SymbolFlags.ExportType, or SymbolFlags.ExportNamespace since neither is a member of SymbolFlags.Value, SymbolFlags.Type, or SymbolFlags.Namespace.

@svstanev, if you can't get a repro (although that'd be really helpful), could you try this branch from this PR (or do you need a built version)? I certainly can't guarantee that we won't regress this again, if that does fix it, without some kind of reproduction in our test suite, though.

@svstanev
Copy link
Author

I finally managed to reproduce the problem: a module (b.ts) is imported into a variable (B) and then an interface named B is declared. It definitely looks wrong to redefine/reuse the identifier B though 2.4.2 has no problem compiling the code.

Either way it would be great if the compiler can handle this case appropriately -- report the exact error (name already exists) or compile the code as in the previous version.

@weswigham, I hope you can better explain what's going on and how should tsc handle cases like this.

Here is how to reproduce the problem:

a.ts

import * as B from './b'

export interface B {
    x: any
}

b.ts (the content of this module is not important - you just need a second module to import into module a)

export const zzz = 123

@svstanev
Copy link
Author

A few more things:

  • renaming either the import variable or the interface makes the code to compile just fine in 2.5.1
  • VS Code 1.15.1 appears to distinguish the interface from the variable and properly shows the usages though both have the same name; the project is configured to use TS 2.5.1 (local)

IMO, it's clearly an ambiguous use of the same name (as opposite of a partial definition of one thing like the partial classes in C# for example) and the compiler should complain about it (compile time error). Either way the language service should be alined with the compiler behavior.

I hope this helps.

@Katona
Copy link

Katona commented Sep 8, 2017

Hi!

I have similar problem (with '239' instead of '240') is there a way to extract some debug info from the compiler to track down the problematic files?

@svstanev
Copy link
Author

Hi @Katona,

239 appears to be a node of kind ImportClause (see node_modules/typescript/lib/typescript.d.ts for details) so I guess there is something wrong with the imports in one of your source files. In order to find where exactly the problem is you may try debugging the TS compiler like this:

node --inspect node_modules/.bin/tsc -p .

assuming you're in the project's root folder (where tsconfig.json is) and typescript is installed as a local dependency.

For more info:

HTH

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants