diff --git a/internal/bzlmod/BUILD.bazel b/internal/bzlmod/BUILD.bazel index 7262a896c..16528376b 100644 --- a/internal/bzlmod/BUILD.bazel +++ b/internal/bzlmod/BUILD.bazel @@ -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", @@ -15,18 +15,12 @@ filegroup( visibility = ["//visibility:public"], ) -bzl_library( - name = "directives", - srcs = ["directives.bzl"], - visibility = ["//:__subpackages__"], -) - bzl_library( name = "go_deps", srcs = ["go_deps.bzl"], visibility = ["//:__subpackages__"], deps = [ - ":directives", + ":default_gazelle_overrides", ":semver", "//internal:go_repository", ], @@ -60,3 +54,9 @@ bzl_library( srcs = ["utils.bzl"], visibility = ["//:__subpackages__"], ) + +bzl_library( + name = "default_gazelle_overrides", + srcs = ["default_gazelle_overrides.bzl"], + visibility = ["//:__subpackages__"], +) diff --git a/internal/bzlmod/directives.bzl b/internal/bzlmod/default_gazelle_overrides.bzl similarity index 74% rename from internal/bzlmod/directives.bzl rename to internal/bzlmod/default_gazelle_overrides.bzl index 48cea51c1..593eb0ced 100644 --- a/internal/bzlmod/directives.bzl +++ b/internal/bzlmod/default_gazelle_overrides.bzl @@ -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", @@ -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", ], diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index b17e86669..6a7ac1aea 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -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", @@ -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") + def _get_directives(path, gazelle_overrides): override = gazelle_overrides.get(path) if override: @@ -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") @@ -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), ) @@ -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 + `"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. diff --git a/tests/bcr/MODULE.bazel b/tests/bcr/MODULE.bazel index fe0d242c5..fd07f9e2f 100644 --- a/tests/bcr/MODULE.bazel +++ b/tests/bcr/MODULE.bazel @@ -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. diff --git a/tests/bcr/go.mod b/tests/bcr/go.mod index 8e41da700..a3508955e 100644 --- a/tests/bcr/go.mod +++ b/tests/bcr/go.mod @@ -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 +) diff --git a/tests/bcr/go.sum b/tests/bcr/go.sum index b523b6c78..099ae8fcd 100644 --- a/tests/bcr/go.sum +++ b/tests/bcr/go.sum @@ -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= diff --git a/tests/bcr/pkg/BUILD.bazel b/tests/bcr/pkg/BUILD.bazel index f2d3555ad..06de19559 100644 --- a/tests/bcr/pkg/BUILD.bazel +++ b/tests/bcr/pkg/BUILD.bazel @@ -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", ], diff --git a/tests/bcr/pkg/mvs_test.go b/tests/bcr/pkg/mvs_test.go index 5d55508bd..0079710f1 100644 --- a/tests/bcr/pkg/mvs_test.go +++ b/tests/bcr/pkg/mvs_test.go @@ -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" ) @@ -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("foo") +}