Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bzlmod: Add go_deps.from_file #1300

Merged
merged 5 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 1 addition & 206 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,212 +22,7 @@ use_repo(
)

go_deps = use_extension("//:extensions.bzl", "go_deps")
go_deps.module(
fmeum marked this conversation as resolved.
Show resolved Hide resolved
importpath = "honnef.co/go/tools",
sum = "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=",
version = "v0.0.0-20190523083050-ea95bdfd59fc",
)
go_deps.module(
importpath = "github.com/bazelbuild/buildtools",
sum = "h1:ox8T5nCkvLflRgKZ6uzzdvLDN3gbNZBgLBqA5Xgn7QA=",
version = "v0.0.0-20220323134444-a9f46b2bb3de",
build_naming_convention = "go_default_library",
)
go_deps.module(
importpath = "github.com/bazelbuild/rules_go",
sum = "h1:8/MDe6zCE7nYKXhlbamM1izUgJjw2ce7M5kd4QKF/3Y=",
version = "v0.31.0",
)
go_deps.module(
importpath = "github.com/bmatcuk/doublestar/v4",
sum = "h1:X0krlUVAVmtr2cRoTqR8aDMrDqnB36ht8wpWTiQ3jsA=",
version = "v4.0.2",
)
go_deps.module(
importpath = "github.com/BurntSushi/toml",
sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=",
version = "v0.3.1",
)
go_deps.module(
importpath = "github.com/census-instrumentation/opencensus-proto",
sum = "h1:glEXhBS5PSLLv4IXzLA5yPRVX4bilULVyxxbrfOtDAk=",
version = "v0.2.1",
)
go_deps.module(
importpath = "github.com/chzyer/logex",
sum = "h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=",
version = "v1.1.10",
)
go_deps.module(
importpath = "github.com/chzyer/readline",
sum = "h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=",
version = "v0.0.0-20180603132655-2972be24d48e",
)
go_deps.module(
importpath = "github.com/chzyer/test",
sum = "h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=",
version = "v0.0.0-20180213035817-a1ea475d72b1",
)
go_deps.module(
importpath = "github.com/client9/misspell",
sum = "h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=",
version = "v0.3.4",
)
go_deps.module(
importpath = "github.com/davecgh/go-spew",
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
version = "v1.1.1",
)
go_deps.module(
importpath = "github.com/envoyproxy/go-control-plane",
sum = "h1:4cmBvAEBNJaGARUEs3/suWRyfyBfhf7I60WBZq+bv2w=",
version = "v0.9.1-0.20191026205805-5f8ba28d4473",
)
go_deps.module(
importpath = "github.com/envoyproxy/protoc-gen-validate",
sum = "h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=",
version = "v0.1.0",
)
go_deps.module(
importpath = "github.com/fsnotify/fsnotify",
sum = "h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=",
version = "v1.5.1",
)
go_deps.module(
importpath = "github.com/golang/glog",
sum = "h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=",
version = "v0.0.0-20160126235308-23def4e6c14b",
)
go_deps.module(
importpath = "github.com/golang/mock",
sum = "h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=",
version = "v1.1.1",
)
go_deps.module(
importpath = "github.com/golang/protobuf",
sum = "h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=",
version = "v1.4.3",
)
go_deps.module(
importpath = "github.com/google/go-cmp",
sum = "h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=",
version = "v0.5.7",
)
go_deps.module(
importpath = "github.com/pelletier/go-toml",
sum = "h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=",
version = "v1.9.4",
)
go_deps.module(
importpath = "github.com/pmezard/go-difflib",
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
version = "v1.0.0",
)
go_deps.module(
importpath = "github.com/prometheus/client_model",
sum = "h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=",
version = "v0.0.0-20190812154241-14fe0d1b01d4",
)
go_deps.module(
importpath = "github.com/yuin/goldmark",
sum = "h1:OtISOGfH6sOWa1/qXqqAiOIAO6Z5J3AEAE18WAq6BiQ=",
version = "v1.4.0",
)
go_deps.module(
importpath = "cloud.google.com/go",
sum = "h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=",
version = "v0.26.0",
)
go_deps.module(
importpath = "gopkg.in/check.v1",
sum = "h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=",
version = "v0.0.0-20161208181325-20d25e280405",
)
go_deps.module(
importpath = "gopkg.in/yaml.v2",
sum = "h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=",
version = "v2.2.2",
)
go_deps.module(
importpath = "go.starlark.net",
sum = "h1:xwwDQW5We85NaTk2APgoN9202w/l0DVGp+GZMfsrh7s=",
version = "v0.0.0-20210223155950-e043a3d3c984",
)
go_deps.module(
importpath = "google.golang.org/appengine",
sum = "h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=",
version = "v1.4.0",
)
go_deps.module(
importpath = "google.golang.org/genproto",
sum = "h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=",
version = "v0.0.0-20200526211855-cb27e3aa2013",
)
go_deps.module(
importpath = "google.golang.org/grpc",
sum = "h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg=",
version = "v1.27.0",
)
go_deps.module(
importpath = "google.golang.org/protobuf",
sum = "h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=",
version = "v1.25.0",
)
go_deps.module(
importpath = "golang.org/x/crypto",
sum = "h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=",
version = "v0.0.0-20191011191535-87dc89f01550",
)
go_deps.module(
importpath = "golang.org/x/exp",
sum = "h1:c2HOrn5iMezYjSlGPncknSEr/8x5LELb/ilJbXi9DEA=",
version = "v0.0.0-20190121172915-509febef88a4",
)
go_deps.module(
importpath = "golang.org/x/lint",
sum = "h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0=",
version = "v0.0.0-20190313153728-d0100b6bd8b3",
)
go_deps.module(
importpath = "golang.org/x/mod",
sum = "h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=",
version = "v0.6.0-dev.0.20220106191415-9b9b3d81d5e3",
)
go_deps.module(
importpath = "golang.org/x/net",
sum = "h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=",
version = "v0.0.0-20210805182204-aaa1db679c0d",
)
go_deps.module(
importpath = "golang.org/x/oauth2",
sum = "h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=",
version = "v0.0.0-20180821212333-d2e6202438be",
)
go_deps.module(
importpath = "golang.org/x/sync",
sum = "h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=",
version = "v0.0.0-20210220032951-036812b2e83c",
)
go_deps.module(
importpath = "golang.org/x/sys",
sum = "h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=",
version = "v0.0.0-20220319134239-a9b59b0215f8",
)
go_deps.module(
importpath = "golang.org/x/text",
sum = "h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=",
version = "v0.3.6",
)
go_deps.module(
importpath = "golang.org/x/tools",
sum = "h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=",
version = "v0.1.10",
)
go_deps.module(
importpath = "golang.org/x/xerrors",
sum = "h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=",
version = "v0.0.0-20200804184101-5ec99f83aff1",
)
go_deps.from_file(go_mod = "//:go.mod")
fmeum marked this conversation as resolved.
Show resolved Hide resolved
use_repo(
go_deps,
"com_github_bazelbuild_buildtools",
Expand Down
7 changes: 7 additions & 0 deletions internal/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ filegroup(
srcs = [
"BUILD.bazel",
"go_deps.bzl",
"go_mod.bzl",
"non_module_deps.bzl",
"semver.bzl",
],
Expand All @@ -22,6 +23,12 @@ bzl_library(
],
)

bzl_library(
name = "go_mod",
srcs = ["go_mod.bzl"],
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "non_module_deps",
srcs = ["non_module_deps.bzl"],
Expand Down
58 changes: 37 additions & 21 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//internal:go_repository.bzl", "go_repository")
load(":go_mod.bzl", "deps_from_go_mod")
load(":semver.bzl", "semver")

def _repo_name(importpath):
Expand Down Expand Up @@ -47,6 +48,10 @@ def _go_deps_impl(module_ctx):
elif check_direct_deps == "error":
outdated_direct_dep_printer = fail

additional_module_tags = []
for from_file_tag in module.tags.from_file:
additional_module_tags += deps_from_go_mod(module_ctx, from_file_tag.go_mod)

# Parse the go_dep.module tags of all transitive dependencies and apply
# Minimum Version Selection to resolve importpaths to Go module versions
# and sums.
Expand All @@ -58,47 +63,51 @@ def _go_deps_impl(module_ctx):
# full graph, we can assume that at that place all its required
# transitive dependencies have also been declared - we may end up
# resolving them to higher versions, but only compatible ones.
importpaths = {}
for module_tag in module.tags.module:
if module_tag.importpath in importpaths:
fail("Duplicate importpath '{}' in module '{}'".format(module_tag.importpath, module.name))
importpaths[module_tag.importpath] = None
paths = {}
for module_tag in module.tags.module + additional_module_tags:
if module_tag.path in paths:
fail("Duplicate Go module path '{}' in module '{}'".format(module_tag.path, module.name))
paths[module_tag.path] = None
raw_version = module_tag.version
if raw_version.startswith("v"):
raw_version = raw_version[1:]
if getattr(module, "is_root", False):
root_versions[module_tag.importpath] = raw_version

# For modules imported from a go.sum, we know which ones are direct
# dependencies and can thus only report implicit version upgrades
# for direct dependencies. For manually specified go_deps.module
# tags, we always report version upgrades.
if getattr(module, "is_root", False) and getattr(module_tag, "direct", True):
root_versions[module_tag.path] = raw_version
version = semver.to_comparable(raw_version)
if module_tag.importpath not in module_resolutions or version > module_resolutions[module_tag.importpath].version:
module_resolutions[module_tag.importpath] = struct(
if module_tag.path not in module_resolutions or version > module_resolutions[module_tag.path].version:
module_resolutions[module_tag.path] = struct(
module = module.name,
repo_name = _repo_name(module_tag.importpath),
repo_name = _repo_name(module_tag.path),
version = version,
raw_version = raw_version,
sum = module_tag.sum,
build_naming_convention = module_tag.build_naming_convention,
)
is_root_module = False

for importpath, root_version in root_versions.items():
if semver.to_comparable(root_version) < module_resolutions[importpath].version:
for path, root_version in root_versions.items():
if semver.to_comparable(root_version) < module_resolutions[path].version:
outdated_direct_dep_printer(
"For Go module '{importpath}', the root module requires module version v{root_version}, but got v{resolved_version} in the resolved dependency graph.".format(
importpath = importpath,
"For Go module '{path}', the root module requires module version v{root_version}, but got v{resolved_version} in the resolved dependency graph.".format(
path = path,
root_version = root_version,
resolved_version = module_resolutions[importpath].raw_version,
resolved_version = module_resolutions[path].raw_version,
),
)

[
go_repository(
name = module.repo_name,
importpath = importpath,
importpath = path,
sum = module.sum,
version = "v" + module.raw_version,
build_naming_convention = module.build_naming_convention,
)
for importpath, module in module_resolutions.items()
for path, module in module_resolutions.items()
]

# With transitive dependencies, Gazelle would no longer just have to pass a
Expand All @@ -107,10 +116,10 @@ def _go_deps_impl(module_ctx):
# Gazelle directives for all of those modules here.
directives = {
module.repo_name: [
"importpath=" + importpath,
"importpath=" + path,
"build_naming_convention=" + module.build_naming_convention,
]
for importpath, module in module_resolutions.items()
for path, module in module_resolutions.items()
}
_go_repository_directives(
name = "bazel_gazelle_go_repository_directives",
Expand All @@ -125,9 +134,15 @@ _config_tag = tag_class(
},
)

_from_file_tag = tag_class(
attrs = {
"go_mod": attr.label(mandatory = True),
}
)

_module_tag = tag_class(
attrs = {
"importpath": attr.string(mandatory = True),
"path": attr.string(mandatory = True),
"version": attr.string(mandatory = True),
"sum": attr.string(),
"build_naming_convention": attr.string(default = "import_alias"),
Expand All @@ -138,6 +153,7 @@ go_deps = module_extension(
_go_deps_impl,
tag_classes = {
"config": _config_tag,
"from_file": _from_file_tag,
"module": _module_tag,
},
)
Loading