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
Angular2 AOT compilation - "Cannot determine the module for class (... many components which are unused)" #13590
Comments
Component must be a part of a module. It's intended. |
@DzmitryShylovich It makes sense for components you are USING to need to be part of a module. But as far as the compiler is concerned, these files should not matter. They are unused, un-referenced .ts files that happen to contain components. |
@swimmadude66 u can exclude unused files in tsconfig |
@DzmitryShylovich you can, but barrel files complicate that. If a class is re-exported in a barrel file, I have to ignore the barrel file as well, which can cause problems with the classes I DO need from that barrel. It seems weird that the compiler should try to compile any more than it absolutely has to. Tree-shaking aside, I'd expect the compiler to want to only deal with files given to it or explicitly linked to given files. |
this is how compilers work... |
The fact that JIT compilation works should be pretty good evidence that the compiler does not NEED these files. It is throwing an error instead of a warning on files which could be excluded with no harm done. you can talk down to me about how compilers work all day, but this is a real issue in a somewhat basic use case. I am only asking for some way to at least suppress the error and continue at my own risk. My co-worker is attempting to remove any barrel files which could be blocking us from using blanket excludes, but I'd ask you take a look at the issue I originally opened against @ngtools/webpack, where another user had the same error from a component only referenced in their tests. angular/angular-cli#3636 The compiler is aware of files which I am not asking it to compile, and it is throwing unrecoverable errors for recoverable situations. plain as that. |
I don't understand why you have components in the same directory that aren't included in the project. |
@Toxicable the unused files are in a library-style npm module. Generally the ideal usecase is something like this: in @project/angular (our shared code npm module) we have all the components, pipes, directives, and services needed to make our base app and communicate with our backend platform. Our partners want an app that looks similar, but uses a different home page, or has some new component added. However, most of the other pages will be the same, and all the services needed to connect to our platform might be the same. Our approach to maximize reusable code was make each partner create modules, and import a combination of new pieces, and shared pieces from the npm module. My new
this then blows up in AOT saying: Since literally no files point at As for the JIT works !== AOT works, the base app works with AOT, and the changes in the Proof of Concept partner are incredibly small. I do not mean to imply that everything that works in JIT should work in AOT, but I have very good reason to believe that this should. |
I have another example where this current behaviour doesn't make sense - tests.
I then use this component in my testing module:
Everything's valid and great, until when i run I don't think AOT should be concerned with |
The general rule is: ngc will compile everything that tsc compiles. And it will try to compile every component that it finds. However, Angular can't compile components without a related module. We could change this error into a warning though. |
I also have test wrapper components that are used for testing only and that causes AOT to blow up as described here. Would be nice if AOT could ignore all components that satisfies a wildcard expression like TestComponent* etc. |
Alright, so more info here. We do seem to have found a workaround for our case (doesn't fix the testWrapper case). It seems the real issue in our case was our barrel files. When importing ANYTHING from a barrel file, it follows the chain of all things re-emitted by the barrels. Since we were pulling in our components from a top level |
seeing same error:
Angular 2 Kitchen sink: http://ng2.javascriptninja.io Sean |
i18 should not walk the entire project structure and instead look at the declarations used. |
I tried to clean the directives that are not being used, and the rabbit hole just got deeper:
let me add that all is working fine with angular-cli and angular-compiler, so its just i18 that is not liking my project. Reference (nice and clean setup): https://github.com/born2net/ng-skeleton Regards, Sean |
Same error for commented out components .. unused and commented out components for development stage are just useful step to not process
|
@kirjai ngc compiles all files inside a source directory. it doesn't matter if you are import it or not. |
@DzmitryShylovich which I understand, I'm just saying that I don't think that the AOT should care about this file at all in this case.. in my mind, skipping .spec files during AOT compilation is the way to go. But clearly there's something that's stopping the team from doing that, I understand. As an alternative, then maybe the putting .spec files in the same directory as the entity the tests are written for should not be suggested by the style guide? |
I also run into this error message (and some others) because of our test code and AOT in combination. I can recommend this article. It explains how certain error messages can be provoked and how to fix/workaround them. Considering that people will stumble into this exact issue if they follow the official testing guide and build their app with AOT, you may want to update the guide? (If someone looks for an answer regarding
|
By the way, it also tries to determine the module for abstract classes: "Error: Cannot determine the module for class AsyncTransform" Adding it to a module also doesn't work 😄. |
this is happening for some classes as well. Err: Cannot determine the module for class AppGlobalModalComponent
|
Having an issue to maintain compatibility for a library built with ng 9 that I wanted people running ng 8 to be able to use. I offer through the library some utility classes that your components can extend upon. In that chain of parent classes some of these are abstract and with ng 9 need to have a So I nearly got away with it... BUT:
I do not expose any module within the library, people are just supposed to extends on the classes we offer. But I don't want them to have to imports the parent classes in their modules (wouldn't make any sense). So I'm stuck and I'll just cut a breaking change release requiring people to upgrade to ng 9 instead of having backward compat |
Hey all, sorry for the silence on this issue. ngc in View Engine, by design, generates So if an The correct way to do that is to scope your tsconfig. For example, you might have a top-level tsconfig for your editor, which includes all files, and then a Projects (each tsconfig is a "project") should be structured such that a component and its module are always compiled together. In Ivy the same rules still largely apply, with several small differences:
|
@alxhub I'm glad to hear it is no longer an error! I understand that ngc will try and compile anything that tsc would, and that tsc will compile anything within its scope. That being said, the requirement that a component be added to a module is 100% an angular concern, so removing it from your typescript project scope feels like kicking the can down the road. Additionally, with patterns like barrel files being common, it can be difficult to truly remove a single file from compilation, without ALSO removing it from any re-exports along the way. This is why the calls to simply "exclude those components" above were often met with dissatisfaction. Regarding the remaining limitation with ivy (the template errors on components without modules), is it too much to ask for those to be warnings as well? I understand that may be more difficult (or impossible) if type-checking occurs before the component is identified as module-less, but it seems to me like a warning along the lines of Overall, I am very excited to see movement on this issue, and look forward to trying out my original use case with the new changes in the ivy compiler! |
Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular version. If the problem still exists in your application, please open a new issue and follow the instructions in the issue template. |
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. |
Current behavior
Unused components/pipes/directives in my workspace are detected by the compiler, which throws the error
Cannot determine the module for class (...)
for each file. It stops compilation, and does not seem be configurable. This is a problem, since I need to have those files in my workspace, but do not need them in the resultant app (partner implementations requiring different combos of shared components). This is especially frustrating with regards to compiling in a webpack loader, which should be able to provide a list of files which are included, regardless of workspace.Expected behavior
I would expect these errors to be warnings, and/or able to be silenced by a compiler option. Alternatively, with regards to webpack, you could allow a list of files to be inserted, so that a webpack could provide all files in the require chain, instead of all files in the workspace.
Minimal reproduction of the problem with instructions
Cannot demo in plunkr, since it uses JIT.
Cannot determine the module for class
What is the motivation / use case for changing the behavior?
My company has a base app for ourselves, and our partners use modified versions of that app as their own. Rather than maintain all partners separately, we use a shared library of common generic components, imported as needed. For our base app, everything is fine, since we use every component. For partners, we cannot use AOT, since some of the components in the shared npm package do not have a declared module.
Please tell us about your environment:
Happens across all devices, but the current testing setup is:
WIndows 10
VS Code
Cmder (bash terminal)
Angular version:
v2.1.0 (though we have also tested in 2.3.1
Browser: All - this is a compiler issue, not browser specific
Language: Typescript
Node (for AoT issues): node v6.3.0
The text was updated successfully, but these errors were encountered: