-
-
Notifications
You must be signed in to change notification settings - Fork 409
fix(runtime-core): route manifest validation errors through errorLoadRemote #4654
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
Open
sessa
wants to merge
3
commits into
module-federation:main
Choose a base branch
from
sessa:fix/error-load-remote-manifest-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
233 changes: 233 additions & 0 deletions
233
packages/runtime-core/__tests__/error-load-remote-manifest.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| import { assert, describe, it, expect, vi } from 'vitest'; | ||
| import { ModuleFederation } from '../src/core'; | ||
| import type { ModuleFederationRuntimePlugin } from '../src/type/plugin'; | ||
| import { mockStaticServer, removeScriptTags } from './mock/utils'; | ||
| import { resetFederationGlobalInfo } from '../src/global'; | ||
|
|
||
| mockStaticServer({ | ||
| baseDir: __dirname, | ||
| filterKeywords: [], | ||
| basename: 'http://localhost:1111/', | ||
| }); | ||
|
|
||
| describe('errorLoadRemote — manifest validation errors', () => { | ||
| beforeEach(() => { | ||
| resetFederationGlobalInfo(); | ||
| removeScriptTags(); | ||
| }); | ||
|
|
||
| it('calls errorLoadRemote when manifest is valid JSON but missing required fields', async () => { | ||
| const errorLoadRemoteSpy = vi.fn(); | ||
|
|
||
| const incompleteManifest = { | ||
| id: '@test/bad-remote', | ||
| name: '@test/bad-remote', | ||
| }; | ||
|
|
||
| const fetchPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-fetch-plugin', | ||
| fetch(url) { | ||
| if (url.includes('bad-manifest')) { | ||
| return Promise.resolve( | ||
| new Response(JSON.stringify(incompleteManifest), { | ||
| status: 200, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }), | ||
| ); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const errorHandlerPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-error-handler', | ||
| errorLoadRemote(args) { | ||
| errorLoadRemoteSpy(args); | ||
| return undefined; | ||
| }, | ||
| }); | ||
|
|
||
| const FM = new ModuleFederation({ | ||
| name: '@test/host', | ||
| remotes: [ | ||
| { | ||
| name: '@test/bad-remote', | ||
| entry: 'http://localhost:9999/bad-manifest/mf-manifest.json', | ||
| }, | ||
| ], | ||
| plugins: [fetchPlugin(), errorHandlerPlugin()], | ||
| }); | ||
|
|
||
| await expect( | ||
| FM.loadRemote('@test/bad-remote/someExpose'), | ||
| ).rejects.toThrow(); | ||
|
|
||
| expect(errorLoadRemoteSpy).toHaveBeenCalled(); | ||
| const callArgs = errorLoadRemoteSpy.mock.calls[0][0]; | ||
| expect(callArgs.lifecycle).toBe('afterResolve'); | ||
| expect(callArgs.error).toBeDefined(); | ||
| expect(String(callArgs.error)).toContain('Missing required fields'); | ||
| }); | ||
|
|
||
| it('recovers via errorLoadRemote when manifest has missing fields and plugin returns fallback', async () => { | ||
| const validManifestData = { | ||
| id: '@test/bad-remote', | ||
| name: '@test/bad-remote', | ||
| metaData: { | ||
| name: '@test/bad-remote', | ||
| publicPath: 'http://localhost:1111/', | ||
| type: 'app', | ||
| buildInfo: { buildVersion: 'custom' }, | ||
| remoteEntry: { | ||
| name: 'federation-remote-entry.js', | ||
| path: 'resources/hooks/app2/', | ||
| }, | ||
| types: { name: 'index.d.ts', path: './' }, | ||
| globalName: '@loader-hooks/app2', | ||
| }, | ||
| remotes: [], | ||
| shared: [], | ||
| exposes: [], | ||
| }; | ||
|
|
||
| const incompleteManifest = { | ||
| id: '@test/bad-remote', | ||
| name: '@test/bad-remote', | ||
| }; | ||
|
|
||
| let fetchCallCount = 0; | ||
| const fetchPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-fetch-plugin', | ||
| fetch(url) { | ||
| if (url.includes('bad-manifest')) { | ||
| fetchCallCount++; | ||
| if (fetchCallCount === 1) { | ||
| return Promise.resolve( | ||
| new Response(JSON.stringify(incompleteManifest), { | ||
| status: 200, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const errorHandlerPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-error-handler', | ||
| errorLoadRemote({ lifecycle }) { | ||
| if (lifecycle === 'afterResolve') { | ||
| return validManifestData; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const FM = new ModuleFederation({ | ||
| name: '@test/host-recover', | ||
| remotes: [ | ||
| { | ||
| name: '@test/bad-remote', | ||
| entry: 'http://localhost:9999/bad-manifest/mf-manifest.json', | ||
| }, | ||
| ], | ||
| plugins: [fetchPlugin(), errorHandlerPlugin()], | ||
| }); | ||
|
|
||
| const module = await FM.loadRemote<() => string>('@test/bad-remote/say'); | ||
| assert(module); | ||
| expect(module()).toBe('hello app2'); | ||
| }); | ||
|
|
||
| it('rejects when errorLoadRemote returns an invalid failover manifest', async () => { | ||
| const errorLoadRemoteSpy = vi.fn(); | ||
| const invalidFallback = { | ||
| id: '@test/bad-remote', | ||
| name: '@test/bad-remote', | ||
| }; | ||
|
|
||
| const fetchPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-fetch-plugin', | ||
| fetch(url) { | ||
| if (url.includes('bad-manifest')) { | ||
| return Promise.resolve( | ||
| new Response(JSON.stringify({ id: 'x', name: 'x' }), { | ||
| status: 200, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }), | ||
| ); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const errorHandlerPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-error-handler', | ||
| errorLoadRemote(args) { | ||
| errorLoadRemoteSpy(args); | ||
| if (args.lifecycle === 'afterResolve') { | ||
| return invalidFallback; | ||
| } | ||
| return undefined; | ||
| }, | ||
| }); | ||
|
|
||
| const FM = new ModuleFederation({ | ||
| name: '@test/host-bad-fallback', | ||
| remotes: [ | ||
| { | ||
| name: '@test/bad-remote', | ||
| entry: 'http://localhost:9999/bad-manifest/mf-manifest.json', | ||
| }, | ||
| ], | ||
| plugins: [fetchPlugin(), errorHandlerPlugin()], | ||
| }); | ||
|
|
||
| await expect(FM.loadRemote('@test/bad-remote/someExpose')).rejects.toThrow( | ||
| /Missing required fields/, | ||
| ); | ||
|
|
||
| const lifecycles = errorLoadRemoteSpy.mock.calls.map( | ||
| (c: any[]) => c[0].lifecycle, | ||
| ); | ||
| expect(lifecycles).toContain('afterResolve'); | ||
| }); | ||
|
|
||
| it('calls errorLoadRemote when manifest fetch fails (network error)', async () => { | ||
| const errorLoadRemoteSpy = vi.fn(); | ||
|
|
||
| const fetchPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-fetch-plugin', | ||
| fetch(url) { | ||
| if (url.includes('unreachable')) { | ||
| return Promise.reject(new TypeError('Failed to fetch')); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const errorHandlerPlugin: () => ModuleFederationRuntimePlugin = () => ({ | ||
| name: 'test-error-handler', | ||
| errorLoadRemote(args) { | ||
| errorLoadRemoteSpy(args); | ||
| return undefined; | ||
| }, | ||
| }); | ||
|
|
||
| const FM = new ModuleFederation({ | ||
| name: '@test/host-network', | ||
| remotes: [ | ||
| { | ||
| name: '@test/unreachable-remote', | ||
| entry: 'http://localhost:9999/unreachable/mf-manifest.json', | ||
| }, | ||
| ], | ||
| plugins: [fetchPlugin(), errorHandlerPlugin()], | ||
| }); | ||
|
|
||
| await expect( | ||
| FM.loadRemote('@test/unreachable-remote/someExpose'), | ||
| ).rejects.toThrow(); | ||
|
|
||
| expect(errorLoadRemoteSpy).toHaveBeenCalled(); | ||
| const callArgs = errorLoadRemoteSpy.mock.calls[0][0]; | ||
| expect(callArgs.lifecycle).toBe('afterResolve'); | ||
| expect(callArgs.error).toBeInstanceOf(TypeError); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
getManifestJson, moving the required-fieldassertentirely inside the fetchtrymeans values returned fromerrorLoadRemoteare now accepted without shape checks. If a plugin returns a non-manifest payload (for example anargsobject), it gets treated asmanifestJsonand then fails later ingenerateSnapshotFromManifest, outside this recovery block, which bypasses the intended afterResolve handling and produces a harder-to-recover error path. Re-validatemanifestJsonafter thecatchbefore caching/using it.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2c5cecf. Added a post-catch
assertthat re-validatesmanifestJsonafter theerrorLoadRemotefailover, ensuring payloads returned by plugins are checked for required fields (metaData,exposes,shared) before being cached or passed togenerateSnapshotFromManifest. Also added a test covering this case.