heybourn / headwind

An opinionated Tailwind CSS class sorter built for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=heybourn.headwind
MIT License
1.38k stars 46 forks source link

Run on save doesn't wait for results causing race condition with watchers #119

Closed Xeevis closed 2 years ago

Xeevis commented 3 years ago

Describe the bug When working with Maizzle I've noticed more tailwind markup meant higher chance of watch crashing. It turns out it's caused by Headwind's Run on Save.

To Reproduce Steps to reproduce the behavior:

  1. npm install -g @maizzle/cli
  2. maizzle new (hit enter on all prompts)
  3. cd maizzle && code ./ && maizzle serve
  4. Wait until BrowserSync is launched in console & make sure Headwind extension is enabled in VS Code
  5. Manually save ./src/templates/promotional.html in VS Code
  6. Will throw > RangeError: received empty string

Expected behavior No watcher error, VS code should save file only once all edits by extensions are in place.

Screenshots image

Desktop (please complete the following information):

Additional context Disabling Headwind or setting "headwind.runOnSave": false fixes the issue. (reload required)

Issue also reported on Maizzle repo https://github.com/maizzle/framework/issues/428 where it got closed as Headwind issue.

Xeevis commented 3 years ago

I'm no expert, but shouldn't executeCommand return Promise/Thenable which should be waited for? https://github.com/heybourn/headwind/blob/6ccdeae090eb636288f37f08b6702fd7d76c06e6/src/extension.ts#L120-L124 Something along these lines or something

 context.subscriptions.push( 
    workspace.onWillSaveTextDocument((_e) => { 
        _e.waitUntil(Promise.resolve(commands.executeCommand('headwind.sortTailwindClasses'))); 
    }) 
 );