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: Remove deprecated override attributes on go_deps.module #1548

Merged
merged 1 commit into from
May 30, 2023
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
141 changes: 14 additions & 127 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ load(
load(":semver.bzl", "semver")
load(
":utils.bzl",
"buildozer_cmd",
"drop_nones",
"format_module_file_fixup",
"format_rule_call",
"get_directive_value",
"with_replaced_or_new_fields",
Expand All @@ -28,89 +26,29 @@ _IGNORED_MODULE_PATHS = [
_FORBIDDEN_OVERRIDE_TAG = """\
Using the "go_deps.{tag_class}" tag in a non-root Bazel module is forbidden, \
but module "{module_name}" requests it.
"""

_FORBIDDEN_OVERRIDE_ATTRIBUTE = """\
Using the "{attribute}" attribute in a "go_deps.{tag_class}" tag is forbidden \
in non-root Bazel modules, but module "{module_name}" requests it.
"""

_DIRECTIVES_CALL_TO_ACTION = """\

If you need this override for a Bazel module that will be available in a public \
registry (such as the Bazel Central Registry), please file an issue at \
https://github.com/bazelbuild/bazel-gazelle/issues/new or submit a PR adding \
the required directives to the "directives.bzl" file at \
https://github.com/bazelbuild/bazel-gazelle/tree/master/internal/bzlmod/directives.bzl.
the required directives to the "default_gazelle_overrides.bzl" file at \
https://github.com/bazelbuild/bazel-gazelle/tree/master/internal/bzlmod/default_gazelle_overrides.bzl.
"""

def _report_forbidden_override(module, tag_class, attribute = None):
if attribute:
message = _FORBIDDEN_OVERRIDE_ATTRIBUTE.format(
attribute = attribute,
tag_class = tag_class,
module_name = module.name,
)
else:
message = _FORBIDDEN_OVERRIDE_TAG.format(
tag_class = tag_class,
module_name = module.name,
)

return message + _DIRECTIVES_CALL_TO_ACTION

def _fail_on_non_root_overrides(module, tag_class, attribute = None):
def _fail_on_non_root_overrides(module, tag_class):
if module.is_root:
return

tags = getattr(module.tags, tag_class)
for tag in tags:
if attribute:
if getattr(tag, attribute):
fail(_report_forbidden_override(module, tag_class, attribute))
else:
fail(_report_forbidden_override(module, tag_class))
if getattr(module.tags, tag_class):
fail(_FORBIDDEN_OVERRIDE_TAG.format(
tag_class = tag_class,
module_name = module.name,
))

def _check_directive(directive):
if directive.startswith("gazelle:") and " " in directive and not directive[len("gazelle:"):][0].isspace():
return
fail("Invalid Gazelle directive: \"{}\". Gazelle directives must be of the form \"gazelle:key value\".".format(directive))

def _synthesize_gazelle_override(module, gazelle_overrides, fixups):
"""Translate deprecated override attributes to directives for a transition period."""
directives = []

build_naming_convention = getattr(module, "build_naming_convention", "")
if build_naming_convention:
directive = "gazelle:go_naming_convention " + build_naming_convention
directives.append(directive)

build_file_proto_mode = getattr(module, "build_file_proto_mode", "")
if build_file_proto_mode:
directive = "gazelle:proto " + build_file_proto_mode
directives.append(directive)

if directives:
_safe_update_overrides(module, gazelle_overrides, directives)
fixups.extend([
buildozer_cmd("new", "go_deps.gazelle_override", module.path),
buildozer_cmd("add", "directives", name = module.path, *directives),
buildozer_cmd("rename", "name", "path", name = module.path),
])

def _safe_update_overrides(module, gazelle_overrides, directives):
if module.path in gazelle_overrides:
existing = gazelle_overrides[module.path].directives
build_file_generation = gazelle_overrides[module.path].build_file_generation
else:
existing = []
build_file_generation = "auto"

gazelle_overrides[module.path] = struct(
directives = existing + directives,
build_file_generation = build_file_generation,
)

def _get_build_file_generation(path, gazelle_overrides):
override = gazelle_overrides.get(path)
if override:
Expand Down Expand Up @@ -262,12 +200,13 @@ def _go_deps_impl(module_ctx):

# Load sums from manually specified modules separately.
for module_tag in module.tags.module:
if module_tag.build_naming_convention:
fail("""The "build_naming_convention" attribute is no longer supported for "go_deps.module" tags. Use a "gazelle:go_naming_convention" directive via the "gazelle_override" tag's "directives" attribute instead.""")
if module_tag.build_file_proto_mode:
fail("""The "build_file_proto_mode" attribute is no longer supported for "go_deps.module" tags. Use a "gazelle:proto" directive via the "gazelle_override" tag's "directives" attribute instead.""")
sum_version = _canonicalize_raw_version(module_tag.version)
_safe_insert_sum(sums, (module_tag.path, sum_version), module_tag.sum)

_fail_on_non_root_overrides(module, "module", "build_naming_convention")
_fail_on_non_root_overrides(module, "module", "build_file_proto_mode")

# 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 @@ -288,10 +227,6 @@ def _go_deps_impl(module_ctx):
paths[module_tag.path] = None
raw_version = _canonicalize_raw_version(module_tag.version)

# Note: While we still have overrides in rules_go, those will take precedence over the
# ones defined in the root module.
_synthesize_gazelle_override(module_tag, gazelle_overrides, root_fixups if module.is_root else [])

# 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
Expand Down Expand Up @@ -384,24 +319,6 @@ def _go_deps_impl(module_ctx):
}),
)

if root_fixups:
root_fixups.extend([
buildozer_cmd("remove", "build_naming_convention", name = "%go_deps.module"),
buildozer_cmd("remove", "build_file_proto_mode", name = "%go_deps.module"),
])

print("""

The 'build_naming_convention' and 'build_file_proto_mode' attributes of \
go_deps.module have been replaced with the more general go_deps.gazelle_override \
tag and will be removed in the next release of rules_go.

To migrate manually, add a gazelle_override tag for all Go module paths that set \
one of these attributes and add "gazelle:go_naming_convention <value>" (for \
build_naming_convention) or "gazelle:proto <value>" (for build_file_proto_mode) \
to its 'directives' attribute.
""" + format_module_file_fixup(root_fixups))

return _extension_metadata(
module_ctx,
root_module_direct_deps = root_module_direct_deps.keys(),
Expand Down Expand Up @@ -453,42 +370,12 @@ _module_tag = tag_class(
"path": attr.string(mandatory = True),
"version": attr.string(mandatory = True),
"sum": attr.string(),
"build_naming_convention": attr.string(
doc = """The library naming convention to use when
resolving dependencies against this Go module's external
repository.

Deprecated: Use the "gazelle:build_file_names" directive
via gazelle_override tag's "directives" attribute
instead.""",
default = "",
values = [
"go_default_library",
"import",
"import_alias",
],
),
"build_file_proto_mode": attr.string(
doc = """The mode to use when generating rules for
Protocol Buffers files for this Go module's external
repository.

Deprecated: Use the "gazelle:proto" directive via
gazelle_override tag's "directives" attribute
instead.""",
default = "",
values = [
"default",
"disable",
"disable_global",
"legacy",
"package",
],
),
"indirect": attr.bool(
doc = """Whether this Go module is an indirect dependency.""",
default = False,
),
"build_naming_convention": attr.string(doc = """Removed, do not use""", default = ""),
fmeum marked this conversation as resolved.
Show resolved Hide resolved
"build_file_proto_mode": attr.string(doc = """Removed, do not use""", default = ""),
},
)

Expand Down
28 changes: 0 additions & 28 deletions internal/bzlmod/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,6 @@ def get_directive_value(directives, key):

return value

def buildozer_cmd(*args, name = "all"):
return struct(
args = args,
name = name,
)

def format_module_file_fixup(commands):
"""Format a fixup suggestion for a MODULE.bazel file.

Args:
commands: A list of return values of buildozer_cmd.

Returns:
A buildozer script performing the fixup.
"""
script_lines = [_format_buildozer_command(cmd) for cmd in commands]
return """
To migrate automatically, paste the following lines into a file and run it with \
'buildozer -f <file>', using at least buildozer 6.0.0:

""" + "\n".join(script_lines) + "\n\n"

def _format_buildozer_command(cmd):
return "{args}|//MODULE.bazel:{name}".format(
args = " ".join([arg.replace(" ", "\\ ") for arg in cmd.args]),
name = cmd.name,
)

def with_replaced_or_new_fields(_struct, **replacements):
"""Provides a shallow copy of a structure with replacements and/or new fields

Expand Down
11 changes: 8 additions & 3 deletions tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ go_deps.module_override(

# Transitive dependencies have to be listed here explicitly.
go_deps.module(
# Verify that the build naming convention is picked up by Gazelle when it
# emits references to this repo.
build_naming_convention = "go_default_library",
indirect = True,
path = "gopkg.in/yaml.v3",
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
version = "v3.0.1",
)
go_deps.gazelle_override(
path = "gopkg.in/yaml.v3",
directives = [
# Verify that the build naming convention is picked up by Gazelle when it
# emits references to this repo.
"gazelle:go_naming_convention go_default_library",
],
)
go_deps.module(
indirect = True,
path = "github.com/davecgh/go-spew",
Expand Down