Skip to content

Commit d7a25a2

Browse files
authored
Improve consistency by prefering bazel_dep over Go (#2065)
Letting the bazel_dep and Go dependency participate equally in the version resolution is creating various inconsistencies. This can lead to to a root module having to either use `inject_repo` or `use_repo` and `override_repo`. Resolve this by consistently preferring the `bazel_dep` over the Go dependency. Keep the existing code that warns (or can fail) if the dependencies are at different versions. Example MODULE.bazel: ``` module( name = "bazel_dep_should_win", ) bazel_dep(name = "rules_go", version = "0.53.0") bazel_dep(name = "gazelle", version = "0.42.0") bazel_dep(name = "circl", version = "1.3.8") go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") go_deps.from_file(go_mod = "//:go.mod") ``` And the go.mod referring to a newer version. Fixes #2060 <!-- Thanks for sending a PR! Before submitting: 1. If this is your first PR, please read CONTRIBUTING.md and sign the CLA first. We cannot review code without a signed CLA. 2. Please file an issue *first*. All features and most bug fixes should have an associated issue with a design discussed and decided upon. Small bug fixes and documentation improvements don't need issues. 3. New features and bug fixes must have tests. Documentation may need to be updated. If you're unsure what to update, send the PR, and we'll discuss in review. --> **What type of PR is this?** > Uncomment one line below and remove others. > > Bug fix > Feature > Documentation > Other **What package or component does this PR mostly affect?** > For example: > > language/go > cmd/gazelle > go_repository > all **What does this PR do? Why is it needed?** **Which issues(s) does this PR fix?** Fixes # **Other notes for review**
1 parent e59910c commit d7a25a2

File tree

5 files changed

+8
-9
lines changed

5 files changed

+8
-9
lines changed

internal/bzlmod/go_deps.bzl

+2-7
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,8 @@ def _go_deps_impl(module_ctx):
563563

564564
bazel_dep_is_older = path in module_resolutions and bazel_dep.version < module_resolutions[path].version
565565

566-
# Version mismatches between the Go module and the bazel_dep can confuse Go tooling. If the bazel_dep version
567-
# is lower, it won't be used, which can result in unexpected builds and should thus always be reported, even for
568-
# indirect deps. Explicitly overridden modules are not reported as this requires manual action.
566+
# Version mismatches between the Go module and the bazel_dep are problematic. For consistency always
567+
# prefer the bazel_dep version and report any mismatch to the user.
569568
if (path in module_resolutions and
570569
bazel_dep.version != module_resolutions[path].version and
571570
bazel_dep.version != _HIGHEST_VERSION_SENTINEL and
@@ -617,10 +616,6 @@ Mismatch between versions requested for Go module {module}:
617616
go_module_version = go_module_version,
618617
), *remediation)
619618

620-
# Only use the Bazel module if it is at least as high as the required Go module version.
621-
if bazel_dep_is_older:
622-
continue
623-
624619
# TODO: We should update root_versions if the bazel_dep is a direct dependency of the root
625620
# module. However, we currently don't have a way to determine that.
626621
module_resolutions[path] = bazel_dep

tests/bcr/go_mod/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/bazelbuild/rules_go v0.39.1
1414
// NOTE: keep <4.7.0 to test the 'replace'
1515
github.com/bmatcuk/doublestar/v4 v4.6.0
16-
github.com/cloudflare/circl v1.3.7
16+
github.com/cloudflare/circl v1.6.1
1717
github.com/envoyproxy/protoc-gen-validate v1.0.1
1818
github.com/fmeum/dep_on_gazelle v1.0.0
1919
github.com/google/go-jsonnet v0.20.0

tests/bcr/go_mod/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
1717
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1818
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
1919
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
20+
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
21+
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
2022
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2123
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2224
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

tests/bcr/go_work/pkg/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/bazelbuild/rules_go v0.44.0
1010
// NOTE: keep <4.7.0 to test the 'replace'
1111
github.com/bmatcuk/doublestar/v4 v4.6.1
12-
github.com/cloudflare/circl v1.3.7
12+
github.com/cloudflare/circl v1.6.1
1313
github.com/envoyproxy/protoc-gen-validate v1.0.4
1414
github.com/fmeum/dep_on_gazelle v1.0.0
1515
github.com/google/safetext v0.0.0-20240104143208-7a7d9b3d812f

tests/bcr/go_work/pkg/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
1717
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1818
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
1919
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
20+
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
21+
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
2022
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2123
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2224
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)