Angular-cli: Test rebuild fail with Angular 4 and @angular/cli 1.6.5

Created on 23 Jan 2018  Â·  40Comments  Â·  Source: angular/angular-cli

Versions

node v8.9.0
yarn v1.3.2
@angular/cli v1.6.2 to v1.6.5

Repro steps

  • Step 1
    ng new application
  • Step 2
    change in package.json angular version to v4.4.6
  • Step 3
    yarn test => run fine
  • Step 4
    change a spec file => error (stacktrace below)

Observed behavior

It works fine with angular 5 but not with angular 4

Stacktrace :

<!-- Normally this includes a stack trace and some more information. -->
ERROR in TypeError: Cannot read property 'length' of undefined
    at createSourceFile (/home/julien/projects/test-app/node_modules/typescript/lib/typescript.js:16640:110)
    at parseSourceFileWorker (/home/julien/projects/test-app/node_modules/typescript/lib/typescript.js:16572:26)
    at Object.parseSourceFile (/home/julien/projects/test-app/node_modules/typescript/lib/typescript.js:16521:26)
    at Object.createSourceFile (/home/julien/projects/test-app/node_modules/typescript/lib/typescript.js:16371:29)
    at new TypeScriptFileRefactor (/home/julien/projects/test-app/node_modules/@ngtools/webpack/src/refactor.js:79:35)
    at Object.findLazyRoutes (/home/julien/projects/test-app/node_modules/@ngtools/webpack/src/lazy_routes.js:18:22)
    at AotPlugin._findLazyRoutesInAst (/home/julien/projects/test-app/node_modules/@ngtools/webpack/src/plugin.js:217:50)
    at _donePromise.Promise.resolve.then.then.then.then.then (/home/julien/projects/test-app/node_modules/@ngtools/webpack/src/plugin.js:496:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Same thing with --sm=false

Desired behavior


It should rebuild without error with angular 4.X.X

Mention any other details that might be useful (optional)

devkibuild-angular high broken bufix

Most helpful comment

This seems to be related to a change in the compiler_host.ts of @ngtools/webpack. See how this commit changes the invalidate function - line 177.

I fixed it by adapting the function in my node_modules (node_modules/@ngtools/webpack/src/compiler_host.js):

Original:

    invalidate(fileName) {
        fileName = this.resolve(fileName);
        if (fileName in this._files) {
            this._files[fileName] = null;
        }
        this._changedFiles[fileName] = true;
    }

Adapted:

    invalidate(fileName) {
        fileName = this.resolve(fileName);
        if (fileName in this._files) {
            this._files[fileName] = null;
            this._changedFiles[fileName] = true;
        }
    }

We have added a shell script that replaces this on npm start for every developer:

rm node_modules/@ngtools/webpack/src/compiler_host.js*
ln fixed-node-modules/compiler_host.js node_modules/@ngtools/webpack/src/compiler_host.js

All 40 comments

Any news about that ?

same here on a different project

Same here on another project.

@dominique-mueller in this one it works fine first time.

Second time it seems to be looking at directory instead of file...

fileName: '/Users/anvlkv/Projects/intervey/intervey-app/src'

TypeError: Cannot read property 'length' of undefined
    at createSourceFile (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/typescript/lib/typescript.js:15465:109)
    at parseSourceFileWorker (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/typescript/lib/typescript.js:15397:26)
    at Object.parseSourceFile (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/typescript/lib/typescript.js:15346:26)
    at Object.createSourceFile (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/typescript/lib/typescript.js:15195:33)
    at new TypeScriptFileRefactor (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/@ngtools/webpack/src/refactor.js:79:35)
    at Object.findLazyRoutes (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/@ngtools/webpack/src/lazy_routes.js:18:22)
    at AotPlugin._findLazyRoutesInAst (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/@ngtools/webpack/src/plugin.js:217:50)
    at _donePromise.Promise.resolve.then.then.then.then.then (/Users/anvlkv/Projects/intervey/intervey-app/node_modules/@ngtools/webpack/src/plugin.js:496:24)
    at process._tickCallback (internal/process/next_tick.js:109:7) 

Also getting this error. Decimates the ability to unit test due to the extremely lengthy time to rebuild after each change.

I had same issues, finally upgrading all node_modules fixed the problem.

@mrudulp What exactly do you mean by upgrading all node modules? I have tried upgrading all the modules I can, however we are not currently able to upgrade some (notably to angular 5, for example, nor to bootstrap 4 release, etc.) at the current time. Do you know if there was a specific package you upgrade that resolved the problem?

I am still experiencing this issue. I have ultimately had to simply skip unit testing for the time being, as the turnaround time for each test cycle has increased from a few seconds to 30-40 seconds. This is because the entire ng test must be rerun each time. That has made effective unit testing near impossible because of this issue.

I'm seeing a similar set of errors as @anvlkv

ERROR in TypeError: Cannot read property 'length' of undefined
    at createSourceFile (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\typescript\lib\typescript.js:15457:109)
    at parseSourceFileWorker (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\typescript\lib\typescript.js:15389:26)
    at Object.parseSourceFile (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\typescript\lib\typescript.js:15338:26)
    at Object.createSourceFile (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\typescript\lib\typescript.js:15192:29)
    at new TypeScriptFileRefactor (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\@ngtools\webpack\src\refactor.js:79:35)
    at Object.findLazyRoutes (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\@ngtools\webpack\src\lazy_routes.js:18:22)
    at AotPlugin._findLazyRoutesInAst (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\@ngtools\webpack\src\plugin.js:217:50)
    at _donePromise.Promise.resolve.then.then.then.then.then (C:\Users\jtate.NETID\Source\Repos\sage\ERA 2.0\era\angularLocationsAndCenters\node_modules\@ngtools\webpack\src\plugin.js:496:24)
    at process._tickCallback (internal/process/next_tick.js:109:7)

Same issue as @JulienBourgain - Angular cli ng test works the first time then breaks after changing any spec file or code.

I'm running:

Angular 4.4.6
Angular CLI 1.7.1
TypeScript 2.3.4
webpack 3.11.0

ERROR in TypeError: Cannot read property 'length' of undefined
    at createSourceFile (/Users/myuser/myproject/node_modules/typescript/lib/typescript.js:15457:109)
    at parseSourceFileWorker (/Users/myuser/myproject/node_modules/typescript/lib/typescript.js:15389:26)
    at Object.parseSourceFile (/Users/myuser/myproject/node_modules/typescript/lib/typescript.js:15338:26)
    at Object.createSourceFile (/Users/myuser/myproject/node_modules/typescript/lib/typescript.js:15192:29)
    at new TypeScriptFileRefactor (/Users/myuser/myproject/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/refactor.js:79:35)
    at Object.findLazyRoutes (/Users/myuser/myproject/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/lazy_routes.js:18:22)
    at AotPlugin._findLazyRoutesInAst (/Users/myuser/myproject/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/plugin.js:216:50)
    at _donePromise.Promise.resolve.then.then.then.then.then (/Users/myuser/myproject/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/plugin.js:494:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Same issue

ERROR in TypeError: Cannot read property 'length' of undefined
    at createSourceFile (D:\Developer\project\node_modules\typescript\lib\typescript.js:14022:109)
    at parseSourceFileWorker (D:\Developer\project\node_modules\typescript\lib\typescript.js:13961:26)
    at Object.parseSourceFile (D:\Developer\project\node_modules\typescript\lib\typescript.js:13873:26)
    at Object.createSourceFile (D:\Developer\project\node_modules\typescript\lib\typescript.js:13712:29)
    at new TypeScriptFileRefactor (D:\Developer\project\node_modules\@ngtools\webpack\src\refactor.js:79:35)
    at Object.findLazyRoutes (D:\Developer\project\node_modules\@ngtools\webpack\src\lazy_routes.js:18:22)
    at AotPlugin._findLazyRoutesInAst (D:\Developer\project\node_modules\@ngtools\webpack\src\plugin.js:216:50)
    at _donePromise.Promise.resolve.then.then.then.then.then (D:\Developer\project\node_modules\@ngtools\webpack\src\plugin.js:494:24)
    at process._tickCallback (internal/process/next_tick.js:109:7)
Angular CLI: 1.7.1
Node: 6.11.1
OS: win32 x64
Angular: 4.4.6
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router, tsc-wrapped

@angular/cli: 1.7.1
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.4.2
@angular-devkit/schematics: 0.4.2
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.10.1
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.7.2
webpack: 3.11.0

I have noticed there are issues even when using --single-run. The first time through with single run, it works. The second time, it fails. The third time, it works. The fourth time, it fails. Etc. etc.

@jrista that doesn't happen in my situation

@jrista I deleted node_module folder and copied over version numbers from a fresh project created with ng new

Same here... runs fine the first time but then fails on rebuilding... I'm surprised this isn't getting any responses.

Same here. Very frustrating

Angular CLI: 1.7.2
Node: 8.9.4
OS: win32 x64
Angular: 4.4.6

@angular/cli: 1.7.2
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.1
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.10.1
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.6.2
webpack-externals-plugin: 1.0.0
webpack-hot-middleware: 2.21.2
webpack-merge: 4.1.1
webpack-node-externals: 1.6.0
webpack: 3.11.0

Hi, I think it is all solved by deleting your node_modules folder and
running npm install afterwards.

Op wo 7 mrt. 2018 11:37 schreef Adam Pond notifications@github.com:

Same here. Very frustrating

Angular CLI: 1.7.2
Node: 8.9.4

OS: win32 x64
Angular: 4.4.6

@angular/cli: 1.7.2
: 0.3.2
: 0.3.2
: 0.3.1
: 1.1.0
: 1.10.1
: 0.3.2
: 0.3.2
typescript: 2.6.2
webpack-externals-plugin: 1.0.0
webpack-hot-middleware: 2.21.2
webpack-merge: 4.1.1
webpack-node-externals: 1.6.0
webpack: 3.11.0

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/angular/angular-cli/issues/9331#issuecomment-371096743,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACrvYuo2iWPMlSMjbil9JDx2MEllAEIbks5tb7hagaJpZM4RpblQ
.

@anvlkv This was the first thing I tried, it did not help.

Is this issue simply being ignored? Unless I'm missing something, this is very unacceptable. I might try to spend some time digging into this issue, but I REALLY shouldn't have to. I know the problem is that the 'src' folder is being treated as a file and trying to read it's length, but that's all I know at the moment. Can ANYONE assist please?

I was getting my error with angular cli 1.7.1. Tried updating to 1.7.2 to see if it fixed it but no luck.

Aye, I too am curious why this issue has not had any from the angular development team. It is clear it is a wide spread issue. Not everyone is able to upgrade to NG 5 or 6, and the 1.7.x version of the CLI does not appear to have resolved the issue.

This really has made unit testing angular code so tedious that we no longer even bother. The time sink is unparalleled, and we can't invest that much writing unit tests, yet at the same time, the loss of coverage is becoming worrisome.

So let's fix it ourselves. :smiley:

Maybe someone could also try npm cache clean and then reinstall. I'm sure
something like that helped me.

Op wo 7 mrt. 2018 22:02 schreef Chad Hollman notifications@github.com:

In a new project, this does not happen.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/angular/angular-cli/issues/9331#issuecomment-371283338,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACrvYvcxHYyikzejYQCKz_s2yKdn0meXks5tcErkgaJpZM4RpblQ
.

@gh0st

In a new project, this does not happen

What was in your package.json?

$ rm -rf node_modules/

$ npm cache clean
npm ERR! As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead.
npm ERR!
npm ERR! If you're sure you want to delete the entire cache, rerun this command with --force.

npm ERR! A complete log of this run can be found in:
npm ERR!     \npm-cache\_logs\2018-03-07T21_53_42_795Z-debug.log

$ npm cache verify
Cache verified and compressed (~\AppData\Roaming\npm-cache\_cacache):
Content verified: 3424 (188776509 bytes)
Content garbage-collected: 75 (3801693 bytes)
Index entries: 5195
Finished in 56.083s

$ npm install

Still fails on a ng test watch.

@anvlkv

$ rm -rf node_modules/

$ npm cache clean --force
npm WARN using --force I sure hope you know what you are doing.

$ npm install

Tests watch still fails.

@gh0st

The package.json below is a result of the ng new command.

That is generating an Angular 5.2.0 project, but the bug is associated with Angular 4.4.6. As others have pointed out, making the leap to 5.x isn't feasible in all cases.

@lgellert ah you're right, forgot that part. Sorry.

@gh0st Any luck fixing this yourself :) I wanted to try but have not had the time.

@ChadKoder I got started but...I'm in the middle of working on a project that's using angular-cli. And if I ng link angular-cli to my cloned version that I'm working on I don't want to break my dependent project. So no luck quite yet.

I have experienced the same issue with ng ^4.4 and we are unable to upgrade (at the moment) to ng ^5.0 (which does fix the issue). Instead, I found the following combination works:

"@angular/*" : "4.2.6"
"@angular/cli": "1.2.2"
"@angular/compiler-cli": "4.2.6"

I'm not completely sure if it was a combination of those versions or exactly what, I just know I'm sticking with those versions until we can upgrade to ng 5.

<< mike

@mikemichaelis that works! Thanks a lot for help, what a relief.

The following combo also seems to work:

"@angular/*" : "4.4.4"
"@angular/cli": "1.2.2"
"@angular/compiler-cli": "4.2.6"

I tried those combinations and it didn't work 😞

Is there any idea on when this will be fixed? Currently using CLI 1.7.3 and still not fixed. Testing has become almost unbearable at this point.

This seems to be related to a change in the compiler_host.ts of @ngtools/webpack. See how this commit changes the invalidate function - line 177.

I fixed it by adapting the function in my node_modules (node_modules/@ngtools/webpack/src/compiler_host.js):

Original:

    invalidate(fileName) {
        fileName = this.resolve(fileName);
        if (fileName in this._files) {
            this._files[fileName] = null;
        }
        this._changedFiles[fileName] = true;
    }

Adapted:

    invalidate(fileName) {
        fileName = this.resolve(fileName);
        if (fileName in this._files) {
            this._files[fileName] = null;
            this._changedFiles[fileName] = true;
        }
    }

We have added a shell script that replaces this on npm start for every developer:

rm node_modules/@ngtools/webpack/src/compiler_host.js*
ln fixed-node-modules/compiler_host.js node_modules/@ngtools/webpack/src/compiler_host.js

That worked for me @psurrey. So is that shell script part of an update or something? What's the recommended way that one fixes this without manually going to node_modules/.../compiler_host.js?

@gh0st I don't think there is a recommended way, but we do it in the test script of our package.json:

...
"scripts": {
  "test": "./fix-ng-cli.sh && ng test"
}

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular CLI version.

If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.

im my case I checked all the arrays and initialized them by arr: string[] = []
and no more complains.

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings