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

Gazelle does not support unix build tag (//go:build unix) #1465

Closed
tempoz opened this issue Feb 27, 2023 · 2 comments · Fixed by #1512
Closed

Gazelle does not support unix build tag (//go:build unix) #1465

tempoz opened this issue Feb 27, 2023 · 2 comments · Fixed by #1512

Comments

@tempoz
Copy link

tempoz commented Feb 27, 2023

What version of gazelle are you using?

v0.29.0

What version of rules_go are you using?

v0.38.1

What version of Bazel are you using?

v6.0.0

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

Yes.

What operating system and processor architecture are you using?

Debian 11 amd64

What did you do?

I initially tried to depend on github.com/jackc/pgx/stdlib in a bazel project that uses gazelle.

However, that's annoying to reproduce minimally and cleanly, so the error can be minimally reproduced as follows:
On a unix machine, use git apply to apply this patch to an empty workspace (empty directory on which git init has been run):

diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..a27a8fc
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,4 @@
+load("@bazel_gazelle//:def.bzl", "gazelle")
+
+# gazelle:prefix github.com/example/project
+gazelle(name = "gazelle")
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..42ee6b8
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,35 @@
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+http_archive(
+    name = "io_bazel_rules_go",
+    sha256 = "dd926a88a564a9246713a9c00b35315f54cbd46b31a26d5d8fb264c07045f05d",
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip",
+        "https://github.com/bazelbuild/rules_go/releases/download/v0.38.1/rules_go-v0.38.1.zip",
+    ],
+)
+
+http_archive(
+    name = "bazel_gazelle",
+    sha256 = "ecba0f04f96b4960a5b250c8e8eeec42281035970aa8852dda73098274d14a1d",
+    urls = [
+        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
+        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
+    ],
+)
+
+
+load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
+load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
+
+############################################################
+# Define your own dependencies here using go_repository.
+# Else, dependencies declared by rules_go/gazelle will be used.
+# The first declaration of an external repository "wins".
+############################################################
+
+go_rules_dependencies()
+
+go_register_toolchains(version = "1.20.1")
+
+gazelle_dependencies()
diff --git a/foo/foo.go b/foo/foo.go
new file mode 100644
index 0000000..9d41b09
--- /dev/null
+++ b/foo/foo.go
@@ -0,0 +1,3 @@
+package foo
+
+var _ = &Foo{}
diff --git a/foo/no.go b/foo/no.go
new file mode 100644
index 0000000..486262e
--- /dev/null
+++ b/foo/no.go
@@ -0,0 +1,5 @@
+//go:build !unix
+
+package foo
+
+type Foo struct{}
diff --git a/foo/yes.go b/foo/yes.go
new file mode 100644
index 0000000..a76d81e
--- /dev/null
+++ b/foo/yes.go
@@ -0,0 +1,5 @@
+//go:build unix
+
+package foo
+
+type Foo struct{}

Then, in that directory, run bazel run //:gazelle followed by bazel build //foo:foo.

What did you expect to see?

I expected to see, in the foo/BUILD.bazel file:

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

go_library(
    name = "foo",
    srcs = [
        "foo.go",
        "yes.go",
    ],
    importpath = "github.com/example/project/foo",
    visibility = ["//visibility:public"],
)

with the relevant part being the yes.go file (which has the //go:build unix line) included in the srcs list, as, per https://pkg.go.dev/cmd/go@go1.20.1#hdr-Build_constraints, the unix build constraint should be satisfied "if GOOS is a Unix or Unix-like system", where the list of those systems can be found here:

https://cs.opensource.google/go/go/+/refs/tags/go1.20.1:src/go/build/syslist.go;l=34-51 (Lines reproduced below for viewing convenience)

// unixOS is the set of GOOS values matched by the "unix" build tag.
// This is not used for filename matching.
// This list also appears in cmd/dist/build.go and
// cmd/go/internal/imports/build.go.
var unixOS = map[string]bool{
	"aix":       true,
	"android":   true,
	"darwin":    true,
	"dragonfly": true,
	"freebsd":   true,
	"hurd":      true,
	"illumos":   true,
	"ios":       true,
	"linux":     true,
	"netbsd":    true,
	"openbsd":   true,
	"solaris":   true,
}

When I run go env on my machine, I get GOOS="linux", so I should satisfy that build constraint.

I also expect that the build will then complete successfully.

What did you see instead?

Instead, my foo/BUILD.bazel file is the following:

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

go_library(
    name = "foo",
    srcs = [
        "foo.go",
        "no.go",
    ],
    importpath = "github.com/example/project/foo",
    visibility = ["//visibility:public"],
)

with the relevant part being the no.go file (which has the //go:build !unix line) included in the srcs list instead of the yes.go file. Further, the build fails with the following error:

foo/foo.go:3:10: undefined: Foo
compilepkg: error running subcommand external/go_sdk/pkg/tool/linux_amd64/compile: exit status 2
Target //foo:foo failed to build

because when go actually builds those files, the !unix build directive is not satisfied, so the no.go file is not built, and the Foo type is never defined. Note that if I change the "no.go", line to "yes.go",, the build succeeds.

Edit: After testing with the tag linux instead of unix, I see that both files are included in srcs. That would also be an acceptable outcome; I do not need no.go excluded, I just need yes.go included.

btwiuse pushed a commit to btwiuse/k0s that referenced this issue Mar 1, 2023
Note: make bazel-build is broken for now because of

bazel-contrib/bazel-gazelle#1465
@btwiuse
Copy link

btwiuse commented Mar 1, 2023

I'm also having this problem.

For now I can only use !windows as a rough replacement

btwiuse pushed a commit to btwiuse/k0s that referenced this issue Mar 1, 2023
Note: make bazel-build is broken for now because of

bazel-contrib/bazel-gazelle#1465
@tempoz
Copy link
Author

tempoz commented Mar 4, 2023

For people having this problem on dependencies included via go_repository: if you know you will only build on unix machines, you can add "gazelle:build_tags unix" to the build_directives in that go_repository target as a temporary hack until this issue is fixed.

connyay added a commit to connyay/bazel-gazelle that referenced this issue Apr 19, 2023
connyay added a commit to connyay/bazel-gazelle that referenced this issue Apr 19, 2023
fmeum pushed a commit to connyay/bazel-gazelle that referenced this issue May 1, 2023
fmeum pushed a commit that referenced this issue May 1, 2023
* language/go: Handle `unix` build tag.

Fixes #1465

* take out unix as a known os
connyay added a commit to connyay/bazel-gazelle that referenced this issue May 30, 2023
Allows not skipping build files with unix tag and allowing
the compiler to do the correct thing.

Actually fixes bazel-contrib#1465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants