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

Format on save does not work #1265

Closed
audiolomb opened this issue Nov 3, 2022 · 25 comments
Closed

Format on save does not work #1265

audiolomb opened this issue Nov 3, 2022 · 25 comments
Labels
bug Something isn't working

Comments

@audiolomb
Copy link

Issue Description

Format on Save does not work

Environment Information

Terraform Information

Version: 1.2.5
Platform: windows_amd64
Outdated: true

Visual Studio Code

Name Version
Operating System Windows_NT x64 10.0.17763
VSCode 1.73.0

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand)
Extension Author Version
isort ms-python 2022.4.0
powershell ms-vscode 2022.10.0
python ms-python 2022.18.0
remote-containers ms-vscode-remote 0.262.3
remote-ssh ms-vscode-remote 0.90.1
remote-ssh-edit ms-vscode-remote 0.84.0
remote-wsl ms-vscode-remote 0.72.0
svn-scm johnstoncode 2.15.5
terraform hashicorp 2.24.3
vscode-remote-extensionpack ms-vscode-remote 0.21.0
vscode-yaml redhat 1.10.1

Extension Logs

Find this from the first few lines of the relevant Output pane:
View -> Output -> 'HashiCorp Terraform'

@dbanck
Copy link
Member

dbanck commented Nov 3, 2022

Hi @audiolomb!

Sorry to hear that format on saving isn't working for you. Do you mind sharing your VS Code settings and the extension logs, so we can better identify the issue. Thanks!

@cschar
Copy link

cschar commented Nov 4, 2022

same for me, looked into extension logs and saw

[2022-11-04 13:07:36.580] [exthost] [error] Error: fmt was first introduced in Terraform 0.7.7: unable to parse version "": Malformed version: 
    at je (/Users/cody/.vscode/extensions/hashicorp.terraform-2.24.2-darwin-arm64/out/extension.js:17:223)
    at Ue (/Users/cody/.vscode/extensions/hashicorp.terraform-2.24.2-darwin-arm64/out/extension.js:16:11251)
    at Immediate.<anonymous> (/Users/cody/.vscode/extensions/hashicorp.terraform-2.24.2-darwin-arm64/out/extension.js:16:11101)
    at processImmediate (node:internal/timers:466:21)

@dbanck
Copy link
Member

dbanck commented Nov 7, 2022

same for me, looked into extension logs and saw

Which Terraform version are you using? Unfortunately, the language server cannot determine the version and cannot offer formatting support without knowing it.

@dawsonar802
Copy link

dawsonar802 commented Nov 7, 2022

Hello, I am experiencing the same issues with format on save. It will essentially hang and not complete. I am using a devcontainer as well. I have updated to TF version 1.3.4 and vscode version 1.73.0. I have attached a screenshot of the log file, which essentially this message is just repeated a few times each time I try to save. All I did was go into a working main.tf file, add some text at the bottom of the file, then remove it and save the file. Vscode tells me there are problems in the file, then the formatting hangs.

Screenshot 2022-11-07 070103

** Update **
I reverted to version 2.22 of the extension and things seem to be working fine.

@rd-michel
Copy link

same problem here:
vscode: 1.73.00
Terraform v1.3.4
vscode extension version: v2.24.3

@dawsonar802
Copy link

same problem here: vscode: 1.73.00 Terraform v1.3.4 vscode extension version: v2.24.3

@rd-michel are you by chance using modules hosted in a private registry? I was curious if maybe that is my issue, because I have another customer where I have a similar setup and things seem to work fine, the difference is the non-working customer we are using private registry hosted modules and the working customer, they are all local.

@audiolomb
Copy link
Author

The problem is with version 2.24.3.
I reverted back to 2.24.2 and autoformat on save works fine.

@dbanck dbanck added bug Something isn't working and removed waiting-response labels Nov 9, 2022
@radeksimko
Copy link
Member

I unfortunately cannot confirm that the root cause is the same for everyone - even if the problem is surfaced as "formatting doesn't work".

@cschar Thank you for sharing the relevant part of the log. Building on that - Can you run which terraform in the same directory and environment? Is it possible that you use tfenv or another kind of wrapper around terraform, or version manager of some kind? Can you try running terraform version? What I'm guessing the problem may be is that the wrapper prints some errors (such as "terraform version x.y.z not installed") into stderr, and we attempt to parse the stdout for version, which is empty.


@dawsonar802 @audiolomb @rd-michel Can each of you look into the HashiCorp Terraform log pane and find & share any parts of it between request for "textDocument/formatting" and Response to "textDocument/formatting"? Alternatively sharing the whole log would help - we can look it up.

Screenshot 2022-11-15 at 08 24 20

@dawsonar802
Copy link

@radeksimko , I updated the extension to v2.25.0 and reproduced the issues. Below is a screenshot if it helps at all with the behavior. I made a change, saved the file, and then this window will come up for several seconds/minutes.
Screenshot 2022-11-15 071002

Is there another may I can send/share the log with you vs posting it here? The log file is pretty large and has a lot to be redacted it looks like. If not, here is another screenshot which has the /formatting section:
Screenshot 2022-11-15 071425

To add a bit of content, in our case anyway, we have a root module that is calling other modules. The root module is local and the other modules are hosted in a private registry.

@radeksimko
Copy link
Member

radeksimko commented Nov 15, 2022

@dawsonar802 Thank you for sharing the log. According to that, it appears you're likely affected by hashicorp/terraform-ls#1063 where LSP requests (including the formatting ones) are delayed due to slow external network requests.

Can you try commenting out any/all module calls and reproduce it? Relatedly - if you can share any details about the modules sources involved, that would be helpful!

The root module is local and the other modules are hosted in a private registry.

That might be the reason why the metadata for those aren't available on the public registry (according to the log with 404s) - which would confirm your theory above. That said, responding with 404s should be relatively fast still. I guess the issue with blocking may still come up when there's many module calls, i.e. many HTTP requests to send and process. The API unfortunately doesn't have any kind of batching functionality, but we should of course prevent it from blocking other LSP requests.

Is there another may I can send/share the log with you vs posting it here?

Feel free to send it to radek <at> hashicorp.com and/or encrypt it using my PGP key: https://keybase.io/radeksimko

@dawsonar802
Copy link

dawsonar802 commented Nov 15, 2022

Thanks @radeksimko . I sent you an email with the logfile. With regards to commenting out the modules, we have another customer we are working with that is using all local modules and using the affected versions of the extension I do not see the issues there. It is a similar setup with the same version of TF and in a devcontainer as well. I did try commenting out basically all of the resources and modules leaving only a provider block and a data block which seems to not cause any issues. I added back one module block and still did not see the issue. I would expect if more are added the issue would come up again but it seems only a few resources is not triggering it.

For reference, there are ~26 (private registry) custom modules and 67 other resource blocks within this root module.

@sarahkadar
Copy link

Format on save continues to be basically useless for me, it can take 10-20 seconds to save a file on a reasonable network connection.
Screenshot 2023-01-04 at 14 05 33

@github-actions
Copy link

github-actions bot commented Feb 4, 2023

Marking this issue as stale due to inactivity over the last 30 days. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

Thank you for understanding.

@github-actions github-actions bot added the stale label Feb 4, 2023
@dawsonar802
Copy link

To my knowledge this has not been fixed. I will try and confirm next week, but have just been using an earlier version of the extension until there was an update here.

@github-actions github-actions bot removed the stale label Feb 4, 2023
@sarahkadar
Copy link

@dawsonar802 which is the last good version?

@dawsonar802
Copy link

I have been using 2.22, but 2.24.2 works as well from another post here.

@dawsonar802
Copy link

I was on version 2.24.2 and facing the issues. I upgraded to the latest 2.52.2 and still had the issues. I have reverted back to 2.22.0 and do not have the issues currently. Have there been any updates on what may be causing this issue? From what I can tell this seems to mainly happen when using modules, specifically with the source being something other than local. For example, I am working on a new common module and have it currently local vs being in a registry. I can save it fine, auto complete works fine, and no hangs on format when using the newer versions mentioned above.

@albertorm95
Copy link

<= v2.23.0 Works for me

@philipsparrow
Copy link

I've been having the same issue. brew installed tfenv, tried installing terraform-ls using Brew and pointing the extension to that binary, but I was getting this same error about the malformed version "".
Downgrading to v2.23.0 worked for me.

@sarahkadar
Copy link

Thanks everyone, downgraded to v2.23.0 and so far so good.

@evbo
Copy link

evbo commented Mar 17, 2023

version v.2.23.0 does not work for me inside a .devcontainer. Here is my settings, any suggestions?:

{
    "workbench.colorTheme": "Default Dark+",
    "security.workspace.trust.untrustedFiles": "open",
    "files.watcherExclude": {
        "**/.bloop": true,
        "**/.metals": true,
        "**/.ammonite": true
    },
    "files.autoSave": "afterDelay",
    "editor.mouseWheelZoom": true,
    "editor.minimap.enabled": false,
    "terminal.integrated.scrollback": 100000,
    "window.zoomLevel": 3,
    "terminal.integrated.profiles.linux": {
        "bash": {
            "path": "/bin/bash"
        }
    },
    "terminal.integrated.defaultProfile.linux": "bash",
    "terraform.experimentalFeatures.validateOnSave": true,
    "terraform.experimentalFeatures.prefillRequiredFields": true,
    "[terraform]": {
        "editor.defaultFormatter": "hashicorp.terraform",
        "editor.formatOnSave": true,
        "editor.formatOnSaveMode": "file"
    },
    "[terraform-vars]": {
        "editor.defaultFormatter": "hashicorp.terraform",
        "editor.formatOnSave": true,
        "editor.formatOnSaveMode": "file"
    }
}

@ThePlenkov
Copy link

I've spent quite some time to come here. Any updates on this topic?

@albertorm95
Copy link

albertorm95 commented May 2, 2023

Any of you using <=0.23.0 facing the
https://support.hashicorp.com/hc/en-us/articles/13177506317203
Screenshot 2023-05-02 at 10 53 15 AM

@radeksimko
Copy link
Member

@albertorm95 This sounds like a separate topic, but in general I'd suggest upgrading to the latest version to address that. If you experience issues with that version, we can discuss those issues and get to the bottom of it.

As for the original issue in the first post - We have made some improvements wrt performance in the latest version 2.26.0 as per hashicorp/terraform-ls#1258 We plan to make some more changes 🔜 as part of hashicorp/terraform-ls#1056 to help address other similar situations.

There were other root causes discussed here, including missing Terraform CLI or use of wrappers or 3rd party package managers such as tfenv. We'd be happy to discuss those in a separate issue if anyone can provide details about their situation and environment.

With that in mind, I'll close this issue as the initial issue (involving inaccessible Registry modules delaying formatting) was addressed.

@github-actions
Copy link

github-actions bot commented Jun 2, 2023

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 details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests