Skip to content
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

[Module Federation] collision between modules of differents remotes. #5308

Closed
penguintree opened this issue Aug 17, 2021 · 15 comments · Fixed by #5309
Closed

[Module Federation] collision between modules of differents remotes. #5308

penguintree opened this issue Aug 17, 2021 · 15 comments · Fixed by #5309

Comments

@penguintree
Copy link

Bug report

What is the current behavior?
A host consumes two different remotes.
Both remotes have the same file structure, i.e. both exposes ./src/main.js under different names.

remote package 1 :

const moduleFederation = new ModuleFederationPlugin({
   name: 'somePackage',
   filename: 'somePackage.entrypoint.js',
   exposes: {
      './x': './src/main'
   }
});

remote package 2 :

const moduleFederation = new ModuleFederationPlugin({
   name: 'anotherPackage',
   filename: 'anotherPackage.entrypoint.js',
   exposes: {
      './y': './src/main'
   }
});

host :

const moduleFederation = new ModuleFederationPlugin({
   name: 'host',
   filename: 'host.entrypoint.js',
   remotes: {
      'firstRemote': 'somePackage@http://localhost:9001/somePackage.entrypoint.js',
      'secondRemote': 'anotherPackage@http://localhost:9002/anotherPackage.entrypoint.js'
   }
});

In the host app, when using both federated modules, the first one overrides the second.

import firstPackage from 'firstRemote/x';
import secondPackage from 'secondRemote/y'; // here object from firstRemote/x is imported, but it's not the same instance.

If the current behavior is a bug, please provide the steps to reproduce.
This repository reproduces the bug.

What is the expected behavior?
Both modules from both remotes should be loaded independently, as the similarities in the exposed file name is merely a coincidence and is not known by the host.

Other relevant information:
webpack version: 5.50.0
Node.js version: 14.17.0
Operating System: Windows 10 20H2 19042.1110
Additional tools: n/a

@webpack-bot
Copy link

webpack-bot commented Aug 17, 2021

For maintainers only:

  • webpack-4
  • webpack-5
  • bug
  • critical-bug
  • enhancement
  • documentation
  • performance
  • dependencies
  • question

@alexander-akait
Copy link
Member

Please update webpack to the latest version

@penguintree
Copy link
Author

Please update webpack to the latest version

It's already 5.50.0. I missed a « 0 » in my post, corrected.

@penguintree
Copy link
Author

@alexander-akait I don't understand why it is labelled as "question". I really think it is a bug. Can you explain ? Maybe it is part of the process.

@alexander-akait
Copy link
Member

alexander-akait commented Aug 17, 2021

Because we have a lot of tests for module federation and most likely there is no bug here, only but no one has looked yet, it is in my todo

@alexander-akait
Copy link
Member

alexander-akait commented Aug 19, 2021

TypeError: secondRemote_y__WEBPACK_IMPORTED_MODULE_1___default(...).doSomethingWithPackage2 is not a function

Are you sure reproducible test repo works fine?

@alexander-akait
Copy link
Member

It was answered a lot of time:

https://github.com/penguintree/webpack-module-federation-module-cache-issue/blob/main/remote-package-1/package.json#L2
https://github.com/penguintree/webpack-module-federation-module-cache-issue/blob/main/remote-package-2/package.json#L2

Webpack generates runtime based on uniqueName (webpack generates shared function, look at source code, webpackChunktoolpackage_v1), you have the same uniqueName for different packages, so you share code between remotes, it creates collisions, here logic for default value https://github.com/webpack/webpack/blob/main/lib/config/defaults.js#L616, so you should have different names

@alexander-akait alexander-akait transferred this issue from webpack/webpack Aug 19, 2021
@alexander-akait
Copy link
Member

@penguintree
Copy link
Author

TypeError: secondRemote_y__WEBPACK_IMPORTED_MODULE_1___default(...).doSomethingWithPackage2 is not a function

Are you sure reproducible test repo works fine?

Yes the error is the symptom. The function doSometingWithPackage2 do exists in remote-pacakge-2 and should be available.

@alexander-akait
Copy link
Member

Change name in package.json for remote-package-1 and remote-package-2 and all will be work fine

chenxsan added a commit to chenxsan/webpack.js.org that referenced this issue Aug 19, 2021
@penguintree
Copy link
Author

Thanks a lot.
I would have thought that the name provided in ModuleFederationPlugin would be sufficient to make to completely independant packages. It looks like both must be unique, the name in the plugin and in package.json.

@alexander-akait
Copy link
Member

In theory you can create shared remotes between different builds, it is very exotic, but it is possible

@penguintree
Copy link
Author

I ran in this issue while exploring how I could handle different remote packages version in a rather large federated application. I didn't found a lot of information about remote versioning apart from a video from Jack Herrington based on unpkg. Is there any official guidelines or documentation you are aware of about this ?

@alexander-akait
Copy link
Member

I think better to open another issue about this and ask directly questions, so we will improve this, module federation is real big theme, so there are a lot of complex information

@Cantarella
Copy link

На этот вопрос отвечали много раз:

https://github.com/penguintree/webpack-module-federation-module-cache-issue/blob/main/remote-package-1/package.json#L2 https://github.com/penguintree/webpack-module -federation-module-cache-issue/blob/main/remote-package-2/package.json#L2

Webpack генерирует среду выполнения на основе uniqueName(webpack генерирует общую функцию, посмотрите исходный код webpackChunktoolpackage_v1), у вас есть то же самое uniqueNameдля разных пакетов, поэтому вы делитесь кодом между remotes, это создает коллизии, вот логика для значения по умолчанию https://github.com/webpack/webpack/blob/main/lib/config/defaults.js#L616 , поэтому у вас должны быть разные имена

despite the fact that it has been answered many times - it was this answer of yours that helped me. Thank you)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants