How do I configure goland to recognize 'mod' packages?
Solution 1
The answers on this site worked for me. Basically, enable go modules in GoLand via:
Make sure Go Modules integration is enabled in settings (Preferences / Settings | Go | Go Modules), and GOPATH indexing is disabled (Preferences / Settings | Go | GOPATH | Index entire GOPATH).
As soon as I did this, the red imports went away and everything worked.
Solution 2
GoLand support
The latest version of GoLand implemented support for vgo and go modules, but it hasn't caught up with go1.11rc1 syntax changes. Just in case it helps someone in the interim, I am going to document the things I tried and their problems and successes.
TL;DR: Don't put your project inside $GOPATH
AND create your new project as a "Go Module (vgo)" type, OR turn that setting on for existing projects.
With go1.11rc1 installed as your global go
, there are three basic use cases for go mod
projects in GoLand...
Create a new project inside $GOPATH
:
- Create a new project of type "Go Module (vgo)": File -> New, select "Go Module (vgo)"
- Set your project directory to something inside
$GOPATH
:$GOPATH/src/github.com/stevetarver/insidegopath
- Create your
main.go
file referencing a package that does not exist in your$GOPATH
. - Add that package to your imports.
Using go get
the GoLand way via vgo
as described in the gif here:
- Click on the import package.
- Click on the red inspection bulb.
- Click on "Sync packages of ...".
-
FAIL:
go: go mod -sync is now go mod tidy
Using go get
the GoLand embedded terminal way:
- Open the embedded terminal.
-
go get
your import. -
FAIL:
ᐅ go get github.com/urfave/cli go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src; ignoring go.mod; see 'go help modules'
Let's turn that variable on and try again:
- Note: the terminal plugin preferences provide no way to set environment variables.
- Set
GO111MODULE=on
: Open Preferences -> Appearance & Behavior -> Path Variables, and addGO111MODULE=on
. - Exit the terminal, retry, restart GoLand, retry, same failure as above.
-
env | grep GO111MODULE
in the terminal produces nothing. - NOTE: if this would have worked, it would have been a bad solution - GoLand does not appear to have a per-project settings for this - that variable would have been turned on for all projects which would break those that aren't ready for go modules.
- According to this answer, you can create a custom command line launcher to include this env var, but eeuuwww - how would you keep track of when to start GoLand normally and when to use the command line launcher?
You could set GO111MODULE=on
in your shell init script, but that would break all the projects that don't use go modules yet.
You could also prefix each go command with the env var: export GO111MODULE=on; go get github.com/urfave/cli
or create a go
shell script wrapper in your project directory that does this for you.
None of these are really workable solutions, but part of the point of go modules is escape from the dreaded go workspace, so read on, it gets better
Create a new project outside $GOPATH
:
- Create a new project of type "Go Module (vgo)": File -> New, select "Go Module (vgo)"
- Set your project directory to something outside
$GOPATH
- Fix up your
go.mod
: the generated file containsmodule "outsidegopath"
, but we want something likemodule github.com/stevetarver/outsidegopath
. This is a bit wonky - GoLand will try to rewritego.mod
and remove parts of the path. Repeat a couple times and it will stop trying. - Create your
main.go
file. If you create this through the ide as a go file, it will containpackage outsidegopath
. Fix that to bepackage main
. - Now you can
go get github.com/urfave/cli
and it is fetched to$GOPATH/pkg/mod
as expected.
Add go mod
support to an existing new project:
This turned out to be really simple - best way of working with go modules in GoLand:
- Open Preferences: Go -> Go Module (vgo), and check "Enable Go Modules (vgo) integration"
- Works as described above - but you create your own
go.mod
withgo mod init module-name
.
Solution 3
The module management should be easier with Go 1.13 (Aug. 2019):
The
GO111MODULE
environment variable continues to default toauto
, but theauto
setting now activates the module-aware mode of thego
command whenever the current working directory contains, or is below a directory containing, ago.mod
file — even if the current directory is withinGOPATH/src
.This change simplifies the migration of existing code within
GOPATH/src
and the ongoing maintenance of module-aware packages alongside non-module-aware importers.
That means the all "Don't put your project inside $GOPATH
" part will no longer be needed.
As long as there is a go.mod
file, modules will be recognized, from command-line or from an IDE like Goland.
Related videos on Youtube
Steve Tarver
I love solving complex problems in just about every domain. This means you will find me hanging around all levels of the stack, from front end, to server, to datastore and addressing design and implementation issues in usability, performance, scalability, infrastructure, and operations.
Updated on November 25, 2020Comments
-
Steve Tarver over 3 years
I am taking go1.11rc1 for a spin and the first thing I noticed is that goland does not recognize imports.
The goland version announcement says: "support for Go modules out of the box (formerly known as vgo)"
Anyone know how to fix this?
Problem:
- Packages like "github.com/urfave/cli" colored red and hover text says: "Cannot resolve directory..."
- Imported package items like "NewApp" in "app := cli.NewApp()" colored red and hover text says: "Unresolved reference ..."
Steps to reproduce:
- Install go1.11rc1: remove the current install, install 1.11rc1, check version.
- Create a new project directory outside the go path:
mkdir pjg && cd pjg
- Create a
go.mod
file:go mod init github.com/stevetarver/pjg
- Add a package to the project:
go get github.com/urfave/cli
go.mod
file now looks like:module github.com/stevetarver/pjg/v1 require github.com/urfave/cli v1.20.0 // indirect
Create
main.go
:package main import ( "fmt" "log" "os" "github.com/urfave/cli" ) func main() { app := cli.NewApp() app.Name = "boom" app.Usage = "make an explosive entrance" app.Action = func(c *cli.Context) error { fmt.Println("boom! I say!") return nil } err := app.Run(os.Args) if err != nil { log.Fatal(err) } }
View
main.go
in goland, and hover over red text to see problem.- mod packages are stored in
$GOPATH/pkg/mod/
- goland version: 2018.2.1
- go version: go1.11rc1 darwin/amd64
Notes:
-
$GOPATH
is set correctly -go get
put the package in the right place, GOPATH in env matches goland preferences. - Setting goland preferences Go -> GOPATH -> Module GOPATH to
/Users/starver/code/go/pkg/mod
did not fix this.
-
dlsniper almost 6 yearsGoLand will support Go 1.11 RC1 from 2018.2.2, which at the time of writing is next week, and the error ` go: go mod -sync is now go mod tidy ` will be fixed then.
-
Steve Tarver almost 6 yearsThanks for the info @dlsniper - that is an amazingly quick follow up to the new go rc. Note - I am a vocal advocate of JetBrains products and maintain a personal All Products subscription - not finding fault, just sharing in case it helps someone. Previously, JetBrains has published the opinion that they would wait for new language versions to gel before officially supporting new features - pragmatic and reasonable. I thought there might be a quarter before supporting big changes like this were supported and the info would be more durable.
-
Arnaud P about 3 yearsIIUC, this should be the accepted answer. Certainly spot on for me 🙏
-
s0xzwasd almost 3 yearsAgreed with Arnaud :)
-
Vikram Hosakote over 2 yearsYes, this is the correct answer and worked for me too. My go project doesn't have
go.mod
and the steps in intellij-support.jetbrains.com/hc/en-us/community/posts/… worked for me. -
user1894292 over 2 yearsMe too. Thanks @gMale
-
Dallin about 2 yearsI had this turned on and was still seeing the problem described. Turning it off and back on again fixed it.