dsherret / ts-type-info

TypeScript AST and code generator [Deprecated]
MIT License
94 stars 8 forks source link

Module Resolution Failing #205

Closed brad-jones closed 7 years ago

brad-jones commented 7 years ago

Using this package out of the box, without any custom compiler options I get the following error:

Failed getting info from "Foo".
Detail: Cannot read property 'getSymbol' of undefined

On inspection of the stack trace, it is failing where it hits the class that "Foo" extends because it has not been able to resolve the imported parent class.

The TsNode contains:

resolvedModules: 
      { 'app/Container': undefined,
        'app/Contracts/IFoo': undefined,
        'app/Contracts/IBar': undefined }

I then attempted to provide my compiler options from my tsconfig.json file.

As an aside it would seem like a nice idea to automatically pull in the compiler options from a tsconfig.json file if one can be found.

They look like this:

{
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "types": [
        "reflect-metadata"
    ],
    "baseUrl": ".",
    "paths": {
        "*": [
            "*",
            "app/*"
        ]
    }
}

NOTE: I cast the CompilerOptions to to get them passed through.

After I did this I then got the following exception:

/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45131
            var loader_1 = function (moduleName, containingFile) { return ts.resolveModuleName(moduleName, containingFile, options, host).resolvedModule; };
                                                                                                                                         ^
TypeError: Cannot read property 'resolvedModule' of undefined
    at loader_1 (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45131:138)
    at loadWithLocalCache (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45051:36)
    at resolveModuleNamesWorker (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45132:88)
    at processImportedModules (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45965:35)
    at findSourceFile (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45880:17)
    at processSourceFile (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45782:27)
    at processRootFile (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45686:13)
    at /home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45147:60
    at Object.forEach (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:1110:30)
    at Object.createProgram (/home/brad/Projects/typescript-playground/node_modules/typescript/lib/typescript.js:45147:16)

As you might have already worked out I have setup my environment to be able to require from the project root. This tsconfig.json works just fine in atom-typescript. ie: Atom resolves app/Contracts/IFoo without compliant. It would seem though that the baseUrl & paths properties are not being taken into to consideration when calling your reflection lib.

brad-jones commented 7 years ago

Further to this I modded the TsMain class and provided my own compiler host as per the example here: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#customizing-module-resolution

I then setup the resolveModuleNames function to resolve my project root'ed modules correctly. This seemed to work however I now have the following output:

[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: 
-----------------------------------------
Failed getting info from "BaseError".

Detail: Cannot read property 'getSymbol' of undefined

Stack: TypeError: Cannot read property 'getSymbol' of undefined
    at TypeCacheContainer.getFileName (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:41:28)
    at TypeCacheContainer.getCache (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:33:29)
    at TsCache.getType (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:20:45)
    at TsTypeNode.getTsTypeFromNode (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/TsTypeNode.js:107:29)
    at TsTypeNode.getTypeAtLocationByNode (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/TsTypeNode.js:101:25)
    at TsTypeNode.getType (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/TsTypeNode.js:75:21)
    at TsFactory.getTypeFromTypeNode (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/factories/TsFactory.js:96:79)
    at /home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/binders/ts/class/TsClassBinder.js:24:61
    at Array.map (native)
    at TsClassBinder.getExtendsTypes (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/binders/ts/class/TsClassBinder.js:24:14)
-----------------------------------------

[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword
[ts-type-info]: Unknown class child kind: AbstractKeyword

/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:41
        var symbol = tsType.getSymbol();
                           ^
TypeError: Cannot read property 'getSymbol' of undefined
    at TypeCacheContainer.getFileName (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:41:28)
    at TypeCacheContainer.getCache (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:33:29)
    at TsCache.getType (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/utils/TsCache.js:20:45)
    at TsTypeNode.getTsTypeFromNode (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/TsTypeNode.js:107:29)
    at TsTypeNode.getTypeAtLocationByNode (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/TsTypeNode.js:101:25)
    at TsTypeNode.getType (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/compiler/TsTypeNode.js:75:21)
    at TsFactory.getTypeFromTypeNode (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/factories/TsFactory.js:96:79)
    at /home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/binders/ts/class/TsClassBinder.js:24:61
    at Array.map (native)
    at TsClassBinder.getExtendsTypes (/home/brad/Projects/typescript-playground/node_modules/ts-type-info/dist/binders/ts/class/TsClassBinder.js:24:14)
dsherret commented 7 years ago

Sorry for the long reply, Brad. I was away, but now I'm back. Thanks for your feedback and for opening this issue. I opened up a couple of issues because of it.

I'll reduce some of the noise in the log messages and investigate the error you're having. The part of the code that's throwing that error actually won't exist in the next minor revision, but I'll take a look to try to figure out why it was occurring to see if I'll need to fix the new code.

brad-jones commented 7 years ago

Cheers @dsherret looking forward to the updates.

If I can be of any further assistance let me know.

dsherret commented 7 years ago

This should be fixed in 6.2. Let me know if my fix didn't work.

Note: The compiler api had some breaking changes that led me to implement #204 so hopefully it doesn't blow up. It's hard to account for every scenario.

By the way, I added #210 that allows you to pass in a custom compiler host.

brad-jones commented 7 years ago

@dsherret your a legend, works beautifully.

Now I just need to easily serialize the results of getInfoFromFiles so I can have fast reflection at runtime. Any progress on #123

dsherret commented 7 years ago

@brad-jones I'm working on #208, which will allow me to more easily do that. I'm separating the definitions, writing and code generation, from the typescript compiler part of this library. Not sure how long it will take because I'm doing a major internal code refactoring to clean up some of the mess and simplify a few things.