VS Code Go: $GOPATH/go.mod exists but should not - go

I'm trying to use the Go features of VS Code (like 'Go to definition') in a package with a go.mod. However, if I go into the repository's main directory (with the go.mod) and code ., I get this error:
Error loading workspace folders (expected 1, got 0) failed to load view for file:///Users/kurt/Documents/http-enrollment-portal: err: exit status 1: stderr: $GOPATH/go.mod exists but should not
I've read that I might have to update the gopls language server, so I pressed Cmd + Shift + P and selected "Go: Install/Update Tools" and selected gopls; however, this gives me the same error:
go.toolsGopath setting is not set. Using GOPATH /Users/kurt/Documents/http-enrollment-portal:/Users/kurt/go
Installing 1 tool at /Users/kurt/Documents/http-enrollment-portal/bin in module mode.
gopls
Installing golang.org/x/tools/gopls FAILED
1 tools failed to install.
gopls:
Error: Command failed: /usr/local/opt/go#1.12/bin/go get -v golang.org/x/tools/gopls
$GOPATH/go.mod exists but should not
$GOPATH/go.mod exists but should not
The strange thing is that 'Go to definition' does work in other repositories. I suppose a possible solution is to set the go.toolsGopath?

go env -w GOPATH=
GOPATH and Modules
this link
When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).

I had a similar issue. So to explain I am using https://www.mongodb.com/blog/post/quick-start-golang-mongodb-starting-and-setup
To try Mongodb with Go. In VS Code I first installed go mod init quickstart
Then I used go get go.mongodb.org/mongo-driver and got the error $GOPATH/go.mod exists but should not.
I had previously set my GOPATH according to https://www.freecodecamp.org/news/setting-up-go-programming-language-on-windows-f02c8c14e2f/ (see Phase 3 on this page).
I went into my Windows Environment Variables and deleted my GOPATH. On the Windows 10 start menu go to System then Advanced system settings under the Advanced tab click the Environment Variables button and I deleted my GOPATH variable.
Next back in VS Code I again try go get go.mongodb.org/mongo-driver and it works.

you have to go to the folder where you created your go project and delete the go.mod file after you create another file with the command : go mod init nameproject on your command prompt (power

Related

Using `shirou/gopsutil` Go package as a vendored package fails on macOS (10.14.6)

I was trying to build influx/telegraf locally and vendor all the packages using go mod vendor command. However, interestingly, the build fails after that with the following error:
# github.com/shirou/gopsutil/host
vendor/github.com/shirou/gopsutil/host/host_darwin_cgo.go:9:11: fatal error: 'include/smc.c' file not found
#include "include/smc.c"
^~~~~~~~~~~~~~~
Steps to reproduce (after setting GOPATH variable):
# Fetch the project, e.g. influx/telegraf
go get -d github.com/influxdata/telegraf
# CD into the project
cd $GOPATH/src/influxdata/telegraf
# Checkout v1.14.5 (the current latest version)
git checkout v1.14.5
# Fetch the modules under vendor/ directory
go mod vendor
# Then modify the Makefile as follows:
# 1. Remove the `deps` target and its reference in the `all` target; it is not needed anymore, as we fetched all modules.
# 2. Add the `-mod vendor` flag everywhere `go build`, `go install`, or `go test` is called. This is necessary since Telegraf v1.14.5 is still on version 1.12 of Go, which requires this flag to use the modules under `vendor/` directory.
# Finally, build the project and you should get the error above
make
It turned out that this error is caused by a well known issue whereby go mod vendor prunes non-package directories, hence causing the host/include folder of shirou/gopsutil, which contains the smc.c file, to be pruned:
https://github.com/golang/go/issues/26366
The impact of this issue on shirou/gopsutil is already reported:
https://github.com/shirou/gopsutil/issues/832
There is no easy solution apparently, but a work-around for now is to manually copy the missing directories to vendor/github.com/shirou/gopsutil. In my case, I copied freebsd_headers and include from $GOPATH/pkg/mod/github.com/shirou/gopsutil/host to $GOPATH/src/github.com/influxdata/telegraf/vendor/github.com/shirou/gopsutil and it worked.
I hope you find this helpful.
As suggested by #Rafid, you can do a manual copy.
I found this below work-around to be helpful as well.
Run these commands
go get github.com/shirou/gopsutil
go mod vendor

go build cannot find appdynamics go-sdk package

go build is unable to find the 'appdynamics' package, even though the GOPATH is properly set. I downloaded the package, copied it onto the GOPATH: ~/go/src/appdynamics and ran go install appdynamics. I am using go v.1.10 on Ubuntu 18.4.
Visual Studio Code is able see the package and code completion works within the IDE. However, running go get -fix -v appdynamics produces the following error:
warning: no cgo types: exit status 1
warning: no cgo types: exit status 1
I have also tested this using the github.com/appdynamics namespace per the appdynamics go-sdk instructions. Also, I am aware of all the other go build 'cannot find package' questions on S.O.

About warning popup after gopls installation in vscode

I installed gopls using command set GO111MODULE=on and golang.org/x/tools/gopls#latest to use gopls.
After that, I restarted the program and whenever I write and save the source code, a warning window appears like the picture below.
I'm not sure what this warning means.
I am working on GOPATH and have all the packages I need.
But I don't know why i need a module here.
As mentioned in "GOPATH in the VS Code Go extension"
Out of the box, the extension uses the value of the environment variable GOPATH. From Go 1.8 onwards, if no such environment variable is set, then the default GOPATH as deciphered from the command go env is used.
Check if you have set go.gopath or go.inferGopath.
Check what the returned value of go env GOPATH is.
More generally, it is a good idea to initialize a module at the root of your project (wherever your project is, GOPATH or not)
cd /path/to/my/project
go mod init myproject
Some issues mentioned that same error message
You are neither in a module nor in your GOPATH.
Please see https://github.com/golang/go/wiki/Modules for information on how to set up your Go project.
Issue 36120 for instance said:
I believe this is because my GOPATH is a colon-separated string:
GOPATH="/Users/user/go:/Users/user/go-work"
But... that was fixed in CL 211304 and commit 74e303f in gopls v0.3.2.

Error when i running the application hyperledger fabric

I'm following this tutorial:
https://github.com/ITPeople-Blockchain/auction
and at the step: "Build peer and Orderer binaries and Start Orderer",
at the command: make native
I recived following error:
Makefile:71: *** "No go in PATH: Check dependencies". Arresto.
I'm using Ubuntu on VirtualBox and I set the gopath correctly
GOPATH may be right, but that's not what the error refers to - it's saying that the go command is not in your PATH. You need to add GOROOT/bin to your PATH.
go path is not set.
find installed directory of go lang. (e.g linux )
whereis go
then export below var.
export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
these exported variable scope is limited to current terminal only so better to set into file ~/.bashrc
I'm following this tutorial: https://github.com/ITPeople-Blockchain/auction and at the step: "Build peer and Orderer binaries and Start Orderer", at the command: make native
The repository mentioned above has been updated about 2 months ago, while while you'd better try to follow more recently updated: Get Started.
Makefile:71: *** "No go in PATH: Check dependencies". Arresto.
Also note that ^^^ error actually tells you are missing go binary from your PATH. Check you have configured GOPATH, GOROOT and make sure go binary accessible within PATH.

Go get not working in Atom

So I've installed Atom on Windows 7 and I wanted to try out Go language in that. I installed a package called go-plus, but it needed 2 more things to install:
So I clicked Run Go Get, but I get this error:
I tried it on Debian and on Manjaro and after runing Run Go Get it just works. So I thought about running go get -u ... in my cmd, it worked, but still when I open Atom those 2 messages pop out. How can I deal with it?
The go get command downloads and installs packages in your go workspace, as denoted by the GOPATH environment variable. You must set this first for Atom to be able to download utilities.
You may use the go env command to verify, also you can click on the go-plus icon at the bottom of Atom to see the GOPATH value visible to Atom.
Your screenshots and comments indicate you have an invalid folder value in your GOPATH. Easiest is to set it to a single, absolute folder. You can read more about it here:
How to Write Go Code: The GOPATH environment variable

Resources