debugging go application doesn't stop at breakpoints - debugging

I have latest IntelliJ Ultimate EAP and latest Gogland and the problem occurs on both under MacOS Sierra.
I'm trying to debug a go application by creating a debug profile for Go Application and the Run Kind is configured to Package, which contains the package name of the current project.
under Go tool arguments I have -ldflags="-linkmode internal" configured.
When I start a debug session while having some breakpoints, it would run but not stop at these breakpoints.
what am I missing?
thanks
update
ok I was able to reproduce the issue with the following project:
test1.go contains:
package main
const Numb uint64 = 5
test2.go contains:
package main
import "fmt"
func main() {
fmt.Println(Numb);
}
I Created a Go Application run/debug configuration with the following parameters:
Run kind: package
Package: github.com/kfirufk/test
Working Directory: /usr/local/Cellar/go/1.8.1/src/github.com/kfirufk/test/
when I choose a breakpoint on test2.go line 6 (the first and only line of code in the main function) and I start a debug session, I see the following output:
"/Users/ufk/Library/Application Support/IntelliJIdea2017.1/Go/lib/dlv/mac/dlv" --listen=localhost:53111 --headless=true exec /private/var/folders/cn/n7rwdd_95_l54s3zdnbxvw040000gn/T/Unnamedgo --
GOROOT=/usr/local/Cellar/go/1.8.1/libexec
GOPATH=/usr/local/opt/go
/usr/local/Cellar/go/1.8.1/libexec/bin/go build -o /private/var/folders/cn/n7rwdd_95_l54s3zdnbxvw040000gn/T/Unnamedgo -gcflags "-N -l" github.com/kfirufk/test
API server listening at: 127.0.0.1:53111
5
I get the program's output properly but Intellij did not stop in the required breakpoint.
the problem is reproduced on Intellij 2017.1.3 with Go Lang Plugin 0.171.1928 on MacOS Sierra 10.12.4.
update
trying to play with intellij's delve to try to understand better what's going on:
/Users/ufk/Library/Application\ Support/IntelliJIdea2017.1/intellij-go/lib/dlv/mac/dlv exec ./test
then I executed:
(dlv) step
and received:
Command failed: could not find FDE for PC 0x78bc000
did I fail to understand how to use delve or is something doesn't work properly here ?
update
yeap.. with continue the debugger works properly with both versions of delve (installed from homebrew and the intellij's version). but still intellij works the same, doesn't stop at breakpoints. i create a breakpoint at test2.go at the line when I print the variable.
any ideas ?

Ok.. I finally got it. I got mixed up with the GOROOT structure and GOPATH structure and I placed my project in GOROOT instead of GOPATH.
this is what I did to resolve the issue
I completely deleted go with brew uninstall --force go, then reinstalled go with the following environment variables:
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
then I placed my project at ~/golang/src/github.com/kfirufk/windy-server
and I was finally able to properly debug go applications.
thank you all for assisting me! :)

For my case (VSCode on Ubuntu 20.04), if the project is inside a symbolic link directory (or sub-dir of a symbolic dir), vscode will shows "unverified" breakpoints and
"Error on CreateBreakpoint: could not find file ..."
which leads to not stop on breakpoints.
cd \`readlink -f <project dir>\` && code .
will be ok.

For me, issue resolved adding:
export GOROOT="/usr/local/go"
to $HOME/.profile.
EDIT:
I'm using LiteIde X35.2, with Delve Debugger
Version: 1.1.0
Build: $Id: 1990ba12450cab9425a2ae62e6ab988725023d5c $
under:
DISTRIB=LinuxMint VERSION=18.3 CODENAME=sylvia
RELEASE=#41~16.04.1-Ubuntu SMP Wed Oct 10 20:16:04 UTC 2018
UBUNTU_CODENAME=xenial KERNEL=4.15.0-38-generic HDWPLATFORM=x86_64
DESKTOP_SESSION=XFCE WINDOWS_MANAGER=XFWM4
DESKTOP_COMPONENTS=kdevtmpfs gnome-keyring-d xfce4-session xfce4-panel
xfce4-volumed polkit-gnome-au xfce4-power-man xfce4-terminal
gnome-pty-helpe
GTK=libgtk-3-0:amd64 3.18.9-1ubuntu3.3 GStreamer=gst-launch-1.0
version 1.8.3 GStreamer 1.8.3
https://launchpad.net/distros/ubuntu/+source/gstreamer1.0

In my case, the process was already running at that port and didn't close. So I had to manually stop the process and run again from IntelliJ.

In my case (GoLand), the problem was with the flag -trimpath not being set in my debug configurations:
"Run", "Debug...", "Edit Configurations."
In the templates item, go to "Go Test" option and add the argument -trimpath in "Go tool arguments"
You can also set in your environment with:
go env -w GOFLAGS="-trimpath"
That also works for the "attach to process" debugging option.
You can see GoLand IDE Logs in the menu "Help", "Show Log Files". It will make your life easier.
Reference: https://youtrack.jetbrains.com/issue/GO-8277

Related

Unable to Debug in GoLand IDE : could not launch process: can not run under Rosetta

Hi New here to Go Language.
I have indtalled go and have downloaded GoLand. I was able to run the code successfully. However when i try to debug the code its showing the following error
could not launch process: can not run under Rosetta, check that the
installed build of Go is right for your CPU architecture
My Go Version is : go version go1.17.8 darwin/arm64
Device details :
What should i do to resolve this issue?
# build new dlv
git clone https://github.com/go-delve/delve.git
cd delve/cmd/dlv
go build
go install
cd $GOPATH/bin
mkdir macarm
cp ./dlv ./macarm/
### config goland ide
menu: help -> edit Coustom Properties
input : dlv.path=$GOPATH/bin

GoLand IDE not correctly compiling. How to setup correct paths?

I am trying to setup GoLand correctly to be able to use it with Go.
I'm trying to run the follow simple HelloWorld go project.
package HelloWorldProject
import "fmt"
func main(){
fmt.Printf("Hello")
fmt.Printf("1+1 = ", 1+1)
}
This is my console's results:
GOROOT=/usr/local/Cellar/go/1.10/libexec #gosetup
GOPATH=/Users/jeanmac/go #gosetup
/usr/local/Cellar/go/1.10/libexec/bin/go build -i -o /private/var/folders/r5/rfwd1cqd4kv8cmh5gh_qxpvm0000gn/T/___Hello /Users/jeanmac/go/src/github.com/jpere547/HelloWorldProject/Hello.go #gosetup
Compilation finished with exit code 0
I am on Mac OS and I installed Go using Brew.
Results of brew info go:
go: stable 1.10 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.10 (8,150 files, 336.9MB) *
Poured from bottle on 2018-03-22 at 19:38:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.8 ✔
==> Options
--without-cgo
Build without cgo (also disables race detector)
--without-race
Build without race detector
--HEAD
Install HEAD version
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
The GoLand configurations is below:
GOROOT
GOPATH
It looks like you are trying to run a non main package. Specifically, instead of package HelloWorldProject, you should use package main. After that the IDE will be able to not only build but also run the package.
When you do go build something.go or go isntall something.go that only builds/installs the go package to give you an executable. You will need to run that executable.
The easiest way to run simple Golang programs is simply use go run something.go that will run your go file.
As long as your GOPATH is correctly set it should work
you can try to change the package name instead of main
There is an option in configuration to run after build. I think you are missing to set it's configuration.Steps:
1. Go to Run
2. Select "Edit configuration"
3. Select the check-box below output directory field specifying "Run after build".

GoType issues with SublimeLinter

I'm a bit new to Go, and I've been trying to use the SublimeLinter-contrib-gotype package to lint my files. It works well for the most part, but for some reason it throws an error if I try to import a Go package from GitHub. I've been trying to use the simple example from the Echo framework. When I run the code, it works fine but for some reason the linter is causing issues.
It throws an error saying:
could not import github.com/labstack/echo (can't find import: )
I've already run the go get github.com/labstack/echo command, but it doesn't seem to have helped.
I've attached a screenshot as well:
And here is a link to the code I was using.
I'd faced the same problem. Try running
launchctl setenv GOPATH $GOPATH
and restarting ST, this won't work after a reboot.
Source
If you use Go Sublime, the following should work better.
Sublime Text menu > Preferences > Package Settings > GoSublime > Settings - Default / User
"shell": ["/usr/bin/bash"],
"env": {"GOPATH": "/Users/username/gopath/"},

gocode external package give no suggestion for vscode

My auto-complete / intellisense stopped working after upgrading from the Go extension here of Visual Studio Code: https://github.com/microsoft/vscode-go
I've opened an issue in there, but I'm thinking this might have to do with gocode.
I'm running Windows 10, Go version 1.5.1, Visual Studio Code 0.10.5 and Go extension 0.6.21. Was working fine before updating and I was using Go extension 0.6.17.
Problem is suggestions / intellisense works for internal package, but every external package I have no longer give suggestions when writing . or ctrl+space. Even net/http or core Go package does not work.
I wanted to try and run gocode myself to see what it's actually returning, so I've looked into the code for the extension and it is executing a child process similar to that: gocode -f=json autocomplete filename offset.
Not really sure how to get offset or what it represents, number of bytes for position that triggers the auto-complete? In any case, when I run this I got no output and I do not return to the prompt, I have to CTRL+C to return.
I'm unable to find anything interesting on Google either, probably wrongly searching or not exactly knowing what to search for.
Does someone would have an idea, goal of running gocode was to see if in fact the gocode was returning suggestion for external package than if yes, there's something with the extension.
If gocode does not returns anything, well it means it's not working properly now, anything I can do?
Thanks
Edit 1: Log from gocode following kostya answer
2015/12/23 07:26:11 Import path "github.com/gocraft/web" was not resolved
2015/12/23 07:26:11 Gocode's build context is:
2015/12/23 07:26:11 GOROOT: c:\go
2015/12/23 07:26:11 GOPATH:
2015/12/23 07:26:11 GOOS: windows
2015/12/23 07:26:11 GOARCH: amd64
2015/12/23 07:26:11 GBProjectRoot: ""
2015/12/23 07:26:11 lib-path: ""
It seems my GOPATH variable is not evaluated, but it's set, and when I run echo %GOPATH% I receive the correct value.
Try running gocode server process in the console and observe the output:
gocode close
gocode -debug -s
Make sure that you are running the latest version of gocode:
go get -u github.com/mdempsky/gocode
You might want to run the following command to build gocode instead (though I believe you won't be able to use debug method that I suggested in this case):
go get -u -ldflags -H=windowsgui github.com/mdempsky/gocode
as suggested at https://github.com/mdempsky/gocode

Go: "stat hello.go: no such file or directory"

Just installed Go on Mac OS X, Yosemite Version 10.10.3, as explained in the Getting Started page of the official website:
Mac OS X package installer
Download the package file, open it, and follow the prompts to install
the Go tools. The package installs the Go distribution to
/usr/local/go.
The package should put the /usr/local/go/bin directory in your PATH
environment variable. You may need to restart any open Terminal
sessions for the change to take effect.
I am now in the Test your installation section, which states:
Check that Go is installed correctly by building a simple program, as
follows.
Create a file named hello.go and put the following program in it:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n") }
Then run it with the go tool:
$ go run hello.go
hello, world
If you see the "hello, world" message then your Go installation is
working.
So, I created a hello.go file and, since I could not (ie: did not know how to) access the /usr/local/go/bin directory, I saved it on my desktop.
Obviously, I got the following error message:
stat hello.go: no such file or directory
So, where should I save my Go files to be able to run them?
UPDATE: after some research, I stumbled upond this video, explaining how to set the GOPATH.
If I want my Go workspace to be in user/code/go, how should I write my export GOPATH= command?
if your are using docker compose up/down to build image. You need to run
docker compose build
after changes in Dockerfile.
In my case, it was as simple as right clicking the file, and then clicking 'open in integrated terminal.' Then, to run your file type 'go run filename.go'
The GOPATH and PATH environment variables
The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory ($HOME/go).
If you really want to change your GOPATH to something else add GOPATH to your shell/bash/zsh initialization file .bash_profile, .bashrc or .zshrc.
export GOPATH=/something-else
Add GOPATH/bin directory to your PATH environment variable so you can run Go programs anywhere.
export PATH=$PATH:$(go env GOPATH)/bin
Following the explanation of Andrew Gerrand in Writing, building, installing, and testing Go code, I configured my Go workspace as follows:
1. Create workspace
First, since I want my Go code to be in my_user_name/code/go, I started by creating the corresponding folder:
$ cd code
$ mkdir go
2. Setup GOPATH
Then I configured the GOPATH:
$ cd
$ export GOPATH=$HOME/code/go
$ cd code/go
3. Add files to workspace
Last, I manually (through the Finder) moved my hello.go file into the workspace and ran:
$ go run hello.go
Worked like a charm.

Resources