-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw if React and React DOM versions don't match
Throw an error during module initialization if the version of the "react-dom" package does not match the version of "react". We used to be more relaxed about this, because the "react" package changed so infrequently. However, we now have many more features that rely on an internal protocol between the two packages, including Hooks, Float, and the compiler runtime. So it's important that both packages are versioned in lockstep. Before this change, a version mismatch would often result in a cryptic internal error with no indication of the root cause. Instead, we will now compare the versions during module initialization and immediately throw an error to catch mistakes as early as possible and provide a clear error message.
- Loading branch information
Showing
13 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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
24 changes: 24 additions & 0 deletions
24
packages/react-dom/src/shared/ensureCorrectIsomorphicReactVersion.js
This file contains 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,24 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import reactDOMPackageVersion from 'shared/ReactVersion'; | ||
import * as IsomorphicReactPackage from 'react'; | ||
|
||
export function ensureCorrectIsomorphicReactVersion() { | ||
const isomorphicReactPackageVersion = IsomorphicReactPackage.version; | ||
if (isomorphicReactPackageVersion !== reactDOMPackageVersion) { | ||
// TODO: Do we have a canoncical documentation page for this? | ||
throw new Error( | ||
'Incompatible React versions: The "react" and "react-dom" packages must ' + | ||
'have the exact same version. Instead got:\n' + | ||
` - react: ${isomorphicReactPackageVersion}\n` + | ||
` - react-dom: ${reactDOMPackageVersion}`, | ||
); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
packages/react/src/__tests__/ReactMismatchedVersions-test.js
This file contains 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,127 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('ReactMismatchedVersions-test', () => { | ||
// Polyfills for test environment | ||
global.ReadableStream = | ||
require('web-streams-polyfill/ponyfill/es6').ReadableStream; | ||
global.TextEncoder = require('util').TextEncoder; | ||
|
||
jest.mock('react', () => { | ||
const actualReact = jest.requireActual('react'); | ||
return { | ||
...actualReact, | ||
version: '18.0.0-whoa-this-aint-the-right-react', | ||
__actualVersion: actualReact.version, | ||
}; | ||
}); | ||
const React = require('react'); | ||
const actualReactVersion = React.__actualVersion; | ||
|
||
test('importing "react-dom/client" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/client')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
// When running in source mode, we lazily require the implementation to | ||
// simulate the static config dependency injection we do at build time. So it | ||
// only errors once you call something and trigger the require. Running the | ||
// test in build mode is sufficient. | ||
// @gate !source | ||
test('importing "react-dom/server" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/server')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
// @gate !source | ||
test('importing "react-dom/server.node" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/server.node')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
// @gate !source | ||
test('importing "react-dom/server.browser" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/server.browser')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
// @gate !source | ||
test('importing "react-dom/server.bun" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/server.bun')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
// @gate !source | ||
test('importing "react-dom/server.edge" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/server.edge')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
test('importing "react-dom/static" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/static')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
test('importing "react-dom/static.node" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/static.node')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
test('importing "react-dom/static.browser" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/static.browser')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
|
||
test('importing "react-dom/static.edge" throws if version does not match React version', async () => { | ||
expect(() => require('react-dom/static.edge')).toThrow( | ||
'Incompatible React versions: The "react" and "react-dom" packages ' + | ||
'must have the exact same version. Instead got:\n' + | ||
' - react: 18.0.0-whoa-this-aint-the-right-react\n' + | ||
` - react-dom: ${actualReactVersion}`, | ||
); | ||
}); | ||
}); |
This file contains 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 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