10

I have my main.go in a subfolder cmd/admin/main.go but when I'm debugging and there are errors in a file, it gives me the relative path from the main.go folder instead of the workspace folder. So for example I will have the error ..\..\path\to\file.go:238:3: undefined: test which won't work if I try to Ctrl+click it.
If I launch the command from the root go run cmd/admin/main.go that works as intended returning path\to\file.go:238:3: undefined: test.

My launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "cwd": "${workspaceFolder}",
      "program": "${workspaceFolder}/cmd/admin",
      "env": {},
      "args": []
    }
  ]
}

Go version 1.16.6
VSC version 1.58.2
OS Windows10

2
  • Is it Go 1.6.6 or Go 1.16.6? Does your project have go.mod/go.sum files? Commented Jul 19, 2021 at 20:20
  • Sorry Go 1.16.6, both go.mod and go.sum present Commented Jul 20, 2021 at 16:14

2 Answers 2

9
+200

Go File > Add Folder to Workspace Then select the folders containing main.go enter image description here You can also do it in the command line:

code cmd/admin -a

Now make sure your current launch.json has been deleted to start fresh, your workspace should look like this:

enter image description here

Notice that there is a 'package main' and 'func main()' this is required for Go to know the entry point.

Now press Run and Debug with a breakpoint:

enter image description here

That's it, it should now work on any folders you add to your workspace. If you want more specific debug options, add them to your workspace and they'll apply in the context of the file you run from. Click the 'create a launch.json file.:

enter image description here

Select workspace:

enter image description here

Select Go: Launch package

enter image description here

You now have a launch config that will apply to the directory you run it from:

enter image description here

Make sure to save your workspace to keep it:

enter image description here

Notes

  • Be sure to delete your current launch.json files if they already exist anywhere.

  • Make sure all of your source code is located in GOPATH/src, you can find out where GOPATH is by putting this into a command line:

go env GOPATH

More info

VS Code workspace debugging: https://code.visualstudio.com/docs/editor/multi-root-workspaces#_debugging

GOPATH: https://golang.org/doc/gopath_code

Sign up to request clarification or add additional context in comments.

4 Comments

Trying that I just get different path errors when I click on the the link, the base launch.json will give me c:\Users\shado\Projects\path\to\file.go while the new workspace launch.json will give me c:\Users\shado\Projects\PROJECT-X\cmd\path\to\file.go the correct path should be c:\Users\shado\Projects\PROJECT-X\path\to\file.go without the cmd. I guess that's progress.
@Shadoweb In a command prompt put in "go env GOPATH" This shows you were all your source code should go to work with Go tooling like modules. Looks like you're on Windows, so should put all your Go source code in here: c:\Users\shado\go\src\PROJECT-X\ Then delete your current launch.json, add the two subfolders to workspace, and add the debug configurations. Should work. I'll update answer to include this info.
Actually by replacing TEST with panic("TEST") it does work. I suppose I was trying to solve a typo issue instead of a real issue, which would almost never happen anyway... I suppose I can live with that. Thanks for your help.
@Shadoweb not a problem happy you got it working.
1

Try and make sure:

  • you have no GOxxx environment variable set (no GO_BASE_PATH, no GOROOT), except for GOPATH set to %USERPROFILE%\go, and %GOPATH%\bin in your %PATH%
  • you are using Go installed in the default C:\Program Files\Go\ folder
  • you have set up your project using Go modules, with a go mod init myproject
  • you have defined a multi-root workspace for your project root folder, compose of only one root: your project. Save that workspace (it will create a <name>.code-workspace JSON file).

See then if the issue persists (and no cwd should be needed in your launch.json)

2 Comments

I have the folder in C:\dev\Go, but does that really matter? I created the project a long time ago with go mod init but made updates using VSC. I tried the multi-root workspace but it still fails on the Ctrl+click, it will only move up 1 folder instead of 2 and stops at ${workspaceFolder}/cmd. I suppose that's some progress.
@Shadoweb Not sure: I am just describing my setup, where delve works perfectly (make sure to update your Go tools, by the ways)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.