Terraform fmt doesn't work correctly
Closed this issue · 8 comments
My .vimrc:
call plug#begin('~/.vim/plugged')
Plug 'hashivim/vim-terraform'
call plug#end()
" terraform stuff
let g:terraform_align=1
let g:terraform_fmt_on_save=1
Then
vim vpc/main.tf
And I get:
NORMAL vpc/main.tf terraform utf-8[unix] 11% ☰ 1/9 ㏑ : 1
^[[31m
^[[1m^[[31mError: ^[[0m^[[0m^[[1mOnly .tf and .tfvars files can be processed with terraform fmt^[[0m
^[[0m^[[0m^[[0m
"vpc/main.tf" 9L, 141C written
This is driving me insane.
terraform fmt works on the file externally.
terraform 0.12.5
Well obviously this Works For Me, so you're likely going to have to debug what's different about your system yourself, I'm afraid.
The code works by copying your buffer into a temporary file, formatting that, and - if all is well - copying the formatted file back. See here.
So I suppose I'd start by logging the name of that temporary file (stick in echom l:tmpfile
) and see what it says.
Good luck!
Just realized that you wrote Terraform fmt
, not TerraformFmt
.
Well, you probably don't want to do that - you probably want to do TerraformFmt
.
I suppose Terraform fmt
will run terraform fmt
from your current working directory, which I guess is not what you're expecting?
This seems like a Terraform 0.12.X compatibility issue.
I similarly have
output "my_cidrs" {
value = data.terraform_remote_state.vpc.outputs.my_cidr_blocks
}
When I run :TerraformFmt
in vim, I get:
^[[31mError running fmt: In /var/folders/0q/rjhkdhlj7lscb0f04tnb41_h2z2629/T/v09zeB6/0.tf: At 12:9: Unknown token: 12:9 IDENT data.terraform_remote_state.vpc.outputs.my_cidr_blocks^[[0m^[[0m
yet if I wrap the data
like so "${data.terraform_remote_state.vpc.outputs.my_cidr_blocks}"
It works as expected, seems like maybe the plugin isn't supporting first-class expressions?
@the-real-cphillips the plugin just runs "terraform fmt", and then reports the errors from that.
@the-real-cphillips the plugin just runs "terraform fmt", and then reports the errors from that.
welp, that explains everything, I was just digging through the code, and discovered that.
I'm currently running both terraform versions, and my 0.12.5 binary is terraform_12
:)
wonder if there could be support for specifying terraform
binary location
Personally I'd favour heading in the other direction and losing the Terraform
command altogether. It really doesn't add much over just going !terraform
- there's some auto-completion help, but that's it, and even that only works properly if your terraform installation is at the same version as this plugin.
(Not saying this going to happen, just that if I were king of the world...)
I was having the same issue. Updating the vim-terraform to the latest resolved it for me.
FYI, @the-real-cphillips I'm using https://github.com/Yleisradio/homebrew-terraforms to use multiple terraform versions. (https://github.com/tfutils/tfenv is another option)
So: good discussion about what this plugin ought to do, meanwhile seems like so far as it goes this is all Working As Designed.
Please open new issues if that's not right.