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

Add flag to disable gotags filtering #1398

Open
codersasha opened this issue Dec 21, 2022 · 2 comments
Open

Add flag to disable gotags filtering #1398

codersasha opened this issue Dec 21, 2022 · 2 comments

Comments

@codersasha
Copy link

What version of gazelle are you using?

v0.28.0

What version of rules_go are you using?

v0.37.0

What version of Bazel are you using?

5.4.0

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

Yes

What operating system and processor architecture are you using?

Ubuntu 22.04.1 LTS

What did you do?

As of #1243, the latest version of Gazelle automatically filers out files without gotags set. This means that one of my libraries has gone from:

go_library(
    name = "library",
    srcs = [
        "config.go",
        "defaults.go",
        "defaults_local.go",
        "defaults_not_local.go",
    ],
    importpath = "my/path/to/library",
    visibility = ["//visibility:public"],
)

to

go_library(
    name = "library",
    srcs = [
        "config.go",
        "defaults.go",
        "defaults_not_local.go",
    ],
    importpath = "my/path/to/library",
    visibility = ["//visibility:public"],
)

The contents of defaults_local.go is:

//go:build local

package library

var getName = func(name Name) string {
	return "a"
}

And the contents of defaults_not_local.go is:

//go:build !local

package library

var getName = func(name Name) string {
	return "b"
}

This library is then used to build two go_binary targets:

go_binary(
    name ="library_bin",
    embed = [":library"],
)

go_binary(
    name ="library_local_bin",
    gotags = ["local"],
    embed = [":library"],
)

What did you expect to see?

I expected to be able to run both binary targets (bazel run //:library_bin and bazel run //:library_local_bin).

What did you see instead?

bazel run //:library_local_bin now fails with getName is not defined, because defaults_local.go has been removed from the dependencies.

If I configure gazelle to use -gotags=local for all targets, then the other target breaks, since defaults_not_local.go is now excluded.

Given this is a common pattern, it would be good to have a way of disabling the gotags filtering behaviour, so I can include both sources in the target definition, but switch between them at go_binary build time. Alternatively, Gazelle would need a way of generating two libraries, one with the build tag and one without it.

@t0rr3sp3dr0
Copy link

Any luck finding a workaround for this?

@dtkpp
Copy link

dtkpp commented Sep 11, 2024

We have a wrapper rule around the standard go_library rule in Bazel which simply replaces source code files. This force includes both files in go.

Define a new loadable rule with bazel.

Project definition path.

/project/bazel/go_library/def.bzl

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

def my_go_library(name, **kwargs):
   srcs = kwargs.pop("srcs", []) + native.glob(["*_local.go"], exclude = ["*_not_local.go"])
   go_library(
      name = name,
      srcs = srcs,
       **kwargs
   )

Define in gazelle run as a directive.

# gazelle:map_kind go_library my_go_library //project/bazel/go_library:def.bzl
gazelle(name = "gazelle")

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

No branches or pull requests

3 participants