nrwl / nx

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

Angular application with `buildLibsFromSource: false` triggers unwanted rebuilds of unchanged dependable workspace libraries #27081

Closed iRoyPeleg closed 2 weeks ago

iRoyPeleg commented 1 month ago

Current Behavior

In an Angular project, if buildLibsFromSource is set to false, and you are running an application with either build --watch or serve with a webpack-browser configuration, all the workspace libraries that the application depends on will always rerun build.

This is due to this part of the code in webpack-browser.impl.ts & dev-server.impl.ts in the angular package:

baseWebpackConfig.plugins.push(
  // @ts-expect-error - difference between angular and webpack plugin definitions bc of webpack versions
  new WebpackNxBuildCoordinationPlugin(
    `nx run-many --target=${
      context.target.target
    } --projects=${workspaceDependencies.join(',')}`,
    skipInitialRun
  )
);

What happens here, is that you use the WebpackNxBuildCoordinationPlugin, which then runs the nx run-many command on every change that is being made in the application.

The problem is even more evident in serve, where if you make a change in the application, the app compiles and outputs a delta change - and then all the libraries are still being rebuilt after the application was "already rebuilt".

Thanks.

Expected Behavior

During either build --watch or serve, if an app depends on libraries, but they have not changed - the build process for said libraries should not be retriggered.

Only the delta changed libraries should be triggered, and in the correct order.

GitHub Repo

No response

Steps to Reproduce

  1. Create an angular workspace with webpack based implementation
  2. Create an application
  3. Configure buildLibsFromSource: false for the application project.json
  4. Create at least 1 library
  5. Have the application depend on the newly created library
  6. Run the app either via build --watch or via serve
  7. Make a small change to the app to trigger build process (and not in the library)

Here you will see the library recompiles again.

Nx Report

NX   Report complete - copy this into the issue template

Node   : 18.16.1
OS     : win32-x64
pnpm   : 8.14.1

nx                 : 19.5.0
lerna              : 3.10.2
@nx/js             : 19.5.0
@nx/jest           : 19.5.0
@nx/linter         : 19.5.0
@nx/eslint         : 19.5.0
@nx/workspace      : 19.5.0
@nx/angular        : 19.5.0
@nx/cypress        : 19.5.0
@nx/devkit         : 19.5.0
@nx/eslint-plugin  : 19.5.0
@nx/plugin         : 19.5.0
@nrwl/tao          : 19.5.0
@nx/web            : 19.5.0
@nx/webpack        : 19.5.0
typescript         : 5.3.3
---------------------------------------
Community plugins:
@ngneat/spectator                    : 16.0.0
@ngrx/component                      : 17.1.0
@ngrx/component-store                : 17.1.0
@ngrx/effects                        : 17.1.0
@ngrx/entity                         : 17.1.0
@ngrx/router-store                   : 17.1.0
@ngrx/schematics                     : 17.1.0
@ngrx/store                          : 17.1.0
@ngrx/store-devtools                 : 17.1.0
@progress/kendo-angular-buttons      : 8.2.2
@progress/kendo-angular-charts       : 7.3.2
@progress/kendo-angular-common       : 3.2.2
@progress/kendo-angular-dateinputs   : 7.1.6
@progress/kendo-angular-dialog       : 7.1.5
@progress/kendo-angular-dropdowns    : 7.2.4
@progress/kendo-angular-editor       : 4.2.0
@progress/kendo-angular-excel-export : 5.0.2
@progress/kendo-angular-grid         : 7.4.2
@progress/kendo-angular-inputs       : 9.0.5
@progress/kendo-angular-intl         : 4.1.1
@progress/kendo-angular-l10n         : 4.0.1
@progress/kendo-angular-label        : 4.0.2
@progress/kendo-angular-layout       : 7.2.0
@progress/kendo-angular-pdf-export   : 4.0.1
@progress/kendo-angular-popup        : 5.0.2
@progress/kendo-angular-progressbar  : 3.1.2
@progress/kendo-angular-toolbar      : 6.1.2
@progress/kendo-angular-treeview     : 7.1.5
ngrx-immer                           : 1.0.3
---------------------------------------
Local workspace plugins:
         @algotec/workspace-plugin

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

Coly010 commented 1 month ago

Hey @iRoyPeleg :)

I'm going to need you to provide a repo with a reproduction. You can see I have an app that depends on a buildable library here, have buildLibsFromSource: true and I can't reproduce it https://github.com/Coly010/nx27081.git

The NxWebpackBuildCoordinationPlugin also shouldn't be set up when buildLibsFromSource is set to true.

iRoyPeleg commented 1 month ago

@Coly010 , you are of course 100% correct.

Obviously I ment buildLibsFromSource: false, for some reason I switched it up in my head and wrote it all over the ticket the other way around, now edited it.

If buildLibsFromSource: true, no actual libraries needs to be built anyhow, so of course it's the false scenario, again, my bad.

I wanted to verify it on your reproduction, but it appears in your repo it's only the app, no libs inside? maybe you've done it locally and didnt push. There are more discrepancies in that repo (like it's not the webpack variant in the build executor), but I assume it's just not pushed and locally it's configured correctly.

In case you do have a build locally on your end and just need to turn the buildLibsFromSource: false flag, that'll be great, otherwise I'll try to recreate it via a new repo.

Thank you very much!

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because no reproduction was provided within 7 days. Please help us help you. Providing a repository exhibiting the issue helps us diagnose and fix the issue. Any time that we spend reproducing this issue is time taken away from addressing this issue and other issues. This issue will be closed in 21 days if a reproduction is not provided. If a reproduction has been provided, please reply to keep it active. Thanks for being a part of the Nx community! 🙏

iRoyPeleg commented 1 month ago

Hi @Coly010 !

I've reproduced the issue: https://github.com/iRoyPeleg/nx27081

run nx serve myapp and then alter the app.component.html to see mylib being rebuild as well.

Thanks!

Coly010 commented 2 weeks ago

Hey @iRoyPeleg Thanks for the reproduction! Very much appreciated.

After checking this out on the reproduction, I can see that it does build when you make a change to app.component.html, however, I can also see that it will fetch this build from cache, if the library itself (or its dependents) have not changed

image

Therefore this doesn't add much overhead to the rebuild on app change. This is working as we'd expect it to (reading from cache when possible).

This might not be what you expected, but I'm going to close this issue as there is no bug here. I hope the information above helps to explain the additional build logs you see.