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

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 22
  • Comments: 40

Most upvoted comments

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

@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) 

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 .

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?

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

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

@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 had same issues, finally upgrading all node_modules fixed the problem.

@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"
}

@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”

So let’s fix it ourselves. 😃

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.

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.

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