angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

AOT build error Cannot read property 'kind' of undefined

Open samalexander opened this issue 9 years ago • 63 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

Angular CLI: 1.5.0 Node: 8.5.0 OS: darwin x64 Angular: 5.0.1 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

@angular/cdk: 5.0.0-rc0 @angular/cli: 1.5.0 @angular/flex-layout: 2.0.0-beta.10-4905443 @angular/material: 5.0.0-rc0 @angular-devkit/build-optimizer: 0.0.32 @angular-devkit/core: 0.0.20 @angular-devkit/schematics: 0.0.35 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.0 @schematics/angular: 0.1.2 typescript: 2.4.2 webpack-bundle-analyzer: 2.9.1 webpack: 3.8.1

Repro steps.

rm -rf node_modules/ yarn add @angular/cli@latest -D ng build --aot

The log given by the failure.

ERROR in Error: TypeError: Cannot read property 'kind' of undefined at nodeCanBeDecorated (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:7805:35) at nodeIsDecorated (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:7825:16) at Object.nodeOrChildIsDecorated (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:7829:16) at isDecoratedClassElement (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51668:23) at isInstanceDecoratedClassElement (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51659:20) at Object.filter (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:1652:25) at getDecoratedClassElements (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51641:23) at generateClassElementDecorationExpressions (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51815:27) at addClassElementDecorationStatements (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51804:44) at visitClassDeclaration (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:51131:13) at visitTypeScript (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50972:28) at visitorWorker (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50785:24) at sourceElementVisitorWorker (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50817:28) at saveStateAndInvoke (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50738:27) at sourceElementVisitor (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:50799:20) at visitNodes (/Users/sam/Developer/ng-prime/node_modules/typescript/lib/typescript.js:49280:48)

Desired functionality.

AOT build should be successful

Mention any other details that might be useful.

The project built fine with AOT enabled with CLI v1.4.9

samalexander avatar Nov 10 '17 10:11 samalexander

@MohammedYaseen I'm not sure, but probably using a custom github repo as registry for the dependency typescript in package.json would work. See this for example.

Axure avatar Dec 01 '17 15:12 Axure

@Axure, Ok, thanks

MohammedYaseen avatar Dec 04 '17 13:12 MohammedYaseen

We are having the same problem and have been stuck on 1.5.0 because of it. In our case it happens even without arrow functions being passed to decorators

The cause in our case is some decorators being applied to properties that also have Angular decorators (usually @Input) applied to them. Everything works in JIT mode but AOT fails. Stepping through in the debugger we can see that the node's parent is gone and there is no null check to prevent it exploding (as mentioned above by another commenter).

We will probably end up removing the custom decorators anyway (can't rely on decorators at all), but until then, we are stuck or will have to hack Typescript.

Any news on whether this is being looked at? Would a PR to Typescript or the CLI be acceptable?

hccampos avatar Dec 05 '17 05:12 hccampos

@filipesilva @SanderElias here is a repro for this bug https://github.com/hccampos/angular-cli-repro-8434

hccampos avatar Dec 05 '17 10:12 hccampos

In my case it turned out to be the fact that we were using 2 decorators on a function Our code was like this, which should work and is valid

@HostListener('document:keydown.escape', ['$event'])
@dispatch()
onKeydownHandler() {
      return this.actions.hideOverlay();
}

Changing to below got rid of the error

@HostListener('document:keydown.escape', ['$event'])
onKeydownHandler() {
    this.hideOverlay();
}

@dispatch()
hideOverlay() {
   return this.actions.hideOverlay();
}

el-davo avatar Dec 05 '17 13:12 el-davo

@el-davo can't if i've class decorator an property decorator as @hccampos said

elvisbegovic avatar Dec 05 '17 13:12 elvisbegovic

Error fixed in typescript version 2.7.0-dev.20171201 https://github.com/Microsoft/TypeScript/pull/20314

kostia24 avatar Dec 06 '17 17:12 kostia24

@filipesilva can you please get a look here? actually blocking build process (aot)

elvisbegovic avatar Dec 10 '17 13:12 elvisbegovic

Have a similar issue AOT build wont work, have to use --prod with --aot=fallse ERROR in : TypeError: Cannot read property 'kind' of undefined at nodeCanBeDecorated (node_modules\typescript\lib\typescript.js:7811:36)

paulchill avatar Dec 11 '17 22:12 paulchill

Downgrading angular-cli seems to be the only solution at the moment.

arsyed04 avatar Dec 12 '17 12:12 arsyed04

I had the same issue with: Angular CLI 1.6.1 Typescript 2.4.2 Angular 5.0.2

Found a solution by changing typescript.js for now. Replace all the function function nodeCanBeDecorated(node) with the following code

function nodeCanBeDecorated(node) {
       switch (node.kind) {
           case 229 /* ClassDeclaration */:
               // classes are valid targets
               return true;
           case 149 /* PropertyDeclaration */:
               // property declarations are valid if their parent is a class declaration.
   			// return node.parent.kind === 229 /* ClassDeclaration */;
   			return (node.parent && node.parent.kind === 229) || (node.original && node.original.parent && node.original.parent.kind === 229);
           case 153 /* GetAccessor */:
           case 154 /* SetAccessor */:
           case 151 /* MethodDeclaration */:
               // if this method has a body and its parent is a class declaration, this is a valid target.
               return node.body !== undefined &&
   				// && node.parent.kind === 229 /* ClassDeclaration */;
   				(node.parent && node.parent.kind === 229) || (node.original && node.original.parent && node.original.parent.kind === 229);
           case 146 /* Parameter */:
               // if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target;
               // return node.parent.body !== undefined
               //     && (node.parent.kind === 152 /* Constructor */
               //         || node.parent.kind === 151 /* MethodDeclaration */
               //         || node.parent.kind === 154 /* SetAccessor */)
   			//     && node.parent.parent.kind === 229 /* ClassDeclaration */;
   			
   			var parent = node.parent || (node.original && node.original.parent);
   			return parent && parent.body !== undefined &&
                     (parent.kind === 152
                        || parent.kind === 151
                        || parent.kind === 154) && parent.parent.kind === 229;
       }
       return false;
   }

Which comes from here, if you compile typescript: https://github.com/shlomiassaf/TypeScript/commit/7017fa2a0a8137279230f04b59a6ec89f6928331

gmavritsakis avatar Dec 14 '17 23:12 gmavritsakis

"@angular/cli": "^1.6.1" Also throw this error

icjking avatar Dec 15 '17 13:12 icjking

@gmavritsakis Is it angular-cli issue based on your deduction or typescript? I have upgraded all my other libraries including angular and typescript, but issue arise only when I upgrade angular-cli.

If it is angular-cli issue, I strongly recommend you create a pull request with your fix above so that it can be merged into release.

arsyed04 avatar Dec 15 '17 13:12 arsyed04

When I try using [email protected] (which contains the fix), I get this error from the Angular CLI:

Could not find local "typescript" package. The "@ngtools/webpack" package requires a local "typescript@^2.0.2" package to be installed.

The dev version string apparently fails this check: https://github.com/angular/angular-cli/blob/master/packages/@ngtools/webpack/src/index.ts#L9

kevinbuhmann avatar Dec 18 '17 20:12 kevinbuhmann

Can verify that it's angular-cli issue. Rollback to v1.6.0-beta.0 proved that.

glebcha avatar Dec 19 '17 10:12 glebcha

I downgraded to angular-cli 1.5.0 and error is gone.

r-winkler avatar Dec 25 '17 12:12 r-winkler

Is there any version of ngtools/webpack without this error?

artstar avatar Dec 28 '17 17:12 artstar

Install the latest CLI release 1.6.3 and install typescript@next.

danielsogl avatar Dec 29 '17 09:12 danielsogl

@danielsogl Your tip solved the issue for me. Thank you!

petivagyok16 avatar Jan 02 '18 09:01 petivagyok16

Is there a fix path where an upgrade to typescript@next isn't required? I would think this is a angular-cli regression issue since downgrading avoids the error

emilio-martinez avatar Jan 02 '18 17:01 emilio-martinez