bazel-gazelle icon indicating copy to clipboard operation
bazel-gazelle copied to clipboard

Imports of headers not handled inside inline assembly files (.s or .h)

Open esprehn opened this issue 2 years ago • 5 comments

What version of gazelle are you using?

v0.26.0

What version of rules_go are you using?

v0.35.0

What version of Bazel are you using?

5.1.10

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

yes.

What operating system and processor architecture are you using?

Mac OS M1 and Linux Intel.

What did you do?

We're trying to use github.com/go-git/go-git/v5

which depends on github.com/cloudflare/circl

which contains a number of inline assembly files

which contain relative imports of includes across package boundaries, for example:

https://github.com/cloudflare/circl/blob/c56c51d2526f71be07939782d59d1a98744e062c/dh/x448/curve_amd64.s#L6

#include "../../math/fp448/fp_amd64.h"

which fails the build because the headers cannot be found at build time.

What did you expect to see?

Build passes.

What did you see instead?

gazelle does not appear to handle import statements in header files for assembly, which breaks popular libraries like the go-git library.

Gazelle should probably generate go_source() targets inside packages that contain .h files, ex. here:

https://github.com/cloudflare/circl/tree/main/math/fp448

and gazelle should also take a dependency on other packages when a cross project important statement appears in a .h or .s file.

In particular the dependency parser here:

https://github.com/bazelbuild/bazel-gazelle/blob/ec1c643571ece7bb5cac359b96c17b3641638c5d/language/go/fileinfo.go

should parse .s files and resolve relative import statements to absolute paths within the repo. And then produce cross project deps based on those just like import statements in .go code.

gazelle should also produce go_source() rules whenever there's a .h file within a package.

We fixed this in our repo with a series of patches like below, but ideally this would be generated by gazelle itself.

patches/com_github_cloudflare_circl/math_fp25519_BUILD_bazel.patch

--- math/fp25519/BUILD.bazel	2000-01-01 00:00:00.000000000 -0000
+++ math/fp25519/BUILD.bazel	2000-01-01 00:00:00.000000000 -0000
@@ -1,4 +1,4 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_source", "go_test")

 go_library(
     name = "fp25519",
@@ -22,6 +22,14 @@
     }),
 )

+go_source(
+    name = "headers",
+    srcs = [
+        "fp_amd64.h",
+    ],
+    visibility = ["//visibility:public"],
+)
+
 alias(
     name = "go_default_library",
     actual = ":fp25519",
@@ -37,3 +45,4 @@
         "//internal/test",
     ],
 )
+

patches/com_github_cloudflare_circl/source/dh_x25519_BUILD_bazel.new

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

go_library(
    name = "x25519",
    srcs = [
        "curve.go",
        "curve_amd64.go",
        "curve_amd64.h",
        "curve_amd64.s",
        "curve_generic.go",
        "curve_noasm.go",
        "doc.go",
        "key.go",
        "table.go",
    ],
    embed = [
        "//math/fp25519:headers",
    ],
    importpath = "github.com/cloudflare/circl/dh/x25519",
    visibility = ["//visibility:public"],
    deps = [
        "//math/fp25519",
    ] + select({
        "@io_bazel_rules_go//go/platform:amd64": [
            "@org_golang_x_sys//cpu",
        ],
        "//conditions:default": [],
    }),
)

cc/ @skevy @indygreg

esprehn avatar Dec 12 '22 23:12 esprehn

I'm seeing the same error with go-git. @esprehn were you able to find a solution?

patnaikshekhar avatar Jan 04 '23 14:01 patnaikshekhar

Same issue here

malt3 avatar Feb 10 '23 14:02 malt3

same here

Tachone avatar Apr 25 '23 09:04 Tachone

I'm having the same issue on the latest version v0.30.0. My workaround, for now, is to patch in the changes to the BUILD.bazel files. Don't know if I have the best solution for it coping the directory e.g external/com_github_cloudflare_circl/ into a temp folder creating a git repo. Then making my changes and checking in the patch into my repo with git diff > temp.patch but it unblocked me.

charlesoconor avatar May 15 '23 23:05 charlesoconor

FYI: if you are using bzlmod there is another workaround described in this comment on https://github.com/bazelbuild/rules_go/issues/3799

klandergren avatar Mar 27 '24 02:03 klandergren