使用spacemacs编辑terraform文件时经常出现是否重启语言服务器的提示

在spacemacs里引入Terraform layer的包,~/.spacemacs配置如下

dotspacemacs-configuration-layers
'(lsp
  git
  (terraform :variables
      terraform-auto-format-on-save t
      terraform-backend 'lsp) 

当编辑一个.tf文件的时候,经常在状态栏提示

Server tfls:3869 exited with status exit(check corresponding stderr buffer for details). Do you want to restart it? (y or n)

甚至一个光标移动都会出发它。每次选择y还是会经常出现。

如何避免这个提示?为什么会经常提示?哪里的配置不对?

已经说的很清楚了,去看看 *lsp-log* 内容中异常退出的原因?(space-b-b 输入 lsp-log 切换 buffer)

ps 看到你 reddit 的回复了,(setq lsp-log-io t) 再试试,然后看看 *lsp-log 开头的另一 buffer

因为 lsp server 错误退出了,要么不用 lsp,要么解决异常退出问题

谢谢你的回复。按照你的设置方法我查看了这个文件

*lsp-log: tfls:1358*

发现这些信息

[Trace - 09:04:46 AM] Sending request 'textDocument/documentLink - (5)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/user12/terraform/module1/main.tf"
  }
}

[Trace - 09:04:47 AM] Received response 'textDocument/documentLink - (5)' in 38ms.
Result: [
  {
    "tooltip": "https://github.com",
    "target": "https://github.com",
    "range": {
      "end": {
        "character": 10,
        "line": 1
      },
      "start": {
        "character": 1,
        "line": 1
      }
    }
  }
]

[Trace - 09:04:58 AM] Received response 'textDocument/documentLink - (13)' in 43ms.
Result: [
  {
    "tooltip": "https://github.com",
    "target": "https://github.com",
    "range": {
      "end": {
        "character": 10,
        "line": 1
      },
      "start": {
        "character": 1,
        "line": 1
      }
    }
  }
]


[Trace - 09:04:58 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "message": "Resource aws_security_group does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 29,
          "line": 3
        },
        "start": {
          "character": 9,
          "line": 3
        }
      }
    },
    {
      "message": "Resource aws_security_group_rule does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 34,
          "line": 17
        },
        "start": {
          "character": 9,
          "line": 17
        }
      }
    },
    {
      "message": "Resource aws_security_group_rule does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 34,
          "line": 29
        },
        "start": {
          "character": 9,
          "line": 29
        }
      }
    },
    {
      "message": "Resource aws_security_group_rule does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 34,
          "line": 42
        },
        "start": {
          "character": 9,
          "line": 42
        }
      }
    }
  ],
  "uri": "file:////Users/user12/terraform/module1/main.tf"
}

另外一个buffer文件

*tfls::stderr*

time="2021-06-21T09:02:27+08:00" level=info msg="Log Level is Debug: false"
e[36mINFOe[0m Server started                               
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }} 
e[36mINFOe[0m {{file:///Users/user12/terraform/module1/main.tf  0 }}

可能是因为在本地的terraform没有执行terraform init获取资源所致。貌似不做mock无法回避这个问题。

因为 lsp server 错误退出了,要么不用 lsp,要么解决异常退出问题

如果不用lsp,有什么替代方案?

没看出来为啥异常退出了,对 terraform 不熟。

不过 *tfls::stderr* 里 log level 没开 debug

要求不高可以不用😂,当然现阶段补全 lsp 确实是比较好的选择,看了下也没有别的 backend 可以选。

打开了log level的debug

M-x toggle-debug-on-error

*tfls::stderr*里的内容看得更多了

...
time="2021-06-21T14:24:31+08:00" level=info msg="Log Level is Debug: false"
e[36mINFOe[0m Server started                               
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x14af3fa]

goroutine 28 [running]:
github.com/hashicorp/terraform/configs.(*Module).appendFile(0xc000e7c600, 0x0, 0xc0006b65b8, 0x1101c9d, 0xc000042410)
	/Users/user12/go/1.16.2/pkg/mod/github.com/hashicorp/[email protected]/configs/module.go:128 +0x3a
github.com/hashicorp/terraform/configs.NewModule(0xc000847d80, 0xa, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15)
	/Users/user12/go/1.16.2/pkg/mod/github.com/hashicorp/[email protected]/configs/module.go:97 +0x1fa
github.com/juliosueiras/terraform-lsp/langserver.TextDocumentComplete(0x1e7d970, 0xc000bfcb10, 0xc000124000, 0x51, 0x10, 0x15, 0x1, 0x0, 0x0, 0x0, ...)
	/Users/user12/terraform/terraform-lsp/langserver/complete.go:56 +0x48e
reflect.Value.call(0x1b75760, 0x1d10f58, 0x13, 0x1cbed0c, 0x4, 0xc000bfcdb0, 0x2, 0x2, 0xc000bfcdb0, 0x199, ...)
	/Users/user12/.goenv/versions/1.16.2/src/reflect/value.go:476 +0x8e7
reflect.Value.Call(0x1b75760, 0x1d10f58, 0x13, 0xc000bfcdb0, 0x2, 0x2, 0x2, 0xc000bfcdb0, 0x1)
	/Users/user12/.goenv/versions/1.16.2/src/reflect/value.go:337 +0xb9
github.com/creachadair/jrpc2/handler.newHandler.func7(0x1e7d970, 0xc000bfcb10, 0xc0002d0540, 0x1c38e60, 0xc000bfcb10, 0x1b87280, 0x246a8a0)
	/Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/[email protected]/handler/handler.go:222 +0x1f4
github.com/creachadair/jrpc2/handler.Func.Handle(0xc0000d23e0, 0x1e7d970, 0xc000bfcb10, 0xc0002d0540, 0x0, 0x0, 0x1e7d970, 0xc000bfcb10)
	/Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/[email protected]/handler/handler.go:23 +0x44
github.com/creachadair/jrpc2.(*Server).invoke(0xc0001ec240, 0x1e7d8c8, 0xc0002d0580, 0x1e69b60, 0xc0000d23e0, 0xc0002d0540, 0x0, 0x0, 0x0, 0x0, ...)
	/Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/[email protected]/server.go:288 +0x175
github.com/creachadair/jrpc2.(*Server).dispatch.func1(0xc000c0e814, 0xc0001ec240, 0xc000bdc420)
	/Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/[email protected]/server.go:185 +0x91
created by github.com/creachadair/jrpc2.(*Server).dispatch
	/Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/[email protected]/server.go:183 +0x137

Process tfls stderr finished

好像是兼容性问题:GitHub - hashicorp/terraform: Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

要求较高,起码可以在保存代码时格式化 :grinning:

谢谢指导。

我是指你这没打开,你自己研究吧🤣