nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.28k stars 2.32k forks source link

@nrwl/angular:ng-packagr-lite executor doesn't compile partially compiled libraries #13421

Closed VanTigranyan closed 1 year ago

VanTigranyan commented 1 year ago

Current Behavior

Hi. So as the title states, when you use a library that is built with the partial compilation mode of Ivy, it doesn't work with ng-packagr-lite. The documentation says nothing about differences between @nrwl/angular:ng-packagr-lite and @nrwl/angular:package (in terms of requirements and compilation process), so the latter works fine and precompiles old libraries while the former one doesn't. Maybe I am not aware of how it should work, but I am stuck right now as I don't need a publishable library inside my monorepo but because of this issue, I have to use @nrwl/angular:package. Please help me understand if I am doing something wrong. I really appreciate any help you can provide.

Expected Behavior

@nrwl/angular:ng-packagr-lite should compile correctly with partially compiled Angular libraries.

Github Repo

https://github.com/VanTigranyan/test-ngxs-nx/tree/master/libs/store

Steps to Reproduce

1.Run "nx build store".

  1. Errors say that NgxsRootModule is not an NgModule or it is not compiled correctly with ngcc.
  2. If you change the executor to @nrwl/angular:package, it compiles correctly.

Nx Report

Node : 16.18.1
   OS   : darwin x64
   npm  : 9.1.2

   nx : 15.2.1
   @nrwl/angular : 15.2.1
   @nrwl/cypress : 15.2.1
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.2.1
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : Not Found
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.2.1
   @nrwl/js : 15.2.1
   @nrwl/linter : 15.2.1
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/rollup : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.2.1
   @nrwl/web : Not Found
   @nrwl/webpack : 15.2.1
   @nrwl/workspace : 15.2.1
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

Failure Logs

> nx run store:build:production

Building Angular Package

------------------------------------------------------------------------------
Building entry point 'store'
------------------------------------------------------------------------------

 >  NX   libs/store/src/lib/store.module.ts:22:12 - error NG6002: 'NgxsRootModule' does not appear to be an NgModule class.

   22   imports: [
                 ~
   23     NgxsModule.forRoot([TestState]),
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   24   ],
      ~~~

     node_modules/@ngxs/store/src/modules/ngxs-root.module.d.ts:11:22
       11 export declare class NgxsRootModule {
                               ~~~~~~~~~~~~~~
       This likely means that the library (@ngxs/store) which declares NgxsRootModule has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.

Error: libs/store/src/lib/store.module.ts:22:12 - error NG6002: 'NgxsRootModule' does not appear to be an NgModule class.

22   imports: [
              ~
23     NgxsModule.forRoot([TestState]),
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24   ],
   ~~~

  node_modules/@ngxs/store/src/modules/ngxs-root.module.d.ts:11:22
    11 export declare class NgxsRootModule {
                            ~~~~~~~~~~~~~~
    This likely means that the library (@ngxs/store) which declares NgxsRootModule has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.

    at /Users/vtigranyan/projects/wf-fusion/test-ngxs/node_modules/@nrwl/angular/src/executors/ng-packagr-lite/ng-packagr-adjustments/ngc/compile-source-files.js:138:19
    at Generator.next (<anonymous>)
    at fulfilled (/Users/vtigranyan/projects/wf-fusion/test-ngxs/node_modules/@nrwl/angular/node_modules/tslib/tslib.js:115:62)

 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Running target "store:build" failed

   Failed tasks:

   - store:build:production

   Hint: run the command with --verbose for more details.

Additional Information

No response

VanTigranyan commented 1 year ago

@leosvelperez Hi. Any input you can provide here? Thanks in advance.

leosvelperez commented 1 year ago

hey @VanTigranyan!

The ng-packagr-lite executor doesn't run ngcc on its own when building a library. It relies on you having run ngcc before building the library. That's correctly stated on the docs, as you can see here https://nx.dev/recipes/other/setup-incremental-builds-angular#requirements.

I'd also recommend reading when to use incremental builds. It's not always a good choice and depends on your application size. Only large applications would benefit from it in a way that outweighs the overhead introduced. If you do need to optimize your application's build time, I recommend you have a look first at using Module Federation for faster builds.

leosvelperez commented 1 year ago

I'm closing this since the executor is working as expected. If you still think there's an issue, please leave a comment and we can revisit it.

VanTigranyan commented 1 year ago

Hi, again @leosvelperez. Thanks a lot for the quick response. Actually IMHO there are a couple of issues.

  1. The documentation here https://nx.dev/packages/angular/executors/ng-packagr-lite#@nrwl/angular:ng-packagr-lite should clearly state what you mentioned. I don't think that it is fair that we should read the full documentation in order to understand how we should use executors if the doc of the executor itself doesn't provide those details. I spent a week figuring out what was wrong with my code, tried to search in docs for anything relevant, and did not succeed.
  2. The doc you sent says the workspace comes preconfigured to run ngcc in the post-install hook when it is not the case. I generated the repository from scratch with angular preset and post-install contains only decorating ng-cli, not ngcc compilation, so again, one should go over the whole documentation in order to know that this is a requirement.
leosvelperez commented 1 year ago

@VanTigranyan that's fair! I can see the issue. We see it natural for folks to discover the ng-packagr-lite executor (or using buildable libraries) when coming from the docs explaining incremental builds, but we can/should definitely improve the executor docs to include that info. The incremental builds recipe requirements is just outdated, we no longer generate workspaces with a postinstall script to run ngcc by default because more and more libraries are moving to full Ivy and don't require it. So, it should be added by developers should they need it. I'll amend that as well.

VanTigranyan commented 1 year ago

Thanks a lot, @leosvelperez. I appreciate your help and also the consideration of changing docs a bit.

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.