-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(@angular/cli): add circular dependency detection #6813
Conversation
91a1da0
to
db0f817
Compare
db0f817
to
0830f1b
Compare
Circular dependencies, like `app.module.ts` importing `app.component.ts` which in turn imports `app.module.ts`, now display a warning during builds: ``` kamik@T460p MINGW64 /d/sandbox/master-project (master) $ ng build Hash: 3516b252f4e32d6c5bb8 Time: 8693ms chunk {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 160 kB {4} [initial] [rendered] chunk {1} main.bundle.js, main.bundle.js.map (main) 5.95 kB {3} [initial] [rendered] chunk {2} styles.bundle.js, styles.bundle.js.map (styles) 10.5 kB {4} [initial] [rendered] chunk {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 1.88 MB [initial] [rendered] chunk {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered] WARNING in Circular dependency detected: src\app\app.module.ts -> src\app\app.component.ts -> src\app\app.module.ts WARNING in Circular dependency detected: src\app\app.component.ts -> src\app\app.module.ts -> src\app\app.component.ts ``` It is important to detect and eliminate circular dependencies because leaving them in might lead to `Maximum call stack size exceeded` errors, or imports being `undefined` at runtime. To remove these warnings from your project you can factor out the circular dependency into a separate module. For instance, if module A imports `foo` from module B, and module B imports `bar` from module A, it is enough to extract `foo` into module C. You can turn off these warnings by running ng set apps.0.hideCircularDependencyWarnings=true. This will add the "hideCircularDependencyWarnings": true value to your .angular-cli.json and disable the warnings. Fix angular#6309 Fix angular#6739
0830f1b
to
0d93406
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for adding the option to disable/suppress this check.
This is good stuff! However, if I do the following steps, I get an error:
Running |
@Splaktar you're right, I forgot to add it to the list of dependencies that are added to the package.json. Will do it in a followup PR. |
Flag is now positive instead of negative and shorter, and can now be set on commands as well (`--show-circular-dependencies`). Dependency was also added to eject as per #6813 (comment).
Flag is now positive instead of negative and shorter, and can now be set on commands as well (`--show-circular-dependencies`). Dependency was also added to eject as per #6813 (comment).
Flag is now positive instead of negative and shorter, and can now be set on commands as well (`--show-circular-dependencies`). Dependency was also added to eject as per angular#6813 (comment).
Flag is now positive instead of negative and shorter, and can now be set on commands as well (`--show-circular-dependencies`). Dependency was also added to eject as per #6813 (comment).
I wonder why I get this warning if I just import classes in order to have types, I'm using cli (v1.3.0-rc.0)
and in another file I have :
This get me:
|
@colas74 weird... it seems to me in java this is allowed, donno if it's cli or ts error. |
@colas74 you have a warning because the imports you are using as circular. It doesn't really matter if it's for types or not (as far as detection is concerned). @istiti you're absolutely right that it's allowed in many languages, and it's perfectly allowed in JavaScript as well. It's just that sometimes Webpack has problems with it (like the ones I marked fixed in the PR). If you don't have any problems with your imports, feel free to go ahead and disable the warnings. But if you come across those errors, you can enable the warnings again and that might give you and idea of where the problem is. |
@filipesilva Thx for your answer. I think there is no problem to import types and create circular dependencies, if they are only used for object's type. In any case, it is not seems to create performances issue |
According to:
Javascript will works , but build system can have problem? So it's maybe one reason why |
The |
Can't add property hideCircularDependencyWarnings, object is not extensible |
@amily4555 try |
And i guess it is now a issue. Let assume we have this classes : @Injectable()
export class ClassA {
public static readonly STATIC_CONST_VALUE: string = "ILL NEVERE CHANGE";
consturctor(private classB: ClassB){}
public illCallClassBMethod(): void {
this.classB.doSomething();
}
}
export class ClassB {
public doSomething() {
// Some logic that requires a static field from classB
console.log(ClassA.STATIC_CONST_VALUE); //this will be now a circular dep, but its just a call to a class not a instance
}
} This will throw a warning now , i guess the way the plugin works , its checking the imports. The feature is good , no doubt , but i would suggest to turn it off by default. |
Flag is now positive instead of negative and shorter, and can now be set on commands as well (`--show-circular-dependencies`). Dependency was also added to eject as per angular#6813 (comment).
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Circular dependencies, like
app.module.ts
importingapp.component.ts
which in turn importsapp.module.ts
, now display a warning during builds:It is important to detect and eliminate circular dependencies because leaving them in might lead to
Maximum call stack size exceeded
errors, or imports beingundefined
at runtime.To remove these warnings from your project you can factor out the circular dependency into a separate module.
For instance, if module A imports
foo
from module B, and module B importsbar
from module A, it is enough to extractfoo
into module C.You can turn off these warnings by running
ng set apps.0.hideCircularDependencyWarnings=true
. This will add the"hideCircularDependencyWarnings": true
value to your.angular-cli.json
and disable the warnings.Fix #6309
Fix #6739