Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform support #5098

Closed
1 task done
HarryET opened this issue Nov 29, 2022 · 27 comments · Fixed by #7657
Closed
1 task done

Terraform support #5098

HarryET opened this issue Nov 29, 2022 · 27 comments · Fixed by #7657
Labels
enhancement [core label] language An umbrella label for all programming languages syntax behaviors potential extension Functionality that could be implemented as an extension (consider moving to community extensions) unsupported language

Comments

@HarryET
Copy link

HarryET commented Nov 29, 2022

Check for existing issues

  • 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 HarryET added enhancement [core label] triage Maintainer needs to classify the issue labels Nov 29, 2022
@JosephTLyons JosephTLyons added language An umbrella label for all programming languages syntax behaviors and removed triage Maintainer needs to classify the issue labels Nov 30, 2022
@mikayla-maki
Copy link
Contributor

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

@JosephTLyons JosephTLyons mentioned this issue Feb 1, 2023
79 tasks
@fdionisi
Copy link
Contributor

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

@JosephTLyons
Copy link
Contributor

JosephTLyons commented Feb 16, 2023

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

@JosephTLyons JosephTLyons changed the title Terraform Support Terraform support Apr 6, 2023
@hovsater hovsater added the potential extension Functionality that could be implemented as an extension (consider moving to community extensions) label Apr 30, 2023
@maxdec
Copy link

maxdec commented Jun 29, 2023

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

@nikolay
Copy link

nikolay commented Sep 6, 2023

Any news, @JosephTLyons?

@bxker
Copy link

bxker commented Dec 6, 2023

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

@cjfinnell
Copy link

Would love to see this as well!

@ivan-kiselev
Copy link

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

@caius
Copy link
Contributor

caius commented Dec 22, 2023

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
Copy link
Contributor

caius commented Jan 3, 2024

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", "-"]
        }
      }
    }
  }
}

@francisceril
Copy link

Still waiting ...

@JosephTLyons JosephTLyons transferred this issue from zed-industries/community Jan 24, 2024
@AMaliutsin
Copy link

Still waiting ...

@caius
Copy link
Contributor

caius commented Jan 25, 2024

@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?

@dbanck
Copy link
Contributor

dbanck commented Jan 26, 2024

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

@wadhah101
Copy link

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 ?

@caius
Copy link
Contributor

caius commented Jan 26, 2024

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.

main...caius:zed:cd/lang-terraform

@JosephTLyons
Copy link
Contributor

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

@Abdelwaheb-Hnaien
Copy link

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

@bxker
Copy link

bxker commented Jan 29, 2024

-- Update --

Looks like @caius is working on this one! #6882

Thanks @caius!

maxbrunsfeld pushed a commit that referenced this issue Feb 5, 2024
Terraform and HCL are almost the same language, but not quite so
proposing them as separate languages within Zed. (Terraform is an
extension of HCL, with a different formatter.)

This is just adding the language definition, parsing and highlighting
functionality, not any LSP or formatting beyond that for either
language.

I've taken a bunch of inspiration from Neovim for having the separate
languages, and also lifted some of their `scm` files (with attribution
comments in this codebase) as the tree-sitter repo doesn't contain them.
(Neovim's code is Apache-2.0 licensed, so should be fine here with
attribution from reading Zed's licenses files.) I've then amended to
make sure the capture groups are named for things Zed understands. I'd
love someone from Zed to confirm that's okay, or if I should clean-room
implement the `scm` files.

Highlighting in Terraform & HCL with a moderate amount of syntax in a
file (Terraform on left, HCL on right.)

<img width="1392" alt="Screenshot 2024-01-31 at 18 07 45"
src="https://github.com/zed-industries/zed/assets/696/1d3c9a08-588e-4b8f-ad92-98ce1e419659">

Release Notes:

- (|Improved) ...
([#5098](#5098)).
@romans-ovo
Copy link

romans-ovo commented Feb 6, 2024

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

@JosephTLyons
Copy link
Contributor

JosephTLyons commented Feb 7, 2024

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

maxdeviant pushed a commit that referenced this issue Feb 8, 2024
With #6882 basic syntax
highlighting support for Terraform has arrived in Zed. To fully support
all features of the language server (when it lands), it's necessary to
handle `*.tfvars` slightly differently.

TL;DR: [terraform-ls](https://github.com/hashicorp/terraform-ls) expects
`terraform` as language id for `*.tf` files and `terraform-vars` as
language id for `*.tfvars` files because the allowed configuration
inside the files is different. Duplicating the Terraform language
configuration was the only way I could see to achieve this.

---

In the
[LSP](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem),
text documents have a language identifier to identify a document on the
server side to avoid reinterpreting the file extension.

The Terraform language server currently uses two different language
identifiers:
* `terraform` - for `*.tf` files
* `terraform-vars` - for `*.tfvars` files

Both file types contain HCL and can be highlighted using the same
grammar and tree-sitter configuration files. The difference in the file
content is that `*.tfvars` files only allow top-level attributes and no
blocks. [_So you could argue that `*.tfvars` can use a stripped down
version of the grammar_]. To set the right context (which affects
completion, hover, validation...) for each file, we need to send a
different language id.

The only way I could see to achieve this with the current architecture
was to copy the Terraform language configuration with a different `name`
and different `path_suffixes`. Everything else is the same.

A Terraform LSP adapter implementation would then map the language
configurations to their specific language ids:

```rust
fn language_ids(&self) -> HashMap<String, String> {
    HashMap::from_iter([
        ("Terraform".into(), "terraform".into()),
        ("Terraform Vars".into(), "terraform-vars".into()),
    ])
}
```

I think it might be helpful in the future to have another way to map
file extensions to specific language ids without having to create a new
language configuration.

### UX Before

![CleanShot 2024-02-07 at 23 00
56@2x](https://github.com/zed-industries/zed/assets/45985/2c40f477-99a2-4dc1-86de-221acccfcedb)

### UX After

![CleanShot 2024-02-07 at 22 58
40@2x](https://github.com/zed-industries/zed/assets/45985/704c9cca-ae14-413a-be1f-d2439ae1ae22)

Release Notes:

- N/A

---

* Part of #5098
@refl3ction
Copy link

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

settings.json

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

maxbrunsfeld added a commit that referenced this issue Feb 27, 2024
* Depends on: #7449
* Closes: #5098

---

This PR adds support for downloading and running the Terraform language
server for `*.tf` and `*.tfvars` files. I've verified that the code
works for `aarch64` and `x86_64` macOS. Downloading new language server
versions on release also works as expected.

Furthermore this PR adds:
- A short docs page for Terraform
- An icon for `*.tf` and `*.tfvars` files

## UX

### File Icons

![CleanShot 2024-02-10 at 23 10
13@2x](https://github.com/zed-industries/zed/assets/45985/6f7cd4f0-e94c-4cfb-b3e9-64b0e33c8a43)

### Completion

![CleanShot 2024-02-13 at 20 54
15@2x](https://github.com/zed-industries/zed/assets/45985/18fafa3b-cb50-4f51-b071-ca9eee3521a6)

### Hover

![CleanShot 2024-02-13 at 20 53
40@2x](https://github.com/zed-industries/zed/assets/45985/4d215315-e019-4d3d-b23c-2691db1803e3)

### Go to definition

![2024-02-13 20 56
28](https://github.com/zed-industries/zed/assets/45985/c21d562f-eb0b-4df9-9175-c53b9923344e)

### Formatting

![2024-02-13 20 59
06](https://github.com/zed-industries/zed/assets/45985/0cdf4ec5-e231-4c8a-a257-cae30a8edc8b)

and more!

## Known issue(s)

@fdionisi discovered that sometimes completion results are inserted with
the wrong indentation. Or rather, if you look closely, they are inserted
with the correct indentation and then something shifts the closing `}`.
I don't think this is related to LSP support and can be addressed in a
separate PR.

![2024-02-13 20 58
16](https://github.com/zed-industries/zed/assets/45985/94a118dd-95f5-4e38-8f83-75fec7a0dddf)

Release Notes:

- Add language server support for Terraform
([#5098](#5098)).

---------

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
@chinyongcy
Copy link

chinyongcy commented May 20, 2024

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

@dbanck
Copy link
Contributor

dbanck commented May 21, 2024

@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.

@avtar
Copy link

avtar commented May 21, 2024

What kind of HCL do you write?

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

@stv-io
Copy link

stv-io commented May 22, 2024

@chinyongcy
Copy link

chinyongcy commented May 22, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement [core label] language An umbrella label for all programming languages syntax behaviors potential extension Functionality that could be implemented as an extension (consider moving to community extensions) unsupported language
Projects
None yet
Development

Successfully merging a pull request may close this issue.