From b160ccd46fb7c19a49530d08906165c73c343dd1 Mon Sep 17 00:00:00 2001 From: Srini Date: Thu, 17 Oct 2024 18:13:50 -0400 Subject: [PATCH] Hard failing if module versions aren't aligned between bazel dep and go.mod for a given module (#1957) **What type of PR is this?** >Bug fix/Change in behavior **What package or component does this PR mostly affect?** >go_deps **What does this PR do? Why is it needed?** If a given Go module is requested both via `go.mod` and directly as a `bazel_dep`, this PR will change Gazelle's behavior to hard fail as opposed to use the Bazel dep so long as it's a higher version than the one in `go.mod` Ideally, Gazelle should fail and alert the developer to the mismatch so that they may align the versions in order to ensure that Native Go builds use the same SoT as Bazel builds. Tested locally: ``` >bazel build //... INFO: Invocation ID: 09d43bbd-a6b9-4e9f-a0b0-094be01359bf ERROR: Traceback (most recent call last): File "/private/var/tmp/_bazel_smuthu/572bd1d287d7ec2c900673904e0dbb38/external/gazelle~/internal/bzlmod/go_deps.bzl", line 571, column 17, in _go_deps_impl fail("\n\nMismatch between versions requested for module {module}\nBazel dependency version requested in MODULE.bazel: {bazel_dep_version}\nGo module version requested in go.mod: {go_module_version}\nPlease resolve this mismatch to prevent discrepancies between native Go and Bazel builds\n\n".format( Error in fail: Mismatch between versions requested for module github.com/cloudflare/circl Bazel dependency version requested in MODULE.bazel: 1.3.8 Go module version requested in go.mod: 1.3.7 Please resolve this mismatch to prevent discrepancies between native Go and Bazel builds ERROR: error evaluating module extension go_deps in @@gazelle~//:extensions.bzl INFO: Elapsed time: 0.195s INFO: 0 processes. ERROR: Build did NOT complete successfully INFO: Build Event Protocol files produced successfully. FAILED: Fetching module extension go_deps in @@gazelle~//:extensions.bzl; starting ``` **Which issues(s) does this PR fix?** No issue currently but happy to create one if necessary [Bazel Slack Discussion](https://bazelbuild.slack.com/archives/C014RARENH0/p1728680692301999) --- MODULE.bazel | 2 +- go.mod | 6 ++++-- go.sum | 4 ++-- internal/bzlmod/go_deps.bzl | 16 ++++++---------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 19848c949..ab60f8314 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,7 +1,7 @@ module( name = "gazelle", # Updated by the Publish to BCR app. - version = "0.999.0", + version = "0.30.0", repo_name = "bazel_gazelle", ) diff --git a/go.mod b/go.mod index cdb764900..b7cb1f519 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,12 @@ module github.com/bazelbuild/bazel-gazelle -go 1.21 +go 1.21.1 + +toolchain go1.22.7 require ( github.com/bazelbuild/buildtools v0.0.0-20240827154017-dd10159baa91 - github.com/bazelbuild/rules_go v0.46.0 + github.com/bazelbuild/rules_go v0.50.1 github.com/bmatcuk/doublestar/v4 v4.6.1 github.com/fsnotify/fsnotify v1.7.0 github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum index 476b0b41e..77b97707a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/bazelbuild/buildtools v0.0.0-20240827154017-dd10159baa91 h1:/wpuwyWvp46gZfQCmbR+4SI5ne7IjRUM5lsXTzpAeWM= github.com/bazelbuild/buildtools v0.0.0-20240827154017-dd10159baa91/go.mod h1:PLNUetjLa77TCCziPsz0EI8a6CUxgC+1jgmWv0H25tg= -github.com/bazelbuild/rules_go v0.46.0 h1:CTefzjN/D3Cdn3rkrM6qMWuQj59OBcuOjyIp3m4hZ7s= -github.com/bazelbuild/rules_go v0.46.0/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs= +github.com/bazelbuild/rules_go v0.50.1 h1:/BUvuaB8MEiUA2oLPPCGtuw5V+doAYyiGTFyoSWlkrw= +github.com/bazelbuild/rules_go v0.50.1/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs= github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I= github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index fac95de7a..1fcaf9011 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -567,16 +567,12 @@ def _go_deps_impl(module_ctx): continue # Only use the Bazel module if it is at least as high as the required Go module version. - if path in module_resolutions and bazel_dep.version < module_resolutions[path].version: - outdated_direct_dep_printer( - "Go module \"{path}\" is provided by Bazel module \"{bazel_module}\" in version {bazel_dep_version}, but requested at higher version {go_version} via Go requirements. Consider adding or updating an appropriate \"bazel_dep\" to ensure that the Bazel module is used to provide the Go module.".format( - path = path, - bazel_module = bazel_dep.module_name, - bazel_dep_version = bazel_dep.raw_version, - go_version = module_resolutions[path].raw_version, - ), - ) - continue + if path in module_resolutions and bazel_dep.version != module_resolutions[path].version: + fail("\n\nMismatch between versions requested for module {module}\nBazel dependency version requested in MODULE.bazel: {bazel_dep_version}\nGo module version requested in go.mod: {go_module_version}\nPlease resolve this mismatch to prevent discrepancies between native Go and Bazel builds\n\n".format( + module = path, + bazel_dep_version = bazel_dep.raw_version, + go_module_version = module_resolutions[path].raw_version, + )) # TODO: We should update root_versions if the bazel_dep is a direct dependency of the root # module. However, we currently don't have a way to determine that.