Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly use go modules in vscode?

I have used vscode 1.41.1 on my mac for a few months and it worked good until I started to use go modules for dependency management. At the moment I am rewriting a simple tool and introduce packages for separate functionalities.

My code structure looks like this:

├── bmr.go -> package main & main(), uses below packages
├── check
│   ├── check.go -> package check
│   └── check_test.go
├── go.mod
├── go.sum
├── push
│   ├── push.go -> package push
│   └── push_test.go
└── s3objects
    ├── s3objects.go -> package s3objects
    └── s3objects_test.go

My go.mod file:

module github.com/some-org/business-metrics-restore

go 1.13

require (
        github.com/aws/aws-sdk-go v1.28.1
        github.com/go-redis/redis v6.15.6+incompatible
        github.com/sirupsen/logrus v1.4.2
        github.com/spf13/viper v1.6.1
        github.com/stretchr/testify v1.4.0
        golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
)

All is fine when I invoke go test/run/build commands from the shell. But when I use 'Debug' -> 'Run Without Debugging' I get:

go: finding github.com/some-org/business-metrics-restore/push latest
go: finding github.com/some-org/business-metrics-restore latest
go: finding github.com/some-org/business-metrics-restore/check latest
go: finding github.com/some-org/business-metrics-restore/s3objects latest
build command-line-arguments: cannot load github.com/some-org/business-metrics-restore/check: module github.com/some-org/business-metrics-restore@latest found (v0.0.0-20191022092726-d1a52439dad8), but does not contain package github.com/some-org/business-metrics-restore/check
Process exiting with code: 1

My code currently is in a feature branch and d1a52439dad8 is the first (init) and only commit on master. No code for the tool (incl. 3 mentioned non main packages) is in the master branch. The problem here is that for some reason as you see above vscode fetches state from master and I cannot override this behaviour.

Can anyone help me?

Thanks!

Best Regards, Rafal.

like image 628
Rafał Radecki Avatar asked Jan 14 '20 11:01

Rafał Radecki


People also ask

How do I run a go program in Visual Studio Code?

In Visual Studio Code, open the folder where you'll create the root directory of your Go application. To open the folder, click the Explorer icon in the Activity Bar then click Open Folder. Open a terminal, Terminal > New Terminal, then run the command go mod init sample-app to initialize your sample Go app.

How do I enable the modules integration in VS Code?

Add the setting "go. formatTool": "goimports" and then use Go: Install/Update Tools to install/update goimports as it has recently added support for modules.

How do I enable go module in Visual Studio Code?

Go module setup in Visual Studio Code. To enable module support in Visual Studio Code, add the following to your IDE settings (press CTRL+,): This will trigger VS Code to install the go package gopls (The Go Language Server). Visual Studio uses gopls, an implementation of the Language Server Protocol server for Go.

What is a Visual Studio Code Module?

This articles explains the setup and recommends settings for Visual Studio Code. A module is a collection of related Go packages that are versioned together. All dependencies and their exact versions of your package imports are stored in the go.mod file. See this Github page for more in-depth information on modules.

Why use VS Code with go?

Why use VS Code with Go? Visual Studio Code is a great polyglot editor that combines a clean, highly configurable, editing interface with an extensive ecosystem of plugins that provide support for a wide variety of programming languages and file types.

What version of Go is used in Visual Studio Code?

The used version of Go is 1.12. Within this package, three imports are used. This will trigger VS Code to install the go package gopls (The Go Language Server). Visual Studio uses gopls, an implementation of the Language Server Protocol server for Go. It supports features, such as It is currently in alpha state, so it is not stable.


2 Answers

I realized that if the go.mod is not at the root of your project VSCode does not work properly. I have an AWS SAM project with the following structure:

├── Makefile
├── README.md
├── nic-update
│   ├── go.mod
│   ├── go.sum
│   ├── main.go
│   ├── main_test.go
│   └── r53service
│       └── r53.go
├── samconfig.toml
└── template.yaml

and the only way it works if by starting VSCode from the nic-update directory.

My go.mod has the following content:

require (
    github.com/aws/aws-lambda-go v1.13.3
    github.com/aws/aws-sdk-go v1.32.12
)

module github.com/jschwindt/ddns-route53/nic-update

go 1.14
like image 193
Juan Schwindt Avatar answered Oct 17 '22 17:10

Juan Schwindt


I realized that if the go.mod is not at the root of your project VSCode does not work properly

That might now (Oct. 2020) be supported, as a consequence of gopls v0.5.1 and its experimental feature Multi-module workspace support from the proposal 32394.

Even if you don't have multiple modules, a go.mod in a sub-folder (instead of the root folder of your project) will be better managed (if you activate the gopls.experimentalWorkspaceModule setting).


As noted by kayochin in the comments:

  • The setting should be "gopls": {"build.experimentalWorkspaceModule": true}
  • See the documentation "gopls / Settings / experimentalWorkspaceModule bool".
like image 19
VonC Avatar answered Oct 17 '22 18:10

VonC