Skip to content

fix(webpack-bundler-runtime): preserve share scopes on repeated container init - #4969

Open
BaurinVladislav wants to merge 1 commit into
module-federation:mainfrom
BaurinVladislav:fix/issue-4691
Open

fix(webpack-bundler-runtime): preserve share scopes on repeated container init#4969
BaurinVladislav wants to merge 1 commit into
module-federation:mainfrom
BaurinVladislav:fix/issue-4691

Conversation

@BaurinVladislav

@BaurinVladislav BaurinVladislav commented Aug 6, 2026

Copy link
Copy Markdown

Description

Fixes #4691 — repeated dynamic remote container init can lose non-default share scopes.

Root cause

When a host re-initializes a remote container with an incomplete shareScopeMap (empty, truncated, or missing non-default scope keys), the previous code would:

  1. Pass empty objects from the truncated hostShareScopeMap to initShareScopeMap
  2. initShareScopeMap would replace (not merge) the existing scope, wiping previously registered shared packages

This caused shared singletons to resolve different runtime instances across remotes, leading to crashes like No QueryClient set.

Fix

Two-level defense:

  1. initContainerEntry.tsresolveShareScope with clear precedence: non-empty host scope > existing federation scope > fallback. When the re-init provides an empty or missing scope entry, the existing scope from the federation instance is used instead.

  2. runtime-coreinitShareScopeMap now merges incoming scopes with existing ones ({...existing, ...incoming}) instead of replacing. This provides defense-in-depth in case resolveShareScope is bypassed or the same pattern appears in other callers.

Changes

File Change
webpack-bundler-runtime/src/initContainerEntry.ts Add resolveShareScope with fallback to existingShareScopeMap
runtime-core/src/shared/index.ts Merge instead of replace in initShareScopeMap
runtime-core/src/shared/disabled.ts Same merge for DisabledSharedHandler
webpack-bundler-runtime/__tests__/initContainerEntry.array.spec.ts Regression tests: full → empty → missing custom scope + isolation assertions
webpack-bundler-runtime/__tests__/initContainerEntry.non-array.spec.ts Same for string shareScopeKey path
runtime/__tests__/shares.spec.ts Tests for merge behavior in initShareScopeMap

Validation

  • webpack-bundler-runtime: 103/103 tests passed
  • runtime: 96/96 tests passed
  • runtime-core: 96/96 tests passed
  • ESLint: 0 errors across all changed packages

Notes

  • CI checks may require maintainer approval (first-time contributor)
  • change: fix label needed (no permissions on upstream)

Related Issue

Closes #4691

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dd53c7a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 47 packages
Name Type
@module-federation/webpack-bundler-runtime Patch
@module-federation/runtime-core Patch
@module-federation/enhanced Patch
@module-federation/esbuild Patch
@module-federation/nextjs-mf Patch
@module-federation/runtime-tools Patch
@module-federation/runtime Patch
@module-federation/bridge-react Patch
@module-federation/modern-js-v3 Patch
@module-federation/modern-js Patch
@module-federation/node Patch
@module-federation/rsbuild-plugin Patch
@module-federation/rspress-plugin Patch
@module-federation/storybook-addon Patch
shared-tree-shaking-no-server-host Patch
shared-tree-shaking-no-server-provider Patch
@module-federation/rspack Patch
@module-federation/inject-external-runtime-core-plugin Patch
@module-federation/devtools Patch
@module-federation/dts-plugin Patch
@module-federation/metro Patch
@module-federation/observability-plugin Patch
@module-federation/playground Patch
@module-federation/retry-plugin Patch
@module-federation/bridge-vue3 Patch
website-new Patch
shared-tree-shaking-with-server-host Patch
shared-tree-shaking-with-server-provider Patch
node-dynamic-remote-new-version Patch
node-dynamic-remote Patch
remote5 Patch
remote6 Patch
@module-federation/metro-plugin-rnc-cli Patch
@module-federation/metro-plugin-rnef Patch
@module-federation/metro-plugin-rock Patch
@module-federation/sdk Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/error-codes Patch
create-module-federation Patch
@module-federation/cli Patch
@module-federation/treeshake-server Patch
@module-federation/treeshake-frontend Patch
@module-federation/utilities Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@BaurinVladislav

Copy link
Copy Markdown
Author

@2heal1 — this builds on your PR #4698. Added a merge in initShareScopeMap (runtime-core) for defense-in-depth, regression tests for both code paths (array + non-array shareScopeKey), and tests for the merge behavior. All tests green (103 + 96 + 96), ESLint clean. Would appreciate a review.

@BaurinVladislav

Copy link
Copy Markdown
Author

@ScriptedAlchemy — fixes #4691 where repeated remote container init with an incomplete shareScopeMap wipes non-default share scopes, causing shared singletons to resolve different instances across remotes. All tests pass, ESLint clean. Ready for review.

@BaurinVladislav

Copy link
Copy Markdown
Author

Gentle nudge — we're waiting on this fix to land before we can roll it out across several production repositories. Would really appreciate a review when you have a moment. Happy to adjust anything needed.

…iner init

resolveShareScope now falls back to the federation instance's existing
shareScopeMap when the incoming host scope is missing or empty.
initShareScopeMap merges incoming scopes with existing ones instead of
replacing them, providing defense-in-depth against incomplete
re-initialization data.

Fixes module-federation#4691
@BaurinVladislav

Copy link
Copy Markdown
Author

@2heal1 @ScriptedAlchemy — could you please review when you have a moment? This builds on #4698: fixes repeated container init wiping non-default share scopes. All tests green (103 + 96 + 96), ESLint clean. Rebased on latest main.

@BaurinVladislav

Copy link
Copy Markdown
Author

@ryok90
@zackarychapple
@chenjiahan
could you please review when you have a moment? This builds on #4698: fixes repeated container init wiping non-default share scopes. All tests green (103 + 96 + 96), ESLint clean. Rebased on latest main.

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 this pull request may close these issues.

bug: repeated dynamic remote init can lose non-default shareScopeMap

1 participant