Using invalid environment causes error in build (was "ERROR in Cannot read property 'length' of undefined")
(on existing project, on windows10) After updating :
- cli 31 to rc0
- angular packages to rc1
- typescript to 2.1.6
I get this insignifiant message error when running ng serve or ng build:
Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined
What does this mean ?
In my case, it's the same what @samupl has reported. A directory is passed to to TypeScript's createSourceFile method by TypeScriptFileRefactor constructor.
I've bumped into this issue when I've implemented dynamic stories loading in Storybook. By using require.context to search for files in a specific directory webpack starts watching the directory alongside the files and this directory is passed to createSourceFile when a change happens.
I'm not sure who is to blame for not skipping directories in the course of events leading to createSourceFile call.
@nikdo it might be fixable on our side, but so far I don't think I've seen a repro that I follow so it's hard to debug and try fixes :/
Thank you for a quick reply, @filipesilva.
After few hours of debugging, I think the problem was introduced by setting a filename to CompilerHost._changedFiles even if the filename is not present in CompilerHost._files array introduced in commit to avoid caching binary resources as UTF8.
That way when a directory is passed to CompilerHost.invalidate method it is handled as a file causing issue mentioned above.
What was the motivation for the change? It seems unrelated to other changes introduced in that commit.
@nikdo I'm not too sure really. If I had to bet, it was to ensure new files would be picked up on reload. Do you have a repro we can follow to see this?
In my case, bower was configured to install bower dependencies at src/assets/bower_components, so had to exclude bower_components from auto type acquisition, as the typescript compilation was looking for typescript declarations (*.d.ts) for those files, and it wasn't finding them.
In tsconfig.app.json, I excluded assets/bower_components/**/*.*
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"baseUrl": "",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts",
"assets/bower_components/**/*.*"
]
}
The above comment by @calebkiage helped me in identifying this issue
So, the general idea of excluding files for which type declarations (*.d.ts) doesn't exist should solve this issue
Just 4 Info, i had same Problem and i went almost crazy...
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
at createSourceFile (C:\DEV\AFB\varias-bepro\bepro-frontend\src\main\frontend\node_modules\typescript\lib\typescrip
t.js:14652:110)
But you know what the problem was?
at the beginning of one of my .ts file was a line like this:
///<reference path="../../../generated/rest/model/Template.ts"/>
import {Component, OnDestroy, OnInit} from '@angular/core';
import {FormGroup} from '@angular/forms';
This caused the error! Without any hint. Nothing. I thought my enviroment is broken, some wrong version or something like that. I removed the line and it worked. But i still don't know how the line got there in the first place...
I hope it helps somebody.
It seems to be an issiue of file/files cannot find in app. I spent some 2-3 hours but resolved with including environments directory to project re-install from repo etc.
@lychyi 's comment worked for me. Hope this issue is resolved soon.
Thanks for pointing me in the right direction @tmrdurgun . I'm currently in a nrwl project and was adding an environment file path to the wrong app object config.
Had the same issue, turned out that in node_modules/typescript/lib/typescript.js (line 12619) the sourceText variable was undefined somehow (function code below) . It pointed to the src/environments/environment.ts file (that existed) while the .angular-cli.json dev environment pointed to environments/environment.dev.ts (that existed too). (ng serve --env=dev was executed) Setting the .angular-cli.json dev environment to environments/environment.ts solved the problem for now.
package.json:
...
"devDependencies": {
"@angular/cli": "~1.7.2",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"typescript": "~2.5.3"
}
function createSourceFile(fileName, languageVersion, scriptKind) {
// code from createNode is inlined here so createNode won't have to deal with special case of creating source files
// this is quite rare comparing to other nodes and createNode should be as fast as possible
var sourceFile = new SourceFileConstructor(265 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length);
nodeCount++;
sourceFile.text = sourceText;
sourceFile.bindDiagnostics = [];`
...
}
I ran into this error, finally, I found out it was because a typo (exclamation mark inside the string interpolation). I wrote:
'Server with ID {{ serverId }} is {{ serverStatus! }}'
instead of
'Server with ID {{ serverId }} is {{ serverStatus }}!'
Simply delete a component folder and the angular-cli will detect the change and try to recompile. The code seems to link to a createSourceFile function in typescript.js
var sourceFile = new SourceFileConstructor(265 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length);
I assume typescript is getting passed a file that doesn't exist anymore and fails to create it?
Edit: seems that this problem occurs whenever a non existing file attempts to get compiled by typescript.
There might be an issue with environment.ts file. check if it is really- export const environment = { production: false,
dialogflow: { angularBot: 'YOUR_CLIENT_TOKEN' } };
I did this change and it worked for me.
@filipesilva Can confirm what @nikdo said, and we fixed by downgrading cli to 1.6.1. I can get some time early next week to debug a little or see if our package.json yields a repro. Mention me if that would help.
https://github.com/angular/devkit/pull/667 should fix this in 6.0 by failing early when the specified environment file (it will be called fileReplacements in v6) does not exist.
Marking this nice to have because there seems to be a fix already.
I created this patch for version 1.7.3 , you can apply the patch to node_modules/@ngtools/webpack/src/plugin.js and you should be good to go.
493c492,494
< this._discoveredLazyRoutes = this._getLazyRoutesFromNgtools();
---
> this._discoveredLazyRoutes = this.firstRun
> ? this._getLazyRoutesFromNgtools()
> : this._findLazyRoutesInAst();
To me removing comments from the environments files worked :+1: :1234:
I solved this problem (error during refactoring test). All is good for angular v4.3.* and v4.4*
- Change @angular/cli" version 1.7.3 to 1.4.10 in package.json (latest for angular4)
rm -rf node_modulesnpm install --no-optional