golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.84k stars 735 forks source link

Can't set breakpoints to run in debug mode (macOS) #3081

Closed Kuo-Chun-Ting closed 9 months ago

Kuo-Chun-Ting commented 9 months ago

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
* Run `go version` to get version of Go from _the VS Code integrated terminal_. - go1.19 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - golang.org/x/tools/gopls v0.14.2 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - Version: 1.84.2 * Check your installed extensions to get the version of the VS Code Go extension - 0.40.0 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. ``` Checking configured tools.... GOBIN: undefined toolsGopath: gopath: /Users/lillard/go GOROOT: /Users/lillard/.gvm/gos/go1.19 PATH: /Users/lillard/.gvm/gos/go1.19/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/Users/lillard/.gvm/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/go/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands PATH (vscode launched with): /Library/Frameworks/Python.framework/Versions/3.11/bin:/Users/lillard/.gvm/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/go/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands go: /Users/lillard/.gvm/gos/go1.19/bin/go: go version go1.19 darwin/amd64 gopls: /Users/lillard/go/bin/gopls (version: v0.14.2 built with go: go1.19) gotests: /Users/lillard/go/bin/gotests (version: v1.6.0 built with go: go1.19) gomodifytags: /Users/lillard/go/bin/gomodifytags (version: v1.16.0 built with go: go1.19) impl: /Users/lillard/go/bin/impl (version: v1.1.0 built with go: go1.19) goplay: /Users/lillard/go/bin/goplay (version: v1.0.0 built with go: go1.19) dlv: /Users/lillard/go/bin/dlv (version: v1.21.2 built with go: go1.21.4) staticcheck: /Users/lillard/go/bin/staticcheck (version: v0.4.6 built with go: go1.19) go env Workspace Folder (test_break_points): /Users/lillard/coding/golang/test_break_points GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/lillard/Library/Caches/go-build" GOENV="/Users/lillard/Library/Application Support/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/lillard/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/lillard/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/Users/lillard/.gvm/gos/go1.19" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/Users/lillard/.gvm/gos/go1.19/pkg/tool/darwin_amd64" GOVCS="" GOVERSION="go1.19" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/lillard/coding/golang/test_break_points/go.mod" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5t/60zp_j115mbcb05zt8w_7v700000gn/T/go-build931582716=/tmp/go-build -gno-record-gcc-switches -fno-common" ```

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file. Share all the settings with the go. or ["go"] or gopls prefixes.

I don't set any variable in the workspace settings.json. Here's the User settings.json

{
    "extensions.autoUpdate": "onlyEnabledExtensions",
    "redhat.telemetry.enabled": true,
    "vscode-spring-boot.rewrite.reconcile-prompt": false,
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter"
    },
    "go.goroot": "/Users/lillard/.gvm/gos/go1.19",
    "git.openRepositoryInParentFolders": "never",
    "editor.inlineSuggest.enabled": true,
    "black-formatter.args": [
        "--line-length, 120"
    ],
    "editor.defaultFormatter": "ms-python.black-formatter",
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[go]": {
        "editor.defaultFormatter": "golang.go"
    },
    "[yaml]": {
        "editor.defaultFormatter": "redhat.vscode-yaml"
    },
    "pylint.showNotifications": "onWarning",
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "window.zoomLevel": 2,
}

Describe the bug

I can run code in debug mode but can't set the breakpoints.

I would expect the code will stop at my break point normally without any errors.

Steps to reproduce the behavior:

  1. Create a folder with <your_module_name>
  2. Move to the folder and create a main.go file
  3. Copy and paste the code below
    
    package main

import "fmt"

func main() { a := 1 fmt.Println(a) }

5. Run the command to generate go.mod `go mod init <your_module_name>`
6. Create the launch file with the following default values

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Launch Package", "type": "go", "request": "launch", "mode": "auto", "program": "${fileDirname}" } ] }


8. Go to the main.go file and press F5 to run debug mode

### Screenshots or recordings
Here are two simple examples for both cases that I run the code with and without breakpoints

When running without breakpoints, the result is like below
![image](https://github.com/golang/vscode-go/assets/41945189/3ea1a3f8-4a17-4fc5-aa8e-e5de166d4806)

When running with breakpoints, I'll get the following errors
![image](https://github.com/golang/vscode-go/assets/41945189/f4d0b480-b16d-41eb-932e-7a2514e8c518)
hyangah commented 9 months ago

Thanks for the report. @Kuo-Chun-Ting Can you please check if you can use dlv command line tool and set the breakpoint?

$ /Users/lillard/go/bin/dlv debug .
(dlv) b main.go:7
(dlv) c

If it fails, I think this is a duplicate of https://github.com/go-delve/delve/issues/3519 and it requires reinstalling xcode. Can you try to reinstall xcode and see if that helps?

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
Kuo-Chun-Ting commented 9 months ago

@hyangah Thank you for the quick response. The steps you provided worked. Thanks again.

hyangah commented 9 months ago

Good to hear that that fixed the issue. Happy coding!

Closing.