zed icon indicating copy to clipboard operation
zed copied to clipboard

Terraform support

Open HarryET opened this issue 2 years ago • 22 comments

Check for existing issues

  • [X] Completed

Is your feature request related to a problem?

Missing support for Terraform/HCL

Describe the solution you'd like

Add support for terraform-ls so that we can have syntax highlighting and auto-complete for terraform configurations

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

N/a

HarryET avatar Nov 29 '22 17:11 HarryET

Tree sitter: https://github.com/MichaHoffmann/tree-sitter-hcl

mikayla-maki avatar Jan 17 '23 17:01 mikayla-maki

No pressure, but having this would allow me to jump into some hardcore mob programming with my current team. 😬

fdionisi avatar Feb 16 '23 18:02 fdionisi

Hey @fdionisi, no promises, but we're currently expecting to add HCL in the rather-near future.

JosephTLyons avatar Feb 16 '23 18:02 JosephTLyons

Any news about this? :) I'd love to use Zed for all my Terraform repositories! 😊

maxdec avatar Jun 29 '23 16:06 maxdec

Any news, @JosephTLyons?

nikolay avatar Sep 06 '23 20:09 nikolay

@JosephTLyons Bumping this up as well, this would be a welcomed feature for all of us DevOps/SRE folks 👍

bxker avatar Dec 06 '23 15:12 bxker

Would love to see this as well!

cjfinnell avatar Dec 14 '23 15:12 cjfinnell

Terraform is nearly the only reason I am jumping back to vscode occasionally

ivan-kiselev avatar Dec 20 '23 15:12 ivan-kiselev

Not wanting this to just be a +1 comment, so can I add HCL covers more than just Terraform too. Packer & Nomad also use HCL in their configuration. GitHub Copilot is mostly good enough in Zed for completion purposes writing HCL, but the lack of highlighting/indentation support is slightly annoying.

Edit: and GitHub's State of Open Source 2023 edition shows HCL sits just below Go in popularity on their platform. 🙃

caius avatar Dec 22 '23 14:12 caius

I've worked around this temporarily by selecting Scheme as my syntax when editing Terraform / HCL files, and as I don't edit Scheme files normally then overridden my settings to get formatting-on-save working as I'd expect with this in my Zed settings:

{
  "language_overrides": {
    "Scheme": {
      "formatter": {
        "external": {
          "command": "terraform",
          "arguments": ["fmt", "-"]
        }
      }
    }
  }
}

caius avatar Jan 03 '24 18:01 caius

Still waiting ...

francisceril avatar Jan 20 '24 07:01 francisceril

Still waiting ...

AMaliutsin avatar Jan 25 '24 16:01 AMaliutsin

@AMaliutsin @francisceril not sure if you’ve seen but the repo is open source now, makes it easier to open a Pull Request to push this forward rather than waiting?

caius avatar Jan 25 '24 20:01 caius

If no one else is working on it, I'll be happy to try implementing it next week.

dbanck avatar Jan 26 '24 09:01 dbanck

I love how fast and clean zed is, the only reason i go back to vscode is terraform. Does anyone know a way how to make tf files open as Scheme by default ?

wadhah101 avatar Jan 26 '24 10:01 wadhah101

If no one else is working on it, I'll be happy to try implementing it next week.

I started spiking it out last night, got some highlighting working but haven't gotten the LSP wired up correctly yet. Errors when invoked with a oneshot cancelled error. Couldn't find any scheme files in the tree-sitter-hcl repo so copied them from https://github.com/nvim-treesitter/nvim-treesitter/tree/master/queries/terraform for now.

https://github.com/zed-industries/zed/compare/main...caius:zed:cd/lang-terraform

caius avatar Jan 26 '24 12:01 caius

Feel free to coordinate and work on it if you like, we'll pull terraform in directly if a PR emerges :)

JosephTLyons avatar Jan 27 '24 02:01 JosephTLyons

Any news about HCL syntaxe support ? Zed is really fantastic !

Abdelwaheb-Hnaien avatar Jan 29 '24 09:01 Abdelwaheb-Hnaien

-- Update --

Looks like @caius is working on this one! https://github.com/zed-industries/zed/pull/6882

Thanks @caius!

bxker avatar Jan 29 '24 21:01 bxker

Syntax is due to be deployed in 122.

Still missing Language Server support: https://github.com/zed-industries/zed/blob/main/crates/zed/src/languages.rs#L325

romans-ovo avatar Feb 06 '24 16:02 romans-ovo

Syntax highlighting landed in preview today: v0.121.1-pre.

JosephTLyons avatar Feb 07 '24 22:02 JosephTLyons

This worked for me after Syntax highlighting for Terraform was added:

settings.json

  "languages": {
    "Terraform": {
      "formatter": {
        "external": {
          "command": "terraform",
          "arguments": [
            "fmt",
            "-"
          ]
        }
      }
    }
  }

0xDones avatar Feb 26 '24 22:02 0xDones

any idea when will the formatting works with .hcl files? Have just tried, it works with .tf file as mentioned above.

Day 0 of trying to use Zed.

  1. Installed terraform pluging image
  2. My settings.json looks like this image

As mentioned by here https://github.com/zed-industries/zed/blob/main/docs/src/languages/terraform.md

chinyongcy avatar May 20 '24 03:05 chinyongcy

@chinyongcy What kind of HCL do you write? Packer files for example?

Unlike some other formats like JSON and YAML, HCL was designed as a toolkit for building languages rather than as a language in itself, so there's a lot more application-level interpretation involved than you might be used to with other grammars. That's why formatting is usually done by specific applications rather than by a product-agnostic formatter.

The Terraform plugin/language server is only equipped to format Terraform files and won't format generic HCL. Many tools provide application-specific formatters, such as packer fmt or vault policy fmt. Depending on the type of HCL you're writing, I hope you can find something similar.

dbanck avatar May 21 '24 12:05 dbanck

What kind of HCL do you write?

Terragrunt and its use of HCL is quite common when working with Terraform.

avtar avatar May 21 '24 23:05 avtar

@avtar terragrunt hclfmt - https://terragrunt.gruntwork.io/docs/getting-started/configuration/#formatting-hcl-files

stv-io avatar May 22 '24 06:05 stv-io

@chinyongcy What kind of HCL do you write? Packer files for example?

Unlike some other formats like JSON and YAML, HCL was designed as a toolkit for building languages rather than as a language in itself, so there's a lot more application-level interpretation involved than you might be used to with other grammars. That's why formatting is usually done by specific applications rather than by a product-agnostic formatter.

The Terraform plugin/language server is only equipped to format Terraform files and won't format generic HCL. Many tools provide application-specific formatters, such as packer fmt or vault policy fmt. Depending on the type of HCL you're writing, I hope you can find something similar.

terragrunt hcl, new to zed, i tried doing this based on some of the reply above for terraform, and try doing it for terragrunt. image but every time when I save my hcl, it clears the file.

Current solution what I have done

  1. Clone this https://github.com/hashicorp/hcl/tree/main/cmd/hclfmt
  2. Go into cmd/hclfmt and then run go build
  3. Shift the hclfmt to /usr/local/bin/ and then chmod +x it

and then this is what I have used for settings.json image

one caveat it is only able to do formatting after I save the file without formatting. However, once i make some changes and edit, and i click save straight away, whatever changes that was there will be lost.

pretty sure that is still something wrong in my setting.json. It is almost perfect now.

chinyongcy avatar May 22 '24 07:05 chinyongcy

The tricky part to make the formatter work is to make sure the terragrunt hclfmt command outputs the file to stdout. Following the recommendation here adjusted inline formatter as following and should admit that it works as a charm:

"HCL": {
      // Workarond to be able to format_on_save terragrunt files
      // https://github.com/gruntwork-io/terragrunt/issues/1037#issuecomment-2143442454
      "format_on_save": {
        "external": {
          "command": "sh",
          "arguments": [
            "-c",
            "TMPFILE=$(mktemp) && cat - > $TMPFILE && terragrunt hclfmt --terragrunt-hclfmt-file $TMPFILE && cat $TMPFILE && rm $TMPFILE"
          ]
        }
      }

begemotik avatar Jun 16 '24 12:06 begemotik

How can I rename variables affecting all occurrences throughout the code?

ChzenChzen avatar Jul 14 '24 13:07 ChzenChzen