Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multi /pkg/<module> problem #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wuhuua
Copy link

@wuhuua wuhuua commented Feb 20, 2024

When I try to debug cockroach with VSCode, I found some bugs when I try to set breakpoints.
VSCode tells me that some files cannot be found in dlv sources, even if I use this to substitute the path to package path:

  {
    "from": "${workspaceFolder}",
    "to": "github.com/cockroachdb/cockroach"
  }, 

One of the cases is /pkg/gossip/gossip.go
When using dlv as follows:

dlv exec ../_bazel/bin/pkg/cmd/cockroach/cockroach_/cockroach -- start --insecure --store=node1 --listen-addr=localhost:26257 --http-addr=localhost:8080 --join=localhost:26257,localhost:26258,localhost:26259

and then use sources command to list all sources. You can find its path like this:

github.com/cockroachdb/cockroach/pkg/gossip/pkg/gossip/gossip.go
The relpath pkg/gossip has been appended twice.

This problem is associated with one issue in cockroach:
cockroachdb/cockroach#64379
You modified complilepkg.go as follows:

// We want the source files to show up (e.g. in stack traces) with the package
// path. We use -trimpath to replace the root path with the correct prefix
// of the package path.
root := abs(".")
relSrcPath, err := filepath.Rel(root, srcs.goSrcs[0].filename)
if err != nil {
  return err
}
rootPkgPath := filepath.Clean(strings.TrimSuffix(packagePath, filepath.Dir(relSrcPath)))
gcFlags = append(gcFlags, fmt.Sprintf("-trimpath=%s=>%s", root, rootPkgPath))

But when you debug bazel, you can get the command passed bazel looks like this:

bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/go_sdk/builder_reset/builder compilepkg 
-sdk external/go_sdk -installsuffix linux_amd64 -tags bazel,gss,bazel,gss 
-src bazel-out/k8-dbg/bin/foo/foo_go_proto_/example.com/foo/foo.pb.go 
-src foo/foo.go -embedroot bazel-out/k8-dbg/bin -embedroot '' 
-embedlookupdir foo/foo_go_proto_/example.com/foo -embedlookupdir foo 
(serveral arcs)
-importpath heyhey -p example.com/foo -package_list bazel-out/k8-opt-exec-ST-13d3ddad9198/bin/external/go_sdk/packages.txt -o bazel-out/k8-dbg/bin/foo/foo.a -x bazel-out/k8-dbg/bin/foo/foo.x -gcflags '-N -l' -asmflags '')

The first src is the protobuf file and therefore srcs.goSrcs[0].filename is bazel-out/k8-dbg/bin/foo/foo_go_proto_/example.com/foo/foo.pb.go, but we are expecting the source file which is not generated.

so I modify this part as follows:

srcFullPath := srcs.goSrcs[0].filename
for _, goSrc := range srcs.goSrcs {
  if !strings.HasSuffix(goSrc.filename, ".pb.go") {
    srcFullPath = goSrc.filename
    break
  }
}
relSrcPath, err := filepath.Rel(root, srcFullPath)

Once srcs has source code which is not generated, we use its path.
Then problem is fixed, and we get its path in dlv as follows:
github.com/cockroachdb/cockroach/pkg/gossip/gossip.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant