diff --git a/rules/proto_gazelle.bzl b/rules/proto_gazelle.bzl index 676512d41..3366c8f33 100644 --- a/rules/proto_gazelle.bzl +++ b/rules/proto_gazelle.bzl @@ -40,6 +40,12 @@ def _valid_env_variable_name(name): return False return True +def _rlocation_path(ctx, file): + if file.short_path.startswith("../"): + return file.short_path[3:] + else: + return ctx.workspace_name + "/" + file.short_path + def _gazelle_runner_impl(ctx): args = [ctx.attr.command] if ctx.attr.mode: @@ -67,18 +73,17 @@ def _gazelle_runner_impl(ctx): out_file = ctx.actions.declare_file(ctx.label.name + ".bash") go_tool = ctx.toolchains["@io_bazel_rules_go//go:toolchain"].sdk.go + repo_config = ctx.file._repo_config substitutions = { "@@ARGS@@": shell.array_literal(args), - "@@GAZELLE_LABEL@@": shell.quote(str(ctx.attr.gazelle.label)), - "@@GAZELLE_SHORT_PATH@@": shell.quote(ctx.executable.gazelle.short_path), + "@@GAZELLE_PATH@@": shell.quote(_rlocation_path(ctx, ctx.executable.gazelle)), "@@GENERATED_MESSAGE@@": """ # Generated by {label} # DO NOT EDIT """.format(label = str(ctx.label)), - "@@RUNNER_LABEL@@": shell.quote(str(ctx.label)), - "@@GOTOOL@@": shell.quote(go_tool.path), + "@@GOTOOL@@": shell.quote(_rlocation_path(ctx, go_tool)), "@@ENV@@": env, - "@@REPO_CONFIG_SHORT_PATH@@": "", + "@@REPO_CONFIG_PATH@@": shell.quote(_rlocation_path(ctx, repo_config)) if repo_config else "", } ctx.actions.expand_template( template = ctx.file._template, @@ -86,24 +91,14 @@ def _gazelle_runner_impl(ctx): substitutions = substitutions, is_executable = True, ) - runfiles = ctx.runfiles(files = ctx.files.cfgs + ctx.files.imports + [ ctx.executable.gazelle, go_tool, - ]) - runfiles = runfiles.merge_all([ + ] + ([repo_config] if repo_config else [])).merge( ctx.attr.gazelle[DefaultInfo].default_runfiles, - ctx.attr.gazelle[DefaultInfo].data_runfiles, - ]) - data_files = [] + ) for d in ctx.attr.data: - data_files = d[DefaultInfo].files.to_list() - runfiles = runfiles.merge_all([ - d[DefaultInfo].default_runfiles, - d[DefaultInfo].data_runfiles, - ]) - runfiles = ctx.runfiles(files = data_files).merge(runfiles) - + runfiles = runfiles.merge(d[DefaultInfo].default_runfiles) return [DefaultInfo( files = depset([out_file]), runfiles = runfiles, @@ -122,6 +117,7 @@ _gazelle_runner = rule( values = [ "fix", "update", + "update-repos", ], default = "update", ), @@ -130,7 +126,7 @@ _gazelle_runner = rule( default = "", ), "external": attr.string( - values = ["", "external", "vendored", "static"], + values = ["", "external", "static", "vendored"], default = "", ), "build_tags": attr.string_list(), @@ -140,6 +136,10 @@ _gazelle_runner = rule( "imports": attr.label_list(allow_files = True), "cfgs": attr.label_list(allow_files = True), "env": attr.string_dict(), + "_repo_config": attr.label( + default = None, + allow_single_file = True, + ), "_template": attr.label( default = "@bazel_gazelle//internal:gazelle.bash.in", allow_single_file = True, @@ -163,14 +163,23 @@ def proto_gazelle(name, **kwargs): fail("{}: both args and extra_args were provided".format(name)) kwargs["extra_args"] = kwargs["args"] kwargs.pop("args") + + visibility = kwargs.pop("visibility", default = None) + + tags_set = {t: "" for t in kwargs.pop("tags", [])} + tags_set["manual"] = "" + tags = [k for k in tags_set.keys()] runner_name = name + "-runner" _gazelle_runner( name = runner_name, - tags = ["manual"], + tags = tags, **kwargs ) native.sh_binary( name = name, srcs = [runner_name], - tags = ["manual"], + tags = tags, + visibility = visibility, + deps = ["@bazel_tools//tools/bash/runfiles"], + data = kwargs["data"] if "data" in kwargs else [], )