Skip to main content

Map source paths

The Go compiler writes the absolute path of each source file into the binary, for example /src/internal/checkout/handler.go. Delve reports these paths. Your source files on your machine have other paths, for example /Users/you/code/checkout/internal/checkout/handler.go. The IDE must map one path to the other. If the mapping is wrong, breakpoints are not hit, or they show as unverified.

What DebugHero does​

When a session starts, the server in the debug container reads the binary of your process through /proc/<pid>/root:

  1. It reads all absolute source paths from the DWARF debug information.
  2. It removes the paths of the Go standard library (/usr/local/go) and of the module cache (/go/pkg/mod).
  3. It uses the common root of the other paths as the build directory, for example /src.

debugctl get sessions shows this directory in the PATH column. The API shows it as substitute_binary_path.

VS Code​

The extension maps the first workspace folder to the build directory:

json
"substitutePath": [{"from": "${workspaceFolder}", "to": "/src"}]

So the first workspace folder must be the directory that was /src in the build. Usually this is the root of the Go module, the directory with go.mod.

To map the paths yourself, connect with a launch.json configuration. See the CLI method in Your first debug session. You can add more than one substitutePath entry, for example for a replace directive to a local module.

GoLand​

GoLand maps the remote paths to the files of your project automatically, so the plugin does not set a mapping. Open the root of the Go module as the project. The source code must be the same version as the image.

When the automatic mapping fails​

debugctl get sessions then shows an error in LAST_ERRORS, and the PATH column is empty.

ErrorCauseFix
no source paths found in binary, no DWARF infoThe binary has no debug information.Build without -ldflags="-w" (or -s -w). See Distroless and stripped binaries.
no source paths found in binaryThe binary has no absolute source paths. This is the result of -trimpath.Build without -trimpath. Or map the module path to your workspace yourself, for example {"from": "${workspaceFolder}", "to": "github.com/acme/checkout"}.
could not automatically determine mappingsThe source paths have no useful common root, for example because Go is installed in another directory in the build image.Map the paths yourself. Use go version -m and the Delve log (debugctl logs <session-id>) to find the build paths.

Check your binary​

To see a source path in the binary, run go tool objdump on one function of your code:

shell
go tool objdump -s 'main.main' ./checkout | head -n 3

The first line shows the file path, for example TEXT main.main(SB) /src/cmd/checkout/main.go. The directory before your package path is the build directory.

Use the same source version​

The mapping connects paths, not file contents. If your local files differ from the code in the image, the IDE stops at the wrong lines. Check out the commit that was built into the image before you debug.