olov / ng-annotate

Add, remove and rebuild AngularJS dependency injection annotations
MIT License
2.03k stars 150 forks source link

Problem when $inject is already defined twice #192

Closed ersimont closed 9 years ago

ersimont commented 9 years ago

I'm using gulp-ng-annotate. I had some silly code that defined $inject for a directive's controller twice (oops!). The only behavior I saw, though, was that when I added ngAnnotate() to my gulpfile suddenly everything stopped working, without any error message. I added/removed files from my build process to narrow it down, then comment/uncomment code until I found the issue.

I'm guessing "silently fail" is not the desired behavior. My first guess is that the issue is here in ng-annotate, but I will happily move this report over to gulp-ng-annotate if everything is OK on your end.

Here is a small example that will trigger the issue:

myApp.directive('myDirective', myDirective);
function myDirective() {
    controller.$inject = ['$scope', '$element', '$attrs'];
    function controller($scope, $element, $attrs) {
        // important stuff here
    }
    controller.$inject = ['$scope', '$element', '$attrs'];
    return {controller: controller,};
}
olov commented 9 years ago

ng-annotate doesn't like that you declare the array twice but it does report an helpful error:

~/projects/ng-annotate/tests-issues % ng-annotate -a issue192.js
error: conflicting inject arrays at line 3 and 7

If you don't see this error message when using gulp then that's a bug so then please open an issue on gulp-ng-annotate.

If you need to have the duplicate arrays then just tell ng-annotate that you don't want it to annotate the controller function:

    function controller($scope, $element, $attrs) {
        "ngNoInject";
        // important stuff here
    }