hashivim/vim-terraform

Format on save not running when file is first created

ldelelis opened this issue · 2 comments

If I start writing a non existent file with vim, and then close with :wq, auto formatting is not being run on save. However, if I then open that file again and save it (either with or without changes), auto-format runs correctly

Steps to reproduce


  1. Create a new file example.tf
  2. Write a valid terraform resource. Example:
variable "some_var" {
  type = string
  description = "this should be a real description"
}
  1. Save and exit with :wq
  2. cat the file. Formatting has not been run for it.
  3. Re-open it with vim, and save with :w. The file automatically formats itself

This is because we check filereadable() before formatting, which does not hold for a non-existent file.

I think that it should be safe not to do this. It maybe made sense when we copied files around and ran terraform fmt on the copy, but now that formatting works by sending the buffer to the formatter's stdin, I don't immediately see what goes wrong if we just remove that check.

Well, I'll make the merge request - but probably leave it to sit around for a day or two in case I think better of it or someone else wants to point out what I've missed.

Fair enough. I'll run some manual tests on it tomorrow during my daily terraform struggle. Thanks for the quick reply!