Switching git branches while in watch mode causes 'can't find stylesheet to import'
Switching branches while sass --watch is running causes a Error: Can't find stylesheet to import. error when the switch causes one of the dependent files to be removed even if at the same time other files are changed so that the removed file is no longer needed after the branch switch.
Reproduction
Here's a reproduction repository: https://github.com/angelikatyborska/sass-watch-bug
To reproduce the bug, follow those steps:
- Checkout the repository,
mainbranch - Run
npm install - Confirm that sass build works on
mainby runningnpm run build:scss - Checkout the
applebranch. It contains an extra file that's also@use'd from the input scss filein.scss. - Confirm that sass build works on
appleby runningnpm run build:scss - Start watch mode by running
npm run build:scss:watch - Checkout
mainbranch while sass is running in watch mode - Observe unexpected build errors:
Error: Can't find stylesheet to import.
Video demo
https://github.com/sass/dart-sass/assets/7852553/b869ef7f-0332-4989-81f1-45af58b663ce
Dependency versions
- dart-sass: 1.77.6
- operating system: macOS Sonoma 14.5
- git: 2.39.3 (Apple Git-146)
Please let me know if the reproduction steps and repo are clear and what other details you need to debug this 🙂
I just noticed that the error does not happen when I close my IDE (Webstorm) and run sass from iterm2 instead of my IDE's built in terminal. Is it possible that my IDE is interfering with the watch mode?
My teammates who use a different IDE (VSCode) experience the same issue.
Unfortunately, we're fairly constrained here by the information provided by the operating system's filesystem watching API and the way that information is interpreted by the watcher library we use (in this case chokidar). Having implemented a watcher library in the past, I can say that the MacOS watcher API is particularly finicky, often reporting information that's incomplete, contradictory, or out-of-order.
The best workaround I can offer is passing --poll, which will skip the OS-level filesystem watcher entirely in favor of manually checking for new files every second. It won't be as efficient, but it'll be more likely to be robust in the face of whatever filesystem shenanigans Git is pulling.
Having a similar issue in a WSL/ubuntu/docker dev environment. When switching branches it compiles the CSS for the branch I'm switching from not the branch I'm switching into. A bit annoying but for now I just have to stop the watcher before switching branches or revert the changes..
Seem like chokidar has a major update coming soon, maybe it will fix this?