zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
48.9k stars 2.94k forks source link

Run command on save #18523

Closed trymeouteh closed 2 weeks ago

trymeouteh commented 3 weeks ago

Check for existing issues

Describe the feature

The ability to run custom commands when you save specific file extensions in Zed. Can be used to act as a "watch" feature when working with code to run bundlers when the user saves their code.

If applicable, add mockups / screenshots to help present your vision of the feature

  1. Wlll look for run-on-save.json file in workplace directory when a file is saved
  2. Will see if run-on-save.json file contains any commands to run for the file extension being saved
  3. If true, it will see if these commands have been saved in the Zed editor to be executed for the file extension when saved. If true, it will execute the commands. If false, it will prompt the user asking them if it wants Zed to execute these commands in this workplace when saving this file.

It is important to ask the user if it should run the commands first for security reasons. But once the user approves, it will always run the commands. If the run-on-save.json file is modified, it will prompt the user and ask them again to ensure the file was not updated with malicious commands.

notpeter commented 2 weeks ago

We are unlikely to implement a arbitrary run-command-on-save feature. This is for a number of reasons, not least of which is it's unclear what should happen if you save a file twice in quick succession (should the cmd be spawed twice? Should the first one be killed? etc). This is why most bundler implement their own watchers.

But the existence of inotify (Linux) and fswatch (MacOS) should make this relatively trivial to implement your own version with the same caveats.

For example on MacOS (brew install fswatch):

fswatch -0 ~/code/zed | xargs -0 -I {} sh -c 'echo "File changed: {}"; echo {} >> /tmp/my.log'

Or on Linux:

inotifywait -m -e modify,create,delete --format '%w%f' ~/source/zed | xargs -I {} sh -c 'echo "File changed: {}"; echo "{}" >> /tmp/a.log'