bazelbuild / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.16k stars 369 forks source link

dlv 1.7.3 and onwards are incompatible with Gazelle due to new CGo dep #1171

Open JamyDev opened 2 years ago

JamyDev commented 2 years ago

What version of gazelle are you using?

6bbfc47f1b0a27ee1efeddcc6671f3e4e03235dc

What version of rules_go are you using?

v0.30.0

What version of Bazel are you using?

4.2.2

Does this issue reproduce with the latest releases of all the above?

Likely

What operating system and processor architecture are you using?

MacOS 12.2

What did you do?

Installing github.com/go-delve/delve@v1.7.3 (and later) and then running Gazelle to generate the build files for the dependency, gazelle will fail to include bpf/include/function_vals.bpf.h in the srcs for github.com/go-delve/delve/pkg/proc/internal/ebpf

The h file is referenced as a # include in helpers.go of that same package, so it should be possible to reference that for an auto import.

What did you expect to see?

BUILD.bazel for bpf package srcs include the necessary .h file

What did you see instead?

▶ bazel build @com_github_go_delve_delve//cmd/dlv
INFO: Invocation ID: dd7143a4-9c1d-4f8e-8fab-e8e64587ce49
INFO: Analyzed target @com_github_go_delve_delve//cmd/dlv:dlv (44 packages loaded, 421 targets configured).
INFO: Found 1 target...
ERROR: /home/user/.cache/bazel/_bazel_user/b97476d719d716accead0f2d5b93104f/external/com_github_go_delve_delve/pkg/proc/internal/ebpf/BUILD.bazel:3:11: GoCompilePkg external/com_github_go_delve_delve/pkg/proc/internal/ebpf/ebpf.a failed: (Exit 1): builder failed: error executing command 
  (cd /home/user/.cache/bazel/_bazel_user/b97476d719d716accead0f2d5b93104f/sandbox/processwrapper-sandbox/1148/execroot/__main__ && \
  exec env - \
    CC=/usr/bin/gcc \
    CGO_ENABLED=1 \
    GOARCH=amd64 \
    GOOS=linux \
    GOPATH='' \
    GOROOT=external/go_sdk \
    GOROOT_FINAL=GOROOT \
    PATH=/usr/bin:/bin \
  bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk [...]
Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox
__main__/external/com_github_go_delve_delve/pkg/proc/internal/ebpf/helpers.go:6:11: fatal error: ./bpf/include/function_vals.bpf.h: No such file or directory
compilation terminated.
compilepkg: error running subcommand external/go_sdk/pkg/tool/linux_amd64/cgo: exit status 2
Target @com_github_go_delve_delve//cmd/dlv:dlv failed to build
INFO: Elapsed time: 19.540s, Critical Path: 0.08s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

BUILD file for this dir:

load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
    name = "ebpf",
    srcs = [
        "context.go",
        "helpers.go",
        "helpers_disabled.go",
        "trace_bpfel_x86.go",
    ],
    cgo = True,
    embedsrcs = select({
        "@io_bazel_rules_go//go/platform:386": [
            "trace_bpfel_x86.o",
        ],
        "@io_bazel_rules_go//go/platform:amd64": [
            "trace_bpfel_x86.o",
        ],
        "//conditions:default": [],
    }),
    importpath = "github.com/go-delve/delve/pkg/proc/internal/ebpf",
    visibility = ["//pkg/proc:__subpackages__"],
    deps = [
        "//pkg/dwarf/godwarf",
        "//pkg/dwarf/op",
    ] + select({
        "@io_bazel_rules_go//go/platform:android_386": [
            "@com_github_cilium_ebpf//:go_default_library",
        ],
        [... stripped for brevity]
        "@io_bazel_rules_go//go/platform:windows_amd64": [
            "@com_github_cilium_ebpf//:go_default_library",
        ],
        "//conditions:default": [],
    }),
)

alias(
    name = "go_default_library",
    actual = ":ebpf",
    visibility = ["//pkg/proc:__subpackages__"],
)

Current workaround:

The following patch added to out @com_github_go_delve_delve repository (-p1)

diff -urN b/pkg/proc/internal/ebpf/BUILD.bazel c/pkg/proc/internal/ebpf/BUILD.bazel
--- b/pkg/proc/internal/ebpf/BUILD.bazel  2022-01-14 23:29:02.948089761 +0000
+++ c/pkg/proc/internal/ebpf/BUILD.bazel  2022-01-14 23:13:52.964810803 +0000
@@ -7,6 +7,7 @@
         "helpers.go",
         "helpers_disabled.go",
         "trace_bpfel_x86.go",
+        "bpf/include/function_vals.bpf.h",
     ],
     cgo = True,
     embedsrcs = select({
ouchuji commented 2 years ago

Problem +1 github.com/go-vgo/robotgo/key v1.0.0-beta5

FastNav commented 2 years ago

Problem +1. Even with @JamyDev's patch I get an error

compilepkg: /repodir/external/com_github_go_delve_delve/pkg/proc/internal/ebpf/trace_bpfel_x86.go:125:12: could not embed trace_bpfel_x86.o: no matching files found
Target @com_github_go_delve_delve//pkg/proc/native:native failed to build
FastNav commented 2 years ago

From my understanding, it seems like Gazelle's official strategy here is to either manually add BUILD files or patch the resulting BUILD files to accept a cc_library rule containing the header and object. Though the easiest thing would be if Delve added Bazel support.

JamyDev commented 2 years ago

@FastNav did you get it figured out, was there a different patch needed for later dlv versions? If so, please share for future folks that run into this.

pjjw commented 1 year ago

for the record, you can also just knock out the ebpf code and force the fallback, as used on all platforms that arent x86_64 linux- here's the patch i apply in delve's go_repository entry

diff --git a/pkg/proc/internal/ebpf/helpers.go b/pkg/proc/internal/ebpf/helpers.go
index d9b5e7df..9c0039bb 100644
--- a/pkg/proc/internal/ebpf/helpers.go
+++ b/pkg/proc/internal/ebpf/helpers.go
@@ -1,5 +1,5 @@
-//go:build linux && amd64 && go1.16
-// +build linux,amd64,go1.16
+//go:build dummy
+// +build dummy

 package ebpf

diff --git a/pkg/proc/internal/ebpf/helpers_disabled.go b/pkg/proc/internal/ebpf/helpers_disabled.go
index 66d18c52..bdb88e69 100644
--- a/pkg/proc/internal/ebpf/helpers_disabled.go
+++ b/pkg/proc/internal/ebpf/helpers_disabled.go
@@ -1,6 +1,3 @@
-//go:build !linux || !amd64 || !go1.16
-// +build !linux !amd64 !go1.16
-
 package ebpf

 import (