From 0d6dcff9b6a57a61457dd29ffad23f1c881c5afb Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 22 May 2023 01:09:10 +0200 Subject: [PATCH] WIP --- internal/bzlmod/go_deps.bzl | 39 ++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index 90cb6d05d..49800c72e 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -18,6 +18,8 @@ load( visibility("//") +_HIGHEST_VERSION_SENTINEL = semver.to_comparable("999999.999999.999999") + _FORBIDDEN_OVERRIDE_TAG = """\ Using the "go_deps.{tag_class}" tag in a non-root Bazel module is forbidden, \ but module "{module_name}" requests it. @@ -251,12 +253,20 @@ def _go_deps_impl(module_ctx): if module.is_root: replace_map.update(go_mod_replace_map) else: - # Register this Bazel module as providing the specified Go module. - # This ensures that there is only ever one repository representing each Go module. - bazel_deps[module_path] = struct( - canonical_name = "@" + from_file_tag.go_mod.workspace_name, - module_name = module.name, - ) + # Register this Bazel module as providing the specified Go module. It participates + # in version resolution using its registry version, which is assumed to be an + # actual semver. An empty version string signals an override, which is assumed to + # be newer than any other version. + # TODO: Decide whether and how to handle non-semver versions. + raw_version = _canonicalize_raw_version(module.version) + version = semver.to_comparable(raw_version) if raw_version else _HIGHEST_VERSION_SENTINEL + if module_path not in bazel_deps or version > bazel_deps[module_path].version: + bazel_deps[module_path] = struct( + module_name = module.name, + repo_name = "@" + from_file_tag.go_mod.workspace_name, + version = version, + raw_version = raw_version, + ) # Load all sums from transitively resolved `go.sum` files that have modules. if len(module_tags_from_go_mod) > 0: @@ -342,7 +352,16 @@ def _go_deps_impl(module_ctx): # not ever report an implicit version upgrade. root_versions.pop(path) else: - root_versions[path] = new_version + root_versions[path] = replace.version + + for path, bazel_dep in bazel_deps.items(): + if path in gazelle_overrides or path in module_overrides or path in replace_map: + continue + if path not in module_resolutions or bazel_dep.version >= module_resolutions[path].version: + # The Bazel module may not be declared as a Go dependency, so don't fail if it doesn't + # appear in these maps. + module_resolutions.pop(path, None) + root_versions.pop(path, None) for path, root_version in root_versions.items(): if semver.to_comparable(root_version) < module_resolutions[path].version: @@ -377,13 +396,12 @@ def _go_deps_impl(module_ctx): importpaths = { module.repo_name: path for path, module in module_resolutions.items() - if path not in bazel_deps } | { - info.canonical_name: path + info.repo_name: path for path, info in bazel_deps.items() }, module_names = { - info.canonical_name: info.module_name + info.repo_name: info.module_name for path, info in bazel_deps.items() }, build_naming_conventions = drop_nones({ @@ -392,7 +410,6 @@ def _go_deps_impl(module_ctx): "go_naming_convention", ) for path, module in module_resolutions.items() - if path not in bazel_deps }), )