Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 21, 2023
1 parent f734a9a commit 0d6dcff
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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({
Expand All @@ -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
}),
)

Expand Down

0 comments on commit 0d6dcff

Please sign in to comment.