angular: Host should not return a redirect source file from `getSourceFile`
I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
When using webpack-dev-server in --watch mode and you make a change the build process throws the following error:
ERROR in Error: Debug Failure. False expression: Host should not return a redirect source file from `getSourceFile`
at tryReuseStructureFromOldProgram (C:\Users\me\repo\web-app-angular\node_modules\typescript\lib\typescript.js:74285:26)
at Object.createProgram (C:\Users\me\repo\web-app-angular\node_modules\typescript\lib\typescript.js:73988:34)
at AngularCompilerProgram._updateProgramWithTypeCheckStubs (C:\Users\me\repo\web-app-angular\packages\compiler-cli\src\transformers\program.ts:508:26)
at C:\Users\me\repo\web-app-angular\packages\compiler-cli\src\transformers\program.ts:187:18
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Expected behavior
It should not throw an error and compile the code as usual.
Minimal reproduction of the problem with instructions
See this PR for more info: https://github.com/dherges/ng-packagr/pull/637
What is the motivation / use case for changing the behavior?
Webback watch mode is useless if you have to restart it after each change.
Environment
Angular version: 5.2.6 | 6.0.0-beta.6
For Tooling issues:
- Node version: 8.9.4
- Platform: Windows
Others:
Typescript: 2.6.2
Webpack: 4.0.1
Webpack-dev-server: 3.1.0
Conclusion
This seems to work. Not sure what else it will break though. Added the redirectInfo fix from the PR i referenced.
// program.js:485 (compiled file)
tmpProgram.getSourceFiles().forEach(function (sf) {
if (_this.hostAdapter.isSourceFile(sf.fileName)) {
if (sf['redirectInfo']) {
sf = sf['redirectInfo'].redirectTarget;
}
sourceFiles.push(sf.fileName);
}
if (util_1.TS.test(sf.fileName) && !util_1.DTS.test(sf.fileName)) {
tsFiles.push(sf.fileName);
}
});
// program.ts:568 (source file)
// note: redirectInfo does not exists on ts.SourceFile hence the any.
tmpProgram.getSourceFiles().forEach(sf => {
if (this.hostAdapter.isSourceFile(sf.fileName)) {
if ((sf as any)['redirectInfo']) {
sf = (sf as any)['redirectInfo'].redirectTarget;
}
sourceFiles.push(sf.fileName);
}
if (TS.test(sf.fileName) && !DTS.test(sf.fileName)) {
tsFiles.push(sf.fileName);
}
});
console.log(sf.fileName);
console.log(sf['redirectInfo'].redirectTarget.fileName);
--------------------------
C:/Users/me/repo/web-app-angular/node_modules/@types/webpack/node_modules/source-map/source-map.d.ts
C:/Users/me/repo/web-app-angular/node_modules/@types/uglify-js/node_modules/source-map/source-map.d.ts
--------------------------
Edit: It seems like my ticket is not clear enough in pointing out the issue based on the new posts in this thread. The issue happens due to identical *.d.ts file found via two paths in the compilation. In plain english: The packages uses the same dependency that includes a definition file.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 59
- Comments: 55 (11 by maintainers)
Commits related to this issue
- Temp fix for https://github.com/angular/angular/issues/22524 — committed to JonWallsten/angular by JonWallsten 6 years ago
- fix(compiler-cli): Temp fix in program.ts issue #22524 Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be sho... — committed to JonWallsten/angular by JonWallsten 6 years ago
- fix(compiler-cli): Temp fix in program.ts issue #22524 Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be sho... — committed to JonWallsten/angular by JonWallsten 6 years ago
- fix(compiler-cli): Temp fix in program.ts issue (#22524) Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be s... — committed to JonWallsten/angular by JonWallsten 6 years ago
- fix(compiler-cli): Temp fix in program.ts issue #22524 Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be sho... — committed to JonWallsten/angular by JonWallsten 6 years ago
- fix(compiler-cli): Temp fix in program.ts issue (#22524) Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be s... — committed to JonWallsten/angular by JonWallsten 6 years ago
- fix(compiler-cli): Temp fix in program.ts issue (#22524) Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be s... — committed to JonWallsten/angular by JonWallsten 6 years ago
- Add @types/webpack dependency — committed to microsoft/monaco-editor-webpack-plugin by kube 6 years ago
- Compiling a project with duplicated sub dependencies causes 'Host should not return a redirect source file from `getSourceFile`' error to be shown. This is fixed by not returning the redirected file, ... — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return redirect target for conflicting source files `getSourceFile` Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return redirect target for conflicting source files from `getSourceFile` Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return redirect target for conflicting source files from `getSourceFile` Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
- fix(compiler-cli): Return original sourceFile instead of redirected sourceFile from getSourceFile Closes #22524 — committed to JonWallsten/angular by JonWallsten 5 years ago
@alexeagle Will this ever be looked at? It’s easily reproducible, seems to be a simple fix and affects a lot of users based on the number of “Thumbs up”. The error still occurs in Angular 8 RC.
After updating
@angular
to 7.0.0 andng-packagr
to4.3.1
I have this error as well every time I change something in code duringserve --aot
mode:ERROR in Debug Failure. False expression: Host should not return a redirect source file from 'getSourceFile'
@charlieargue
Tested with Typescript 2.7.2:
Open node_modules/typescript/lib/typescript.js (the compiled source in your project)
Search for “Host should not return a redirect source file from
getSourceFile
” In TS 2.7.2 that is around line 27286.Directly before this line (the assert) insert:
Start AOT dev build, change sth, watch the error.
Just wanted to let you know that the PR for this issue is done. We’re waiting for the code owner to be back and then approve. So hopefully in a short time we just leave this thread behind us!
I’ve asked the team to consider re-triaging this issue and raising the priority after reading the ngx-bootstrap post on upgrading to Ivy.
I was finally able to find the root cause of the problem (for my use case at least). This bug shows its ugly head whenever there a duplicate typings in the project.
In my case it reappeared when upgrading to redux 4.0.0 in a project, and some plugins still have a dependency on 3.x.x. So I ended up with
and redux brings it’s own typings … twice in my case.
removing the nested node_modules caused the incremental build to finally run on TS 2.7.
How did I find the culprits? I hacked the installed typescript compiler and expanded the error message to also show the file that caused the error, not only the “source redirect” without additional info.
Basically just put a if(newSourceFile.redirectInfo)-wrapped log before the compiled source at
https://github.com/Microsoft/TypeScript/blob/fbeb58a147962aa31a4ef2b00bc4e778849c1ff0/src/compiler/program.ts#L1031
and then the bad files show up. At least that worked in my case.
As a workaround I added a postinstall script to package.json that removes the folders, like
https://github.com/DcsMarcRemolt/ngx-starter-new/blob/master/package.json#L22
Oh and a colleague also had a node_modules folder ABOVE the project folder also containing duplicate typings. Removing that finally fixed it on his machine too.
This is also a problem in Angular 8 when using ivy. From: @angular\compiler-cli\src\ngtsc\typecheck\src\context.js:164-173
Fixed in the same way by adding
if (sf['redirectInfo']) { sf = sf['redirectInfo'].redirectTarget; }
this still happen in cli v6 with aot: true in “serve” target.
Thanks to this great pointer from @DcsMarcRemolt, I was able to find our offending code that lead to this error.
In
node_modules/typescript/lib/typescript.js
, I changed line:to:
And then got the error like:
Which I found odd, so I searched our code base for:
/Typeahead
with a case-sensitive flag, and, lo and behold, someone added an import likeimport { TypeaheadMatch } from 'ngx-bootstrap/Typeahead';
. After fixing the letter casing, the error was gone.A bit unfortunate that such a small typo can cause such an obscure error that lead to hours of debugging, but, oh well… it happens. Hope this helps someone else!
I’ve noticed that using the paths-workaround is resulting in a bad side effect in our monorepo. Since Typescript added support for definitions source maps so we can CTRL-click our way around the packages in our monorepo, but the paths-workaround makes us end up in the node_modules folder instead of the project source folder. If I instead of using the old way
uses this way of pointing to our src folders instead I end up and the correct file when CTRL+click
however when I import stuff from another project instead of adding
import ... from '@my-project/package'
it addsimport ... from '../package'
which is not what I want.So the only way of fixing this issue and keep all the nice features (auto import/ctrl+click to other packages in our monorepo) is to monkey patch angular, which I just did. I’ve added the same fix as in my PR. It’s pretty robust since it looks for a specific line of code. So it won’t have to be updated unless the Angular-team changes that specific line.
To use this. Just add it as a postinstall script in package.json.
I really despise that I have to do this. But the Angular-team leaves me no other choice.
Edit: In the lastest Angular they’ve changed the TS output to ES6. So instead of using
_this.hostAdapter
you need to usethis.hostAdapter
when searching for the line numberEdit 2: They switched back again in Angular 8.
Try change tsconfig.json
It works for me eventually. @piernik
my god man – this work around saved my ass – thank you. I’ve been having an issue with bringing in an ngpackagr library and for some reason rxjs library also points to Rxjs (case change) and it made the compiler for typescript go crazy on rebuilds. Added the following to tsconfig:
ugh. thank you again.
This is actually not a new problem, see angular/angular-cli/issues/8332
Wish they would have taken it seriously back then, now we have the mess as TS 2.6 is supported.
@alexeagle Based on the number of thumbs up in the OP and the activity in this issue, do you still consider this as Frequency low? You are aware that we are unable to compile Angular at all with this bug, right?
Also, I created a PR for this 6 months a go: https://github.com/angular/angular/pull/26036
I started digging a little bit again since this Angular 6/7 is on my plate again. I logged all the files from
tmpProgram.getSourceFiles()
, and it seems like it even includes files explicitly exluded in the tsconfig.The files above will cause the error.
I also found an interesting workaround. This makes sure that any tslib used will be the same one.
@DcsMarcRemolt Sorry that you had to spend additional time on this. I thought the root cause was clear in my original post since I posted a solution for it, and the PR I linked to states “Issue happens due to identical *.d.ts file found via two paths in the compilation.” I’ve updated my post with this to avoid any future confusion.
Short answer to your initial question: no. No backwards updates. The timeline clearly says we move forward. Breaking changes every major, etc. Basic semver.
If you’re stuck in v6 and you can’t upgrade “cause a day”, I really feel for you and your team. Someone needs to tell management or the PO that this will save you days when 9 comes out. Or weeks when 10 comes out.
GL HF. Let me know if you want external help, I’ll be happy to accommodate you.
@Bjeaurn Look at Mr Fancypants over here with the spare day to spend fixing compile errors for a framework upgrade! 😉
Thanks guys! Just wanted to confirm this workaround is 💯 👍 – – and yes, had to do something like this:
Once I found the culprit module – and pretty much keeps coming back, so the package.json script is necessary.
Changing the webpack configs to JS was not sufficient in my case. But as soon as I removed the [at]types/webpack and [at]types/webpack-merge packages from my project the bug disappeared. Finally a current TS version for my projects!
My conflicting packages were
@types/webpack": "^4.4.5"
and@types/webpack-env": "1.13.0"
. The solution was to load webpack for my webpack-config files and webpack-env for my source code - via the tsconfig.jsontypes
option.@soates only a workaround as far as I know. You need to determine what module imports a duplicate .d.ts file and delete that duplicate dependency from the module. For example using a npm postinstall script. In my case it was fontawesome packages that all required a common types module with the types.
Remember to vote on the original post so that it gets visibility!
Confirming that after upgrading to Angular 6, still have the same ERROR. Thanks again for any guidance on this!
Error
relevant package.json: