Skip to content

Handle deleted files gracefully during Vite HMR compilation#19776

Open
mvanhorn wants to merge 1 commit intotailwindlabs:mainfrom
mvanhorn:osc/17532-fix-svg-deletion-hmr-crash
Open

Handle deleted files gracefully during Vite HMR compilation#19776
mvanhorn wants to merge 1 commit intotailwindlabs:mainfrom
mvanhorn:osc/17532-fix-svg-deletion-hmr-crash

Conversation

@mvanhorn
Copy link

Summary

Fixes #17532

When a file that Tailwind is scanning (e.g., an SVG asset) gets deleted while the Vite dev server is running, the compile() call crashes with an ENOENT error because it tries to read the deleted file. This kills the HMR connection and requires a server restart.

The fix wraps the compile() call in Root.generate() with a try-catch that specifically handles ENOENT errors. When a dependency file has been deleted:

  • The compiler and scanner are invalidated
  • The method returns false, signaling the root should be re-evaluated
  • On the next cycle, the compiler re-initializes fresh without the deleted dependency

Non-ENOENT errors are re-thrown to preserve normal error handling.

Test plan

  • All 4620 existing tests pass
  • pnpm build && pnpm test passes
  • Manual testing: start a Vite dev server with Tailwind, add an SVG file referenced in templates, then delete it - the dev server should continue working instead of crashing

This contribution was developed with AI assistance (Claude Code).

When a file that Tailwind is watching (e.g. an SVG asset) gets deleted
during HMR, the compile step fails with ENOENT because it tries to read
a file that no longer exists. This wraps the compile call with a
try-catch that specifically handles ENOENT errors by invalidating the
compiler/scanner state and returning false, allowing the next rebuild
cycle to proceed without the deleted file.

Fixes tailwindlabs#17532

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6eaaf458-71f3-4150-9e3d-9967b90040c2

📥 Commits

Reviewing files that changed from the base of the PR and between c586bd6 and 916195b.

📒 Files selected for processing (1)
  • packages/@tailwindcss-vite/src/index.ts

Walkthrough

The Tailwind CSS Vite plugin's root setup process now includes error handling for the compiler invocation. When an ENOENT error occurs—typically from missing dependency files during hot module replacement—the compiler and scanner are invalidated and the setup returns false to trigger a rebuild. This prevents error propagation in scenarios where files may be deleted during development. The dependency watching and build flow remain unchanged, with the error handling path providing an alternative resolution route without affecting normal operation.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main change: handling deleted files gracefully during Vite HMR compilation.
Description check ✅ Passed The description is well-detailed and directly related to the changeset, explaining the problem, solution approach, and testing performed.
Linked Issues check ✅ Passed The code changes directly address the requirements of issue #17532 by implementing try-catch error handling for ENOENT errors during the compile() call, invalidating state, and allowing HMR recovery.
Out of Scope Changes check ✅ Passed All changes are scoped to the Tailwind Vite plugin's root setup error handling for deleted dependencies, directly addressing the linked issue without introducing unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting an SVG asset causes a Vite HMR crash while processing @import "tailwindcss"

1 participant