Search code examples
gointellij-ideagolandgo-modules

In IntelliJ imports are not loading, even though go modules integration is enabled


When using IntelliJ IDEA with the Go plugin, or GoLand and using Go 1.11 or newer, you can enable Go modules by going to Preferences -> Languages & Frameworks -> Go -> Go Modules (vgo) and check the box 'Enable Go Modules (vgo) integration'.

When you have a project which was properly initialised by go mod init, and therefore has a go.mod and go.sum file, it will download the dependencies when building from command line with go build or from IntelliJ by pressing the green play icon.

However, the import statements to remote dependencies might still be red, even though they seem to have been downloaded and appear in the go.mod file.

Curiously enough, the go project will actually run from the command line, just not from within IntelliJ. Why is this and how can I fix this?


Solution

  • The reason this approach does not work, has to do with the configuration in IntelliJ. That is why it will work from command line, just not in IntelliJ.

    There are two ways to fix this:

    1. Go into Preferences -> Languages & Frameworks -> Go -> Go Modules (vgo) and make sure the box 'Vendoring mode' is not checked.
    2. Make sure you have a folder called 'vendor' in your IntelliJ project. Now the box 'Vendoring mode' can be safely checked.

    With the first approach the dependencies defined in your go.mod file might be re-downloaded, for example when the go project is run from a place where these dependencies are not in the local package registry.

    With the second approach the dependencies are put in the 'vendor' folder and do not necessarily need to be re-downloaded, they are sort of made part of the project.