-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ssr): tolerate circular imports (#3950)
Co-authored-by: Greg Fairbanks <gregfa@zillowgroup.com> Co-authored-by: Ray Thurne Void <ray.thurne.void@gmail.com>
- Loading branch information
1 parent
d53dc92
commit 69f91a1
Showing
18 changed files
with
263 additions
and
91 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { multiply } from './multiply' | ||
|
||
export function add(a, b) { | ||
return a + b | ||
} | ||
|
||
export function addAndMultiply(a, b, c) { | ||
return multiply(add(a, b), c) | ||
} |
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 @@ | ||
This test aim to find out wherever the modules with circular dependencies are correctly initialized |
2 changes: 2 additions & 0 deletions
2
packages/playground/ssr-react/src/circular-dep-init/circular-dep-init.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,2 @@ | ||
export * from './module-a' | ||
export { getValueAB } from './module-b' |
1 change: 1 addition & 0 deletions
1
packages/playground/ssr-react/src/circular-dep-init/module-a.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 @@ | ||
export const valueA = 'circ-dep-init-a' |
8 changes: 8 additions & 0 deletions
8
packages/playground/ssr-react/src/circular-dep-init/module-b.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,8 @@ | ||
import { valueA } from './circular-dep-init' | ||
|
||
export const valueB = 'circ-dep-init-b' | ||
export const valueAB = valueA.concat(` ${valueB}`) | ||
|
||
export function getValueAB() { | ||
return valueAB | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/playground/ssr-react/src/forked-deadlock/README.md
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,45 @@ | ||
This test aim to check for a particular type of circular dependency that causes tricky deadlocks, **deadlocks with forked imports stack** | ||
|
||
``` | ||
A -> B means: B is imported by A and B has A in its stack | ||
A ... B means: A is waiting for B to ssrLoadModule() | ||
H -> X ... Y | ||
H -> X -> Y ... B | ||
H -> A ... B | ||
H -> A -> B ... X | ||
``` | ||
|
||
### Forked deadlock description: | ||
``` | ||
[X] is waiting for [Y] to resolve | ||
↑ ↳ is waiting for [A] to resolve | ||
│ ↳ is waiting for [B] to resolve | ||
│ ↳ is waiting for [X] to resolve | ||
└────────────────────────────────────────────────────────────────────────┘ | ||
``` | ||
|
||
This may seems a traditional deadlock, but the thing that makes this special is the import stack of each module: | ||
``` | ||
[X] stack: | ||
[H] | ||
``` | ||
``` | ||
[Y] stack: | ||
[X] | ||
[H] | ||
``` | ||
``` | ||
[A] stack: | ||
[H] | ||
``` | ||
``` | ||
[B] stack: | ||
[A] | ||
[H] | ||
``` | ||
Even if `[X]` is imported by `[B]`, `[B]` is not in `[X]`'s stack because it's imported by `[H]` in first place then it's stack is only composed by `[H]`. `[H]` **forks** the imports **stack** and this make hard to be found. | ||
|
||
### Fix description | ||
Vite, when imports `[X]`, should check whether `[X]` is already pending and if it is, it must check that, when it was imported in first place, the stack of `[X]` doesn't have any module in common with the current module; in this case `[B]` has the module `[H]` is common with `[X]` and i can assume that a deadlock is going to happen. | ||
|
10 changes: 10 additions & 0 deletions
10
packages/playground/ssr-react/src/forked-deadlock/common-module.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,10 @@ | ||
import { stuckModuleExport } from './stuck-module' | ||
import { deadlockfuseModuleExport } from './deadlock-fuse-module' | ||
|
||
/** | ||
* module H | ||
*/ | ||
export function commonModuleExport() { | ||
stuckModuleExport() | ||
deadlockfuseModuleExport() | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/playground/ssr-react/src/forked-deadlock/deadlock-fuse-module.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,8 @@ | ||
import { fuseStuckBridgeModuleExport } from './fuse-stuck-bridge-module' | ||
|
||
/** | ||
* module A | ||
*/ | ||
export function deadlockfuseModuleExport() { | ||
fuseStuckBridgeModuleExport() | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/playground/ssr-react/src/forked-deadlock/fuse-stuck-bridge-module.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,8 @@ | ||
import { stuckModuleExport } from './stuck-module' | ||
|
||
/** | ||
* module C | ||
*/ | ||
export function fuseStuckBridgeModuleExport() { | ||
stuckModuleExport() | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/playground/ssr-react/src/forked-deadlock/middle-module.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,8 @@ | ||
import { deadlockfuseModuleExport } from './deadlock-fuse-module' | ||
|
||
/** | ||
* module Y | ||
*/ | ||
export function middleModuleExport() { | ||
void deadlockfuseModuleExport | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/playground/ssr-react/src/forked-deadlock/stuck-module.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,8 @@ | ||
import { middleModuleExport } from './middle-module' | ||
|
||
/** | ||
* module X | ||
*/ | ||
export function stuckModuleExport() { | ||
middleModuleExport() | ||
} |
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,9 @@ | ||
import { add } from './add' | ||
|
||
export function multiply(a, b) { | ||
return a * b | ||
} | ||
|
||
export function multiplyAndAdd(a, b, c) { | ||
return add(multiply(a, b), c) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
import { addAndMultiply } from '../add' | ||
import { multiplyAndAdd } from '../multiply' | ||
|
||
export default function About() { | ||
return <h1>About</h1> | ||
return ( | ||
<> | ||
<h1>About</h1> | ||
<div>{addAndMultiply(1, 2, 3)}</div> | ||
<div>{multiplyAndAdd(1, 2, 3)}</div> | ||
</> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,17 @@ | ||
import { addAndMultiply } from '../add' | ||
import { multiplyAndAdd } from '../multiply' | ||
import { commonModuleExport } from '../forked-deadlock/common-module' | ||
import { getValueAB } from '../circular-dep-init/circular-dep-init' | ||
|
||
export default function Home() { | ||
return <h1>Home</h1> | ||
commonModuleExport() | ||
|
||
return ( | ||
<> | ||
<h1>Home</h1> | ||
<div>{addAndMultiply(1, 2, 3)}</div> | ||
<div>{multiplyAndAdd(1, 2, 3)}</div> | ||
<div className="circ-dep-init">{getValueAB()}</div> | ||
</> | ||
) | ||
} |
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
Oops, something went wrong.