From b7db28c225a451cd28f7e6dcd08a332f33289bd4 Mon Sep 17 00:00:00 2001 From: Tyler French <66684063+tyler-french@users.noreply.github.com> Date: Tue, 21 Nov 2023 12:25:10 -0500 Subject: [PATCH] fix(bzlmod): fail on unused overrides (#1669) --- internal/bzlmod/go_deps.bzl | 17 ++++++++++------- tests/bcr/MODULE.bazel.lock | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index 5135728ac..5ecac2255 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -64,6 +64,13 @@ def _fail_on_duplicate_overrides(path, module_name, overrides): if path in overrides: fail("Multiple overrides defined for Go module path \"{}\" in module \"{}\".".format(path, module_name)) +def _fail_on_unmatched_overrides(override_keys, resolutions, override_name): + unmatched_overrides = [path for path in override_keys if path not in resolutions] + if unmatched_overrides: + fail("Some {} did not target a Go module with a matching path: {}".format( + override_name, ", ".join(unmatched_overrides) + )) + def _check_directive(directive): if directive.startswith("gazelle:") and " " in directive and not directive[len("gazelle:"):][0].isspace(): return @@ -313,13 +320,9 @@ def _go_deps_impl(module_ctx): raw_version = raw_version, ) - unmatched_gazelle_overrides = [] - for path in gazelle_overrides.keys(): - if path not in module_resolutions: - unmatched_gazelle_overrides.append(path) - if unmatched_gazelle_overrides: - fail("Some gazelle_overrides did not target a Go module with a matching path: {}" - .format(", ".join(unmatched_gazelle_overrides))) + _fail_on_unmatched_overrides(archive_overrides.keys(), module_resolutions, "archive_overrides") + _fail_on_unmatched_overrides(gazelle_overrides.keys(), module_resolutions, "gazelle_overrides") + _fail_on_unmatched_overrides(module_overrides.keys(), module_resolutions, "module_overrides") # All `replace` directives are applied after version resolution. # We can simply do this by checking the replace paths' existence diff --git a/tests/bcr/MODULE.bazel.lock b/tests/bcr/MODULE.bazel.lock index fe544771f..2d23fb412 100644 --- a/tests/bcr/MODULE.bazel.lock +++ b/tests/bcr/MODULE.bazel.lock @@ -1339,7 +1339,7 @@ }, "@gazelle~override//:extensions.bzl%go_deps": { "general": { - "bzlTransitiveDigest": "hxVTSAg3X82nHfibhRDy0s6HkpFt+/TM4p5q9fgmPj8=", + "bzlTransitiveDigest": "L3AZyX5+0E4G4CLl2qHH0wR716rVmra4pU+jYAVPz4A=", "accumulatedFileDigests": { "@@//:go.mod": "8e62c686b94b37593b38766d425ceccbf86a9b07c0121feaaf42293050a42ae3", "@@circl~1.3.3//:go.mod": "68b12e4662bb0639728490153ffc52d8bdd63be558bdd41bcb0d8f1eeeb03e41", @@ -1893,7 +1893,7 @@ }, "@gazelle~override//:extensions.bzl%go_deps%test_dep@_~go_deps": { "general": { - "bzlTransitiveDigest": "hxVTSAg3X82nHfibhRDy0s6HkpFt+/TM4p5q9fgmPj8=", + "bzlTransitiveDigest": "L3AZyX5+0E4G4CLl2qHH0wR716rVmra4pU+jYAVPz4A=", "accumulatedFileDigests": {}, "envVariables": {}, "generatedRepoSpecs": {