diff --git a/go/def.bzl b/go/def.bzl index 48637e51ca..6d490645ce 100644 --- a/go/def.bzl +++ b/go/def.bzl @@ -48,6 +48,13 @@ def _go_prefix(ctx): prefix = prefix + "/" return prefix +def _prefix_root(ctx): + """slash terminated go-prefix""" + root = ctx.attr.go_prefix.prefix_root + if root != "" and not root.endswith("/"): + root = root + "/" + return root + # TODO(bazel-team): it would be nice if Bazel had this built-in. def symlink_tree_commands(dest_dir, artifact_dict): """Symlink_tree_commands returns a list of commands to create the @@ -165,8 +172,12 @@ def _go_importpath(ctx): Go importpath of the library """ path = _go_prefix(ctx)[:-1] + prefix_root = _prefix_root(ctx) if ctx.label.package: - path += "/" + ctx.label.package + label = ctx.label.package + if ctx.label.package.startswith(prefix_root): + label = label[len(prefix_root):] + path += "/" + label if ctx.label.name != _DEFAULT_LIB: path += "/" + ctx.label.name if path.rfind(_VENDOR_PREFIX) != -1: diff --git a/go/private/go_prefix.bzl b/go/private/go_prefix.bzl index 5ae84b6bf3..0245c1e3e1 100644 --- a/go/private/go_prefix.bzl +++ b/go/private/go_prefix.bzl @@ -22,18 +22,23 @@ def _go_prefix_impl(ctx): """go_prefix_impl provides the go prefix to use as a transitive info provider.""" - return struct(go_prefix = ctx.attr.prefix) + return struct(go_prefix = ctx.attr.prefix, prefix_root = ctx.attr.prefix_root) _go_prefix_rule = rule( _go_prefix_impl, attrs = { "prefix": attr.string(mandatory = True), + "prefix_root": attr.string(default = ""), }, ) -def go_prefix(prefix): - """go_prefix sets the Go import name to be used for this workspace.""" +def go_prefix(prefix, prefix_root=""): + """ + go_prefix sets the Go import name to be used for this workspace. + prefix_root (optional) - subdirectory within the workspace in which go code is located + """ _go_prefix_rule(name = "go_prefix", prefix = prefix, + prefix_root = prefix_root, visibility = ["//visibility:public" ] )