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/runtime init method not executing as expected #2584

Closed
2 tasks done
xiaoYuanDun opened this issue Jun 6, 2024 · 17 comments
Closed
2 tasks done

@module-federation/runtime init method not executing as expected #2584

xiaoYuanDun opened this issue Jun 6, 2024 · 17 comments

Comments

@xiaoYuanDun
Copy link

xiaoYuanDun commented Jun 6, 2024

Clear and concise description of the problem

there are 2 app: "app1", "app2", and now i want change some config for "app2", and i call init under the scope of app1, like this
init({ name: 'app2', remotes: { ... } })

image

but is not working,when i debug code,i find there is a buildId in getGlobalFederationInstance, and the value of buildId is always "app1". the FEDERATION_BUILD_IDENTIFIER stop me, because app1 build with MF_Plugin.

so i want to know, if i want to change init option between different scope, how should i do ?

// ----------------------------------------------------------------

大概描述一下:
目前有两个 app:app1 和 app2,需要在 app1 中对 app2 的配置做一些更新,这时在 app1 的 scope 下调用 init

init({ name: 'app2', remotes: { ... } })

但是此时 app2 的 MF 实例中并没有更新,debug 发现是因为 getGlobalFederationInstance 方法中会判断一个 buildId ,如果 app1 是通过构建时引用 MF 插件,则会生产一个 FEDERATION_BUILD_IDENTIFIER ,导致这里的 buildId 永远都是 "app1"。
image

想要了解一下,这里为什么要对构建时的 MF 实例做这个限制,现在 init 方式已经提供了 name 属性,应该就是为了可以跨实例进行配置修改吧,所以在 getGlobalFederationInstance 寻找 MF 实例时,是不是以 name 作为最高优先级的查找条件比较合适。

Suggested solution

getGlobalFederationInstance 方式可以优先通过入参的 name 来查找 MF 实例

Alternative

No response

Additional context

No response

Validations

  • Read the Contributing Guidelines.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Tasks

Preview Give feedback
No tasks being tracked yet.
@xiaoYuanDun xiaoYuanDun changed the title @module-federation/runtime init 方法未按预期执行初始化 @module-federation/runtime init method not executing as expected Jun 7, 2024
@ScriptedAlchemy
Copy link
Member

please provide a repo to debug

Copy link
Contributor

Hello @xiaoYuanDun. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 5 days.

@xiaoYuanDun
Copy link
Author

xiaoYuanDun commented Jun 12, 2024

please provide a repo to debug

@ScriptedAlchemy

here is the repo, thank you

https://stackblitz.com/~/github.com/xiaoYuanDun/module-federation-demo?file=README.md

@ScriptedAlchemy
Copy link
Member

@xiaoYuanDun according to the source code, this is correct. builder id is the ID of the host.

@xiaoYuanDun
Copy link
Author

xiaoYuanDun commented Jun 12, 2024

@xiaoYuanDun according to the source code, this is correct. builder id is the ID of the host.

yeah, it work, but i wonder why the buildId is the highest priority

in this case, can I only use the init from @module-federation/runtime ?(because i import this from @module-federation/runtime in app1 and save it on window. i dont want do it again)

i dont know if The purpose of providing name to the Developer is to resolve this case here.

if so, when i defined the name = app2, actually i want init the app2, but it stopped by buildId, Is it more reasonable to first judge by the name passed in from the outside?

@xiaoYuanDun
Copy link
Author

@ScriptedAlchemy

If this is a meaningful question, Could you please take a look at this when you have a moment ?I will continue to follow it. THX

@ScriptedAlchemy
Copy link
Member

So you are trying to init the same app as two different names?

@xiaoYuanDun
Copy link
Author

xiaoYuanDun commented Jun 20, 2024

So you are trying to init the same app as two different names?

@ScriptedAlchemy

no, not the same app, different app has their own mf_instance in window.__FEDERATION__.__INSTANCES__ ,right ?

in my case ,the app1 is the main app, it provides a global function, like init, loadRemote, so that the subApp do not need import { init } from "@module-federation/runtime" in their code again, just use the global function providered by app1 on window.

i want manage all the instances in window.__FEDERATION__.__INSTANCES__ with the global method provided by app1, the new api init input parameter requires the name parameter, i guess it's to find the different mf_instance ?

Dose the init can only work with their own scope ? (beacuse init can only find their own mf_instance)

@xiaoYuanDun
Copy link
Author

xiaoYuanDun commented Jun 20, 2024

and , according to the source code, if the app is not built via new MF plugin, they will not has buildId, so the getGlobalFederationInstance will work just as i expected.

i can fix my case by this way, it need move my config in MF_Plugin to fully dynamic runtime, but I feel like this is not a good solution.

if the getGlobalFederationInstance find the mf_instance by name first, no matter whether the buildId exists, that would be nice.

I haven’t seen any special logic related to FEDERATION_BUILD_IDENTIFIER yet. wonder what is the meaning of distinguishing whether it contains buildId?

@ScriptedAlchemy
Copy link
Member

are you trying to recycle the runtime code from the host?
Each instance is unique and during compile, data is encoded into the runtime for that specific host.

By setting a global instance, you are communicating with the host instance only, not the remote instances.

there should be 1 instance of federation per remote, you should have 3 instances created.

By using the global host instance, you are just communicating as the host, dispatching commands in the host who is encoded as app1

@xiaoYuanDun
Copy link
Author

xiaoYuanDun commented Jun 26, 2024

@ScriptedAlchemy

I wrote a new repo here to see if it can be clearer, the steps is in the readme.md

@ScriptedAlchemy
Copy link
Member

You would need to expose a global function that executes init and returns a instance. You cannot execute init and make the return global

@xiaoYuanDun
Copy link
Author

xiaoYuanDun commented Jul 1, 2024

You would need to expose a global function that executes init and returns a instance. You cannot execute init and make the return global

@ScriptedAlchemy

Please excuse me, I don't think I fully grasped what you said.

Just let's focus back here repo , is there any way to make the execution of app1-via-build and app1 behave the same way ?

And yes, I'm able to get my project working for now by switching between app1-via-build and app1 like this, but will you consider unifying the behavior of the two in the future?

Copy link
Contributor

github-actions bot commented Jul 7, 2024

As the issue was labelled with need reproduction, but no response in 5 days. This issue will be closed. Feel free to comment and reopen it if you have any further questions. For background, see Why reproductions are required.

由于该 issue 被标记为 "需要重现",但在 5 天内没有回应,因此该 issue 将被关闭。如果你有任何进一步的问题,请随时发表评论并重新打开该 issue。背景请参考 为什么需要最小重现

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 7, 2024
@xiaoYuanDun
Copy link
Author

As the issue was labelled with need reproduction, but no response in 5 days. This issue will be closed. Feel free to comment and reopen it if you have any further questions. For background, see Why reproductions are required.

由于该 issue 被标记为 "需要重现",但在 5 天内没有回应,因此该 issue 将被关闭。如果你有任何进一步的问题,请随时发表评论并重新打开该 issue。背景请参考 为什么需要最小重现

the repo is here, @ScriptedAlchemy

but it closed automatically, Is anyone still working on this?

@zackarychapple zackarychapple reopened this Jul 8, 2024
Copy link
Contributor

As the issue was labelled with need reproduction, but no response in 5 days. This issue will be closed. Feel free to comment and reopen it if you have any further questions. For background, see Why reproductions are required.

由于该 issue 被标记为 "需要重现",但在 5 天内没有回应,因此该 issue 将被关闭。如果你有任何进一步的问题,请随时发表评论并重新打开该 issue。背景请参考 为什么需要最小重现

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
@asling
Copy link

asling commented Jul 15, 2024

get same issue, any solutions?

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

No branches or pull requests

4 participants