Skip to content

Commit

Permalink
Hard failing if module versions aren't aligned between bazel dep and …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
Buzz-Lightyear authored Oct 17, 2024
1 parent 3dd8374 commit b160ccd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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",
)

Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
16 changes: 6 additions & 10 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b160ccd

Please sign in to comment.