Go modules vs VSCode

概要

Go1.13からデフォルトになるGoの依存パッケージに対するバージョン管理機構Go modulesについて、導入しようと思ったらVSCode周りが辛かったので備忘録としてまとめる。 間違ってたらごめんなさい。

TL;DR

  • この記事読んでおけばだいたいわかる
  • VSCodeでもGo modules対応はできる
    • Language Servergoplsを使う
      • 依存モジュールはGo modulesから取ってくるようになる
      • 補完も効く
  • Language Serverなんもわからんけどすごい
  • やっぱりJetBrainsはすごい

Go modules

端的に表現すると、ついにGoに組み込まれた標準バージョン管理機構、depからの移行先。 この辺読むといいと思いますです。

導入しようとしたきっかけ

  • 今から走るプロジェクトにdep入れるのどうなん?:thinking_face:
    • ライブラリでもgo modules対応見かけるようになってきたしそろそろいいのでは?
    • CI jobでdepvendorの管理をもうしたくない
  • RenovateがGo対応してるやん!って思ったらGo modulesだけだった

立ちはだかる壁VSCodeと理想のGoLand

  • Go modules support in Visual Studio Code
    • As you already know, the Go extension depends on various Go tools from the community to provide language support. Some of these tools support Go modules already, and others don't. Therefore, you will see that some features of the Go extension will work and others won't when using Go modules.
    • まあそれはそう
  • Go modules (vgo) - GoLand 2019.1 Help
    • さすがのJetBrains
    • 個人はともかく会社で全員分買うか?と言う問題
      • 費用というより宗教的な話

希望のLanguage Server Protocol

  • Language Server Protocolについては以下

Official page for Language Server Protocol

Adding features like auto complete, go to definition, or documentation on hover for a programming language takes significant effort. Traditionally this work had to be repeated for each development tool, as each tool provides different APIs for implementing the same feature.

A Language Server is meant to provide the language-specific smarts and communicate with development tools over a protocol that enables inter-process communication.

The idea behind the Language Server Protocol (LSP) is to standardize the protocol for how such servers and development tools communicate. This way, a single Language Server can be re-used in multiple development tools, which in turn can support multiple languages with minimal effort.

LSP is a win for both language providers and tooling vendors!

Goの補完ライブラリの歴史的経緯とGoのLanguage Server

gocode やめます(そして Language Server へ)

  • Goの補完ライブラリのほとんどは内部にgocodeを抱えている
  • しかし、gocodethis project is not maintained anymore, look for alternatives or forks if you need Go autocompletion toolとなってしまった
  • GoのLanguage Server実装であるsourcegraph/go-langserverも内部ではgocodeを使っていた

Google製Go Language Server gopls

  • golang.org/x/tools/cmd/gopls
    • Goコミュニティとしてこれに統一することが決まっている、Google製 Go Language Server。
    • 元々golspだった。
    • 他のLanguage Serverは、以下のようにメンテナンスをやめている文が出ている。
  • sourcegraph/go-langserver

    Note: We have deprioritized work on this language server for use in editors in favor of Google's upcoming Go language server. It is in the best interests of the community to only have a single language server. If you want to use a Go language server with your editor in the meantime, try https://github.com/saibing/bingo, which is a partial fork of this repository with fixes for Go modules and other editor bugs.

  • saibing/bingo

    I am very sorry that I have planned not continue to maintain this project. According to the issue #13 collaborating on golsp, I hope that we will eventually have a unified language server: gopls. It is Google's upcoming Go language server. It is in the best interests of the community to only have a single language server.

goplsのVSCodeへの適用手順

感想

  • 雰囲気でGoを書いている身からしたら十二分に低レイヤな話だった。
    • VSCodeってこの辺(LSP Client)もElectronで実装してるの?キモくない?
  • IDEがそんなに好きではないので、Language Server Protocolでいろんな言語が補完できるようになったら幸せだね。