vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.73k stars 6.21k forks source link

fix(optimizer): trigger onCrawlEnd after manual included deps are registered #18733

Closed sapphi-red closed 8 hours ago

sapphi-red commented 12 hours ago

Description

When the optimizer runs before manual included deps are registered, Vite indefinitely stalled the request to optimized files.

I guess this rarely happens. It happens when runOptimizer in this onCrawlEnd is called while the promise of addManuallyIncludedOptimizeDeps(environment, manuallyIncludedDeps) is pending. https://github.com/vitejs/vite/blob/a384d8fd39162190675abcfea31ba657383a3d03/packages/vite/src/node/optimizer/optimizer.ts#L165 https://github.com/vitejs/vite/blob/a384d8fd39162190675abcfea31ba657383a3d03/packages/vite/src/node/optimizer/optimizer.ts#L174

Changing the code like below would make it constantly reproduce the problem with react template of create-vite.

-      environment.waitForRequestsIdle().then(onCrawlEnd)
+      // environment.waitForRequestsIdle().then(onCrawlEnd)
      waitingForCrawlEnd = true

      // Enter processing state until crawl of static imports ends
      currentlyProcessing = true

      // Initialize discovered deps with manually added optimizeDeps.include info

      const manuallyIncludedDeps: Record<string, string> = {}
      await addManuallyIncludedOptimizeDeps(environment, manuallyIncludedDeps)

+      startNextDiscoveredBatch()
+      await runOptimizer()

      const manuallyIncludedDepsInfo = toDiscoveredDependencies(
        environment,
        manuallyIncludedDeps,
        sessionTimestamp,
      )