Skip to content

Source maps not working with Angular 2. Only compiled source locations shown. #11722

Closed

Description

@chrisnicola

I'm submitting a ... (check one with "x")

[x] bug report

Current behavior
When throwing any kind of exception in an Angular 2 app (e.g., from the constructor of a component) the error messages produced do not appear to support source maps.

Expected behavior

Mainly, I expect source locations to show in stack traces. Especially for trivial examples like throwing an exception from inside of a component.

Reproduction of the problem

I have tested this in a simple example built with angular-cli, it is trivial to reproduce.

https://github.com/chrisnicola/ng2-error-message-example

Edited with results of testing 2016-09-22

After extensive testing the issue may be related to the way the Chrome parses stack traces and maps them in the console to the source map. However Chrome does do this properly, but for some strange reason logging stack traces inside of an Angular 2 app (or possibly just a Zone) doesn't work.

Testing

The following trivial code is what I've used to test with. You will get different results if this code is contained within Angular2 or Zone.js managed code (such as a Component constructor) than if it is outside in the root of the same typescript file. Namely that source maps don't work for the former but do otherwise.

try {
  throw new Error("Thrown.")
} catch (e) {
  console.log(e.stack)
}
You can also just do:

console.log((new Error("Not Thrown")).stack)

Results:

  1. You can log a stack trace with the above code snippet and the Chrome dev tools console will show original source locations.
  2. This is not true if that code is contained inside something running in Angular 2 like a Component constructor
  3. This is still true if the code is contained inside something like an Angular 1 controller constructor
  4. Caveat 1: Only full source map files work however (so webpack must be configured to use "source-map" as the dev tool). This means that this will not work with angular-cli until it is changed to output source map files.
  5. Caveat 2: Passing multiple arguments to console.log such as console.log("Error: ", e.stack) does break source mapped line locations (probably a Chrome bug)
  6. Caveat 3: This won't work with bare errors at all, e.g.) throw "Error"

Conclusions

Support for logging stack traces with source mapped source locations is clearly a bit spotty in Chrome. It does work but not if there are multiple arguments, and you must pass the .stack property of the error to the log and not the error directly.

However while it works, for some reason any errors logged from inside of an Angular 2 component don't work even when the stack traces are nearly identical. I have no idea why this happens but suspect it may have something to do with zones. As I understand little about how Zone.js works or what it does I can't really be of any more help than this.

In the end it appears that for the moment logging original locations in stack traces in Angular 2 apps is not possible. Something I would suggest is a show stopper, particularly considering this is a Typescript oriented framework.

If I'm still just doing something wrong here please let me know. Please, if anyone has an example that shows error handling working with original source locations in the stack traces please let me know.

What is the motivation / use case for changing the behavior?

Stack traces from a Typescript based codebase that don't support original source locations are extremely hard to work with, so this is a pretty serious productivity burden. That being said it is not impossible that this represents a bug that should be fixed in the Chrome dev tools and not Angular.

  • Angular version: 2.0.0
  • Browser: [Chrome]
  • Language: [TypeScript 2.0.2]

Activity

  1. vicb commented on Sep 19, 2016

    @vicb
    Contributor

    Please attach the (unhelpful) error message.

  2. changed the title [-]Source maps not working with Angular 2 at all. Only compiled source locations shown.[/-] [+]Source maps not working with Angular 2. Only compiled source locations shown.[/+] on Sep 19, 2016
  3. chrisnicola commented on Sep 19, 2016

    @chrisnicola
    Author

    I've done what I can, copy and pasting from Chrome's console doesn't really format the text really well. I would suggest checking out the example for a better view of it.

  4. vicb commented on Sep 19, 2016

    @vicb
    Contributor

    I think throw "Test Error" do not preserve stack trace (this is a JS issue, not related to Angular).

    Could you try throw new Error('Test Error') and report ?

  5. chrisnicola commented on Sep 19, 2016

    @chrisnicola
    Author

    Same result. Also it should be noted that throw "Error" does result in a correct source mapped source location if you put it in index.ts instead of in a component.

    EXCEPTION: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception TestErrorHandler.handleError @ error_handler.js:45(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    
    ORIGINAL EXCEPTION: Exception TestErrorHandler.handleError @ error_handler.js:47(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    error_handler.js:50
    
    ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:50(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    error_handler.js:51Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at DebugAppView._View_AppComponent_Host0.createInternal (AppComponent_Host.ngfactory.js:16:28)
        at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:53766:21)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53978:44)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)
        at PlatformRef_._moduleDoBootstrap (http://localhost:4200/main.bundle.js:24433:42)
        at http://localhost:4200/main.bundle.js:24401:27ErrorHandler.handleError @ error_handler.js:51(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    
    Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at DebugAppView._View_AppComponent_Host0.createInternal (AppComponent_Host.ngfactory.js:16:28)
        at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:53766:21)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53978:44)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)
        at PlatformRef_._moduleDoBootstrap (http://localhost:4200/main.bundle.js:24433:42)
        at http://localhost:4200/main.bundle.js:24401:27ErrorHandler.handleError @ error_handler.js:51(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    
    ERROR CONTEXT:ErrorHandler.handleError @ error_handler.js:54(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    error_handler.js:55DebugContext {_view: _View_AppComponent_Host0, _nodeIndex: 0, _tplRow: 0, _tplCol: 0}ErrorHandler.handleError @ error_handler.js:55(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    
    DebugContext {_view: _View_AppComponent_Host0, _nodeIndex: 0, _tplRow: 0, _tplCol: 0}ErrorHandler.handleError @ error_handler.js:55(anonymous function) @ application_ref.js:209ZoneDelegate.invoke @ zone.js:192onInvoke @ ng_zone_impl.js:43ZoneDelegate.invoke @ zone.js:191Zone.run @ zone.js:85(anonymous function) @ zone.js:451ZoneDelegate.invokeTask @ zone.js:225onInvokeTask @ ng_zone_impl.js:34ZoneDelegate.invokeTask @ zone.js:224Zone.runTask @ zone.js:125drainMicroTaskQueue @ zone.js:357
    zone.js:344Unhandled Promise rejection: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Test ; Zone: <root> ; Task: Promise.then ; Value: ViewWrappedError {_nativeError: Error: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Te…, originalError: Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at…, context: DebugContext}_nativeError: Error: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Test
        at ViewWrappedError.Error (native)
        at ViewWrappedError.BaseError [as constructor] (http://localhost:4200/main.bundle.js:6080:34)
        at ViewWrappedError.WrappedError [as constructor] (http://localhost:4200/main.bundle.js:6109:16)
        at new ViewWrappedError (http://localhost:4200/main.bundle.js:38372:16)
        at DebugAppView._rethrowWithContext (http://localhost:4200/main.bundle.js:54035:23)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53981:18)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)context: DebugContextmessage: (...)name: (...)originalError: Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at DebugAppView._View_AppComponent_Host0.createInternal (AppComponent_Host.ngfactory.js:16:28)
        at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:53766:21)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53978:44)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)
        at PlatformRef_._moduleDoBootstrap (http://localhost:4200/main.bundle.js:24433:42)
        at http://localhost:4200/main.bundle.js:24401:27stack: (...)__proto__: WrappedError Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at DebugAppView._View_AppComponent_Host0.createInternal (AppComponent_Host.ngfactory.js:16:28)
        at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:53766:21)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53978:44)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)
        at PlatformRef_._moduleDoBootstrap (http://localhost:4200/main.bundle.js:24433:42)
        at http://localhost:4200/main.bundle.js:24401:27consoleError @ zone.js:344_loop_1 @ zone.js:371drainMicroTaskQueue @ zone.js:375
    
    Unhandled Promise rejection: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Test ; Zone: <root> ; Task: Promise.then ; Value: ViewWrappedError {_nativeError: Error: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Te…, originalError: Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at…, context: DebugContext}_nativeError: Error: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Test
        at ViewWrappedError.Error (native)
        at ViewWrappedError.BaseError [as constructor] (http://localhost:4200/main.bundle.js:6080:34)
        at ViewWrappedError.WrappedError [as constructor] (http://localhost:4200/main.bundle.js:6109:16)
        at new ViewWrappedError (http://localhost:4200/main.bundle.js:38372:16)
        at DebugAppView._rethrowWithContext (http://localhost:4200/main.bundle.js:54035:23)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53981:18)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)context: DebugContextmessage: (...)name: (...)originalError: Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at DebugAppView._View_AppComponent_Host0.createInternal (AppComponent_Host.ngfactory.js:16:28)
        at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:53766:21)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53978:44)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)
        at PlatformRef_._moduleDoBootstrap (http://localhost:4200/main.bundle.js:24433:42)
        at http://localhost:4200/main.bundle.js:24401:27stack: (...)__proto__: WrappedError Error: Exception Test
        at new AppComponent (http://localhost:4200/main.bundle.js:43010:15)
        at DebugAppView._View_AppComponent_Host0.createInternal (AppComponent_Host.ngfactory.js:16:28)
        at DebugAppView.AppView.create (http://localhost:4200/main.bundle.js:53766:21)
        at DebugAppView.create (http://localhost:4200/main.bundle.js:53978:44)
        at ComponentFactory.create (http://localhost:4200/main.bundle.js:38117:36)
        at ApplicationRef_.bootstrap (http://localhost:4200/main.bundle.js:24524:40)
        at http://localhost:4200/main.bundle.js:24433:89
        at Array.forEach (native)
        at PlatformRef_._moduleDoBootstrap (http://localhost:4200/main.bundle.js:24433:42)
        at http://localhost:4200/main.bundle.js:24401:27consoleError @ zone.js:344_loop_1 @ zone.js:371drainMicroTaskQueue @ zone.js:375
    zone.js:346Error: Uncaught (in promise): Error: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Test(…)consoleError @ zone.js:346_loop_1 @ zone.js:371drainMicroTaskQueue @ zone.js:375
    
    Error: Uncaught (in promise): Error: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Exception Test(…)consoleError @ zone.js:346_loop_1 @ zone.js:371drainMicroTaskQueue @ zone.js:375
    

    Edit: Fixed copy-paste formatting of error messages.

  6. vicb commented on Sep 19, 2016

    @vicb
    Contributor

    Can you try to reproduce in a plunker ?

  7. chrisnicola commented on Sep 19, 2016

    @chrisnicola
    Author

    Does the angular-cli example not work?

  8. chrisnicola commented on Sep 20, 2016

    @chrisnicola
    Author

    Ok so I tried this with Plunker and it actually shows the location (though it isn't shown until the third console error message, which is probably something for another discussion).

    https://plnkr.co/edit/T54xlyn2c0KCqKJrT3YP?p=preview

    So this might indicate this is an issue with Webpack + Angular 2. That said the problem manifests in both our own app and in the trivial angular-cli built hello world example I provided. I'm not sure what Plunker is doing differently with respect to source maps. It is also odd that the source map locations work in the Webpack example for errors not thrown from Angular 2.

  9. vicb commented on Sep 20, 2016

    @vicb
    Contributor

    angular-cli does not load long-stack-trace in dev environment, see the issue linked above against the cli.

  10. chrisnicola commented on Sep 20, 2016

    @chrisnicola
    Author

    Sadly this is not the solution. I've added this to src/polyfills.ts in the angular-cli example (and I was already loading it in our project as we followed the guide for setting up with webpack on angular.io).

    I've updated the Git repo with the example with the change so you can verify yourself that the source locations are still not working.

  11. chrisnicola commented on Sep 20, 2016

    @chrisnicola
    Author

    This behaviour is really quite odd. I've tried doing a simple try catch and manually logging exceptions to the console and line numbers and everything work (even from the same .ts file). When I put the exception inside anything Angular is doing the console loging works but the console only links to compiled source locations.

  12. 7 remaining items

  13. vicb commented on Sep 22, 2016

    @vicb
    Contributor

    @pkozlowski-opensource not sure if it is a good thing to re-open this issue. This thread is too long.

    @chrisnicola could you please open a new issue and sum up your findings in the description so that we don't have to read the whole thread to know what is valid / no more valid. Thanks.

  14. chrisnicola commented on Sep 22, 2016

    @chrisnicola
    Author

    @vicb for simplicity and posterity I could just edit the top comment if you like.

  15. vicb commented on Sep 22, 2016

    @vicb
    Contributor

    yep, please add the edit date in the comment then. Thanks.

  16. chrisnicola commented on Sep 22, 2016

    @chrisnicola
    Author

    No problem, let me know if the changes work for you.

  17. added
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    and removed
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    on Oct 4, 2016
  18. chrisnicola commented on Nov 7, 2016

    @chrisnicola
    Author

    Hi @IgorMinar is there anything I can provide in terms of a repro? I have the one built in Angular-CLI but perhaps that one has some issues.

  19. removed
    needs reproductionThis issue needs a reproduction in order for the team to investigate further
    on Feb 6, 2018
  20. IgorMinar commented on Feb 6, 2018

    @IgorMinar
    Contributor

    It's taken way too long, but we are finally fixing this. I'm going to close this as dupe of #19841 for now.

  21. hlolli commented on May 18, 2018

    @hlolli

    I'm useing @angular/cli 6.0.3 (updated from 5x which also didn't work) and angular 5.1.1 and the source maps are located under webpack://. But they don't get referenced in stacktraces, I'm still only pointed to minified scripts.bundle.js and vendor.bundle.js from my ng serve development env. I'm a beginner in angular so if someone could explain in simple english what I need to do to get meaningful error messages, would be much appreciated.

  22. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    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.

  23. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions