fix(deps): guard against undefined deps in commitProcessing#21816
fix(deps): guard against undefined deps in commitProcessing#21816guest363 wants to merge 1 commit intovitejs:mainfrom
Conversation
When the dependency optimizer re-runs during dev startup (e.g. due to virtual module content changes), commitProcessing may encounter deps in newData.optimized that don't exist in metadata.optimized/discovered, or deps in metadata.optimized that were removed from newData.optimized. This adds null guards to prevent TypeError when accessing browserHash and fileHash on undefined entries. Closes #XXXX
|
So I can understand better, does this fixes a bug you're encountering in a project? Or are these changes only hypothetical? |
|
Yes, this fixes a real bug I'm encountering in my project. I hit it while migrating to Astro 6, which introduced a new Font API using Root cause chain (my assumption):
While the upstream root cause is on the Astro side, the Vite optimizer should still be resilient here - a dep appearing in new optimization results without a corresponding entry in previous metadata is a valid edge case that shouldn't crash the server. These guards make commitProcessing defensive against any scenario where the dep sets don't fully overlap. |
|
I've verified this fix in my project using |
|
Vite 8 does not repeat the error |
I think what you said makes sense, but I'm wary that adding this is guarding against a case that shouldn't happen in the first place, and could cause other bugs in the future. For example:
I believe the flow here is that the So perhaps the root cause is in a different place, and it seems a bit like a race condition somewhere with your description. I think it's worth tracking this down if possible, otherwise if you can share your project/repro/setup that would be valuable too. If the other maintainers are fine with this PR, I'm also good with that, but I'd lean towards understanding the edgecase better first. |
|
@bluwy Thank you for the thorough explanation - you're absolutely right that the invariant should hold by design, and guarding against it could mask real issues down the road rather than solving them. Since I've confirmed that Vite 8 fully resolves this issue (verified by removing my v7 patch and upgrading to v8 in my project), I don't think it's worth spending more time tracking down the exact race condition in v7. Closing this PR. Thanks for your time and patience reviewing this! 🙏 |
|
@bluwy I need to reopen this - apologies for closing prematurely. I didn't properly remove my yarn patch when testing with Vite 8, so the results were misleading. After fully removing the patch and upgrading to Vite 8, the bug reproduces consistently. Following your suggestion to understand the edge case better, I added debug logging to So my initial assumption about fonts was wrong - the actual culprit is astro/actions/runtime/entrypoints/route.js. It appears in newData.optimized but was never registered in metadata.optimized or metadata.discovered. It seems like Astro's Actions plugin injects this dependency into the optimizer in a way that bypasses Vite's normal discovery flow, likely during a config change that triggers re-optimization. I haven't been able to reproduce this in a clean minimal repo yet - it may require a specific combination of integrations (React + Svelte + Actions in my case). I'll keep working on isolating a minimal repro. |
When the dependency optimizer re-runs during dev startup (e.g. due to virtual module content changes), commitProcessing may encounter deps in newData.optimized that don't exist in metadata.optimized/discovered, or deps in metadata.optimized that were removed from newData.optimized.
This adds null guards to prevent TypeError when accessing browserHash and fileHash on undefined entries.