-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Circular dependencies in SSR not detected and causes indefinite hang #2491
Description
- Read the docs.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- This is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
Describe the bug
Let's say we have three files: A, B, and C, where A is the entry point. B and C cyclically depend on each other. A depends on both B and C. In this situation, Vite will not detect the cyclic dependency and simply hang indefinitely.
Reproduction
Create the following 3 files inside of the src/pages directory of the ssr-react playground. Attempt to load the /A page, and notice that it hangs forever and the logs are empty.
// A.jsx
import { addFour } from './B'
import { addTwo } from './C'
export default function Main(){
return addFour(addTwo(1))
}
// B.js
import { addTwo } from './C'
export function addOne(x){
return x + 1
}
export function addFour(x){
return addTwo(addTwo(x))
}
// C.js
import { addOne } from './A'
export function addTwo(x){
return addOne(addOne(x))
}Explanation
This bug happens because ssrLoadModule first gets called with A, the entry point. Within instantiateModule, it then tries to load the dependencies B and C in parallel, and caches their promises in the pendingModules map.
When instantiateModule finally runs on B, it blocks until ssrLoadModule completes for its sole dependency: C. When ssrLoadModule is called, the urlStack only has [A, B] so it bypasses the circular dependency test. Since C is already in the pendingModules map, it returns the existing promise waiting for C to load.
Essentially this results in a state where A depends on B and C, while B and C both depend on each other. At this point ssrLoadModule will never complete.
System Info
viteversion: 2.0.5- Operating System: macOS 11.2
- Node version: 15.11
- Package manager (npm/yarn/pnpm) and version: Yarn 1.22.10