angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.74k stars 11.98k forks source link

Multiple assets emit different content to the same filename error for assets with persistent cache #21576

Open pshurygin opened 3 years ago

pshurygin commented 3 years ago

🐞 Bug report

Command (mark with an x)

Is this a regression?

No

Description

This issue is a continuation for the https://github.com/angular/angular-cli/issues/12186. The fix, introduced for that issue, does not correctly work when using webpack 5 persistent cache.

The problem comes from the fact that when you have multiple assets with the same name, but different folder, CLI emits the first visited asset to the root of fs(disregarding it's folder path), while emiting subsequent assets with this name to their corresponding folders.

To do that Angular CLI keeps track of the emitted(visited) files in-memory, and it does not work when part of the compilation was restored from file system cache. This makes it so that CLI does not realise that a certain svg file was previously emited to the root of the fs, which in turn may lead to the emition of a different svg file(with the same name, but different content) to the same place in the root of the fs, triggering webpack emit conflict error.

πŸ”¬ Minimal Reproduction

  1. Enable webpack 5 persistent cache feature
  2. Make sure the cache is clear
  3. Create a project with 2 different SVG assets with the same name, but place them in two different folders
  4. Import one of this images somewhere in the code of the project
  5. Run ng build. The svg is emitted to the root of fs, and is cached
  6. Change the code to import second image right before the first one
  7. Run ng build again. CLI will try to emit the second svg to the root of fs, but there is already the first svg retrieved from the cache there.
  8. You should see the webpack emit conflict error.

πŸ”₯ Exception or Error

Error: Conflict: Multiple assets emit different content to the same filename empty.svg

🌍 Your Environment


Angular CLI: 12.2.0
Node: 14.17.3
Package Manager: npm 6.14.13
OS: darwin x64

Angular: 12.2.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.0
@angular-devkit/build-angular   12.2.0
@angular-devkit/core            12.2.0
@angular-devkit/schematics      12.2.0
@schematics/angular             12.2.0
rxjs                            6.6.7
typescript                      4.2.4
webpack                         5.47.1
alan-agius4 commented 3 years ago

Hi @pshurygin, I tried to replicate this locally, but I am not managing to replicate the issue.

Screenshot 2021-08-14 at 12 49 35

Can you setup a minimal repro please?

You can read here why this is needed. A good way to make a minimal repro is to create a new app via ng new repro-app and adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.

This might be related to your directory structure so its really important to get an accurate repro to diagnose this.

pshurygin commented 3 years ago

@alan-agius4 Are you using actually different images for that? If so, I guess one might also need to have import of the second asset from a different(lazy loaded) chunk, as this is how we have it in our repo. I will try to make a minimal reproduction repo for that.

alan-agius4 commented 3 years ago

Yes, the 2 images are different.

pshurygin commented 3 years ago

@alan-agius4 Yes, you need your second asset to be in a different bundle. I've also didn't mention that you have to run build in development mode, otherwise file names are hashed. Here is the reproduction repo

  1. Comment out contents of child.component.scss
  2. run NG_PERSISTENT_BUILD_CACHE=1 ng build --configuration development
  3. Uncomment contents of child.component.scss
  4. run NG_PERSISTENT_BUILD_CACHE=1 ng build --configuration development again
hschmidtchen commented 1 year ago

Hi @alan-agius4, I would like to provide a data point that this issue still persists. This thread helped me to solve a similar issue which was caused by two independent third-party dependencies adding equally named static assets to the build (fonts). Luckily, one of the dependencies could be replaced but if that was not the case, we would have been forced to disable angular caching for local development.