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

bzlmod: Add build_file_generation to gazelle_override #1522

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions internal/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ filegroup(
testonly = True,
srcs = [
"BUILD.bazel",
"directives.bzl",
"default_gazelle_overrides.bzl",
"go_deps.bzl",
"go_mod.bzl",
"non_module_deps.bzl",
Expand All @@ -15,18 +15,12 @@ filegroup(
visibility = ["//visibility:public"],
)

bzl_library(
name = "directives",
srcs = ["directives.bzl"],
fmeum marked this conversation as resolved.
Show resolved Hide resolved
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "go_deps",
srcs = ["go_deps.bzl"],
visibility = ["//:__subpackages__"],
deps = [
":directives",
":default_gazelle_overrides",
":semver",
"//internal:go_repository",
],
Expand Down Expand Up @@ -60,3 +54,9 @@ bzl_library(
srcs = ["utils.bzl"],
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "default_gazelle_overrides",
srcs = ["default_gazelle_overrides.bzl"],
visibility = ["//:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
visibility("private")

DEFAULT_BUILD_FILE_GENERATION_BY_PATH = {
"github.com/google/safetext": "on",
}

DEFAULT_DIRECTIVES_BY_PATH = {
"github.com/census-instrumentation/opencensus-proto": [
"gazelle:proto disable",
Expand All @@ -10,6 +14,10 @@ DEFAULT_DIRECTIVES_BY_PATH = {
"github.com/google/gnostic": [
"gazelle:proto disable",
],
"github.com/google/safetext": [
"gazelle:build_file_name BUILD.bazel",
"gazelle:build_file_proto_mode disable_global",
],
"github.com/googleapis/gnostic": [
"gazelle:proto disable",
],
Expand Down
36 changes: 33 additions & 3 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
load("//internal:go_repository.bzl", "go_repository")
load(":go_mod.bzl", "deps_from_go_mod", "sums_from_go_mod")
load(":directives.bzl", "DEFAULT_DIRECTIVES_BY_PATH")
load(
":default_gazelle_overrides.bzl",
"DEFAULT_BUILD_FILE_GENERATION_BY_PATH",
"DEFAULT_DIRECTIVES_BY_PATH",
)
load(":semver.bzl", "semver")
load(
":utils.bzl",
Expand Down Expand Up @@ -87,22 +91,33 @@ def _synthesize_gazelle_override(module, gazelle_overrides, fixups):
directives.append(directive)

if directives:
_safe_append_directives(module, gazelle_overrides, directives)
_safe_update_overrides(module, gazelle_overrides, directives)
fixups.extend([
buildozer_cmd("new", "go_deps.gazelle_override", module.path),
buildozer_cmd("add", "directives", name = module.path, *directives),
buildozer_cmd("rename", "name", "path", name = module.path),
])

def _safe_append_directives(module, gazelle_overrides, directives):
def _safe_update_overrides(module, gazelle_overrides, directives):
if module.path in gazelle_overrides:
existing = gazelle_overrides[module.path].directives
build_file_generation = gazelle_overrides[module.path].build_file_generation
else:
existing = []
build_file_generation = "auto"

gazelle_overrides[module.path] = struct(
directives = existing + directives,
build_file_generation = build_file_generation,
)

def _get_build_file_generation(path, gazelle_overrides):
override = gazelle_overrides.get(path)
if override:
return override.build_file_generation

return DEFAULT_BUILD_FILE_GENERATION_BY_PATH.get(path, "auto")
fmeum marked this conversation as resolved.
Show resolved Hide resolved

def _get_directives(path, gazelle_overrides):
override = gazelle_overrides.get(path)
if override:
Expand Down Expand Up @@ -184,6 +199,7 @@ def _go_deps_impl(module_ctx):

gazelle_overrides[gazelle_override_tag.path] = struct(
directives = gazelle_override_tag.directives,
build_file_generation = gazelle_override_tag.build_file_generation,
)

_fail_on_non_root_overrides(module, "module_override")
Expand Down Expand Up @@ -308,6 +324,7 @@ def _go_deps_impl(module_ctx):
replace = getattr(module, "replace", None),
version = "v" + module.raw_version,
build_directives = _get_directives(path, gazelle_overrides),
build_file_generation = _get_build_file_generation(path, gazelle_overrides),
patches = _get_patches(path, module_overrides),
patch_args = _get_patch_args(path, module_overrides),
)
Expand Down Expand Up @@ -433,6 +450,19 @@ _gazelle_override_tag = tag_class(
extension within this Bazel module.""",
mandatory = True,
),
"build_file_generation": attr.string(
default = "auto",
doc = """One of `"auto"` (default), `"on"`, `"off"`.

Whether Gazelle should generate build files for the Go module. In
fmeum marked this conversation as resolved.
Show resolved Hide resolved
`"auto"` mode, Gazelle will run if there is no build file in the Go
module's root directory.""",
values = [
"auto",
"off",
"on",
],
),
"directives": attr.string_list(
doc = """Gazelle configuration directives to use for this Go module's external repository.

Expand Down
1 change: 1 addition & 0 deletions tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ go_deps.module(
use_repo(
go_deps,
"com_github_bmatcuk_doublestar_v4",
"com_github_google_safetext",
"com_github_pelletier_go_toml",
"com_github_stretchr_testify",
# It is not necessary to list transitive dependencies here, only direct ones.
Expand Down
5 changes: 4 additions & 1 deletion tests/bcr/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ go 1.19
// Validate go.mod replace directives can be properly used:
replace github.com/bmatcuk/doublestar/v4 => github.com/bmatcuk/doublestar v1.3.4

require github.com/bmatcuk/doublestar/v4 v4.0.0
require (
github.com/bmatcuk/doublestar/v4 v4.0.0
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2
)
2 changes: 2 additions & 0 deletions tests/bcr/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=
github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 h1:SJ+NtwL6QaZ21U+IrK7d0gGgpjGGvd2kz+FzTHVzdqI=
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2/go.mod h1:Tv1PlzqC9t8wNnpPdctvtSUOPUUg4SHeE6vR1Ir2hmg=
1 change: 1 addition & 0 deletions tests/bcr/pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_test(
srcs = ["mvs_test.go"],
deps = [
"@com_github_bmatcuk_doublestar_v4//:doublestar",
"@com_github_google_safetext//yamltemplate",
"@com_github_pelletier_go_toml//:go-toml",
"@com_github_stretchr_testify//require:go_default_library",
],
Expand Down
9 changes: 8 additions & 1 deletion tests/bcr/pkg/mvs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"testing"

"github.com/bmatcuk/doublestar/v4"
toml "github.com/pelletier/go-toml"
"github.com/google/safetext/yamltemplate"
"github.com/pelletier/go-toml"
"github.com/stretchr/testify/require"
)

Expand All @@ -31,3 +32,9 @@ func TestPatch(t *testing.T) {
// a patch is used to add this constant.
require.Equal(t, "hello", require.Hello)
}

func TestBuildFileGeneration(t *testing.T) {
// github.com/google/safetext@v0.0.0-20220905092116-b49f7bc46da2 requires overwriting the BUILD
// files it provides as well as directives.
yamltemplate.HTMLEscapeString("<b>foo</b>")
}