hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
913 stars 180 forks source link

"Workspace not initialized" error when vscode is opened from a superdirectory #366

Closed dimisjim closed 4 years ago

dimisjim commented 4 years ago

terraform version: 0.12.24 terraform-vscode: 2.0.1 os: ubuntu 20.04

I am running vscode that contains multiple repos and directories. A few directory levels beneath, there is a terraform workspace.

Actual Behavior

[Error - 10:01:22 PM] Server initialization failed. Message: Workspace not initialized. Please run terraform init in /vscode/workspace/path Code: -32098

Expected Behavior:

Language server automatically being able to detect the terraform workspace in one of the subdirectories, OR having the ability to define it in the Global or Workspace specific settings json, for example, something like:

"terraform.languageServer": {
    "args": [
      "serve",
      "-tf-workspace=path/to/workspace",
    ]
}

Similar to https://github.com/hashicorp/vscode-terraform/issues/329 but this one assumes that vs workspace is >= 1 levels above the root terraform workspace.

rauvolaj commented 4 years ago

same issue as above, and my folder structure follows this comment in #329

moskyb commented 4 years ago

Same issue here, my work has a Terraform monorepo for all of our IaC. I tried adding the reistered_providers fix mentioned at the top of #329, and it stopped the error, but the Language Server still won't give me any completions

J0F3 commented 4 years ago

Same here. Terraform configuration is not in the root but in a sub directory as the Terraform code is just part of the wohle project. This problem makes the extension actually useless for all my projects now. ☹

What makes it even worse it that this new behavior is not noted anywhere in the release notes. Furthermore no real workaround seems to be available, right now. The previous version (still from Mikael Olenfalk) had no problem whit that and worked perfectly in my projects/workspaces. But now after the automatically update to the new version it is completely broken (no code completion, no proper syntax highlighting and no formatting anymore) without any warning forehand. 🤬

I do not know what has changed that the behavior is now that different but it makes the current version of the extension worse that the previous! So please fix that immediately.

Please also understand that very often the Terraform code is part of a whole project and the project consists not only of some .tf files in the root. (Apart some very simple, not real world, examples maybe.) So the extension must support that out of the box.

ciaransweet commented 4 years ago

Also experiencing this, would be really handy to have an option to specify the directory where the Terraform scripts are.

dimisjim commented 4 years ago

@J0F3 You can disable it for now by setting the external flag in the settings json to false

"terraform.languageServer": {
    "external": false,
    "args": [
      "serve",
      "-tf-exec=/opt/hashicorp/terraform",
    ]
}
J0F3 commented 4 years ago

@dimisjim Yes, I know. But does not help me either. I do not want do disable the language server. The point is, that I had a working Terraform 0.12 support in VS Code with the extension and now, since the new version, the extension does basically not work in any of my projects anymore. If I have to disable the language Server I do not see much advantage of this extension anymore. 😉

dimisjim commented 4 years ago

Previous extension version was also not working (at least for tf v0.12). I used it for having simple color code formatting for the .tf files, so by disabling it again, it basically has the same functionality as before now.

FernandoMiguel commented 4 years ago

Previous extension version was also not working (at least for tf v0.12). I used it for having simple color code formatting for the .tf files, so by disabling it again, it basically has the same functionality as before now.

previous version worked just fine for me with 0.12

dimisjim commented 4 years ago

@FernandoMiguel

That's bizarre. The reason why the language server project was created and the whole point of this ongoing development was that the existing extension did not support hcl2 (tf 0.12.x)

What features were working in the previous version for you?

FernandoMiguel commented 4 years ago

That's bizarre. The reason why the language server project was created and the whole point of this ongoing development was that the existing extension did not support hcl2 (tf 0.12.x)

What features were working in the previous version for you?

@dimisjim more like what wasnt! everything worked fine. auto complete, error detection, linting, fmt. everything

FernandoMiguel commented 4 years ago

all you had to do was to follow https://github.com/hashicorp/vscode-terraform/issues/157

dimisjim commented 4 years ago

You are referring to a huge issue thread.

What exactly did you have to follow for it to work perfectly? Because from the comments in it, it was quite clear that the support was buggy and by no means complete. Otherwise why wasn't the issue closed and officially released in the vs marketplace?

FernandoMiguel commented 4 years ago

You are referring to a huge issue thread.

where the previous dev kept releasing updates to the LS and the extension

What exactly did you have to follow for it to work perfectly?

error detection, linting, fmt. lint is now disabled due to not supporting tflint https://github.com/hashicorp/vscode-terraform/issues/337

but to not flag broken resources? not be able to fmt my code automatically on save? to totally fail to init, to not work if it is in a subdir (this very own issue)

dimisjim commented 4 years ago

where the previous dev kept releasing updates to the LS and the extension

ok I might have missed it then and assumed it was never really there as it was never officially released and always deemed experimental by the developers themselves.

In any case, it doesn't change the fact that the current 2.0.1 is more like 0.0.1 than 2.x.x

FernandoMiguel commented 4 years ago

v2 doesnt work. period have to follow https://github.com/hashicorp/vscode-terraform/issues/372 to downgrade

taiidani commented 4 years ago

Agreeing with the above sentiments. Pre-2.0.0 the language server crashed occasionally but otherwise was auto-formatting and auto-completing my 0.12 definitions. I almost exclusively have my Terraform in infra/ subdirectories of my workspace or have multiple Terraform installations in a single workspace, so this update has disrupted my preferred layout.

georgegil commented 4 years ago

Agreeing with the above sentiments. Pre-2.0.0 the language server crashed occasionally but otherwise was auto-formatting and auto-completing my 0.12 definitions. I almost exclusively have my Terraform in infra/ subdirectories of my workspace or have multiple Terraform installations in a single workspace, so this update has disrupted my preferred

100%

i cannot believe this was pushed out without consideration of using TF in subdirectories. SMH

nbetm commented 4 years ago

I understand everyone's frustration (I'm one), but as it's stated here the new module/terraform-ls does not support nested modules (sub-dirs).

That being said, I found a very simple workaround that works well for me. I added a single terraform-ls.tf file at the root of the project containing only the list of providers I regular use.

I believe that's what terraform-lsp inside your ~/.vscode/... directory.

dimisjim commented 4 years ago

@nbetm

tried it, getting same error

nbetm commented 4 years ago

@dimisjim

Make sure you run terraform init from the root of your project opening vscode (that should do it). also, here's a small snippet of my terraform-ls.tf:

#
# Providers Config -------------------------------------------------------------
#

provider "null" {
  version = "~> 2.0"
}

provider "template" {
  version = "~> 2.0"
}

provider "aws" {
  version = "~> 2.0"
  region = "us-east-1"
}

provider "datadog" {
  version = "~> 2.5"
}

provider "github" {
  version = "~> 2.8"
}
dimisjim commented 4 years ago

Ok works this way, but you still get this annoying change of focus in the output tab down below, basically this: https://github.com/hashicorp/vscode-terraform/issues/371

nbetm commented 4 years ago

@dimisjim, I haven't run into the annoying change of focus in the output tab yet. but I will be writing some code later today. I will share my final experience with the server being enabled!

barneyparker commented 4 years ago

@nbetm This does work - its a bit of a hack, but i'll do for now! :+1:

opub commented 4 years ago

There is a "Terraform (forked)" extension (l2fprod.terraform-fork v1.4.28) by Frederic Lavigne that seems to be a fork of Mikael Olenfalk's v.1.4 release. I have switched to this until this issue or the upstream version pinning issue are fixed.

jongio commented 4 years ago

Confirming that this: https://github.com/hashicorp/vscode-terraform/issues/366#issuecomment-642763545 works as a stopgap

paultyng commented 4 years ago

Going to close this to merge in with the existing initalization issues or the main folder structure issue: https://github.com/hashicorp/terraform-ls/issues/32#issuecomment-639543256

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the context necessary to investigate further.