From ac3fefb79570725a9c19b1989a998f68374c511b Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Thu, 17 Aug 2017 12:35:35 -0400 Subject: [PATCH] Make go_prefix optional (#733) go_prefix is used to generate Go import paths from Bazel labels. Until now, it has been mandatory, since there's an implicit dependency from all go_library, go_binary, and go_test rules on //:go_prefix. With this change, that dependency is not present when the importpath attribute is specified. This means that if all of the Go rules in a repository specify importpath, there's no need to define a go_prefix rule in the repository root. Fixes #720 Related #721 --- go/private/binary.bzl | 23 +++++++++++++---------- go/private/gazelle.bzl | 14 +++++++++----- go/private/library.bzl | 7 ++++++- go/private/test.bzl | 13 ++++++++----- tests/no_prefix/BUILD | 28 ++++++++++++++++++++++++++++ tests/no_prefix/cmd.go | 6 ++++++ tests/no_prefix/no_prefix.go | 1 + tests/no_prefix/no_prefix_test.go | 3 +++ 8 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 tests/no_prefix/BUILD create mode 100644 tests/no_prefix/cmd.go create mode 100644 tests/no_prefix/no_prefix.go create mode 100644 tests/no_prefix/no_prefix_test.go diff --git a/go/private/binary.bzl b/go/private/binary.bzl index eee6567f26..ba6fc36ee8 100644 --- a/go/private/binary.bzl +++ b/go/private/binary.bzl @@ -13,13 +13,19 @@ # limitations under the License. load("@io_bazel_rules_go//go/private:common.bzl", - "get_go_toolchain", - "go_filetype", - "compile_modes", - "NORMAL_MODE", - "RACE_MODE", + "NORMAL_MODE", + "RACE_MODE", + "compile_modes", + "get_go_toolchain", + "go_filetype", +) +load("@io_bazel_rules_go//go/private:library.bzl", + "emit_library_actions", + "get_library", + "get_searchpath", + "go_importpath", + "go_prefix_default", ) -load("@io_bazel_rules_go//go/private:library.bzl", "emit_library_actions", "go_importpath", "get_library", "get_searchpath") load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary", "GoBinary") def _go_binary_impl(ctx): @@ -97,10 +103,7 @@ go_binary = rule( "x_defs": attr.string_dict(), #TODO(toolchains): Remove _toolchain attribute when real toolchains arrive "_go_toolchain": attr.label(default = Label("@io_bazel_rules_go_toolchain//:go_toolchain")), - "_go_prefix": attr.label(default = Label( - "//:go_prefix", - relative_to_caller_repository = True, - )), + "_go_prefix": attr.label(default = go_prefix_default), }, executable = True, fragments = ["cpp"], diff --git a/go/private/gazelle.bzl b/go/private/gazelle.bzl index 37267d1464..c4aeced931 100644 --- a/go/private/gazelle.bzl +++ b/go/private/gazelle.bzl @@ -20,10 +20,11 @@ $BASE/{gazelle} {args} $@ """ def _gazelle_script_impl(ctx): + prefix = ctx.attr.prefix if ctx.attr.prefix else ctx.attr._go_prefix.go_prefix args = [ctx.attr.command] + ctx.attr.args args += [ "-repo_root", "$WORKSPACE", - "-go_prefix", ctx.attr._go_prefix.go_prefix, + "-go_prefix", prefix, "-external", ctx.attr.external, "-mode", ctx.attr.mode, ] @@ -37,6 +38,11 @@ def _gazelle_script_impl(ctx): runfiles = ctx.runfiles([ctx.file._gazelle]) ) +def _go_prefix_default(prefix): + return (None + if prefix + else Label("//:go_prefix", relative_to_caller_repository = True)) + _gazelle_script = rule( _gazelle_script_impl, attrs = { @@ -45,6 +51,7 @@ _gazelle_script = rule( "external": attr.string(values=["external", "vendored"], default="external"), "build_tags": attr.string_list(), "args": attr.string_list(), + "prefix": attr.string(), "_gazelle": attr.label( default = Label("@io_bazel_rules_go//go/tools/gazelle/gazelle:gazelle"), allow_files = True, @@ -52,10 +59,7 @@ _gazelle_script = rule( executable = True, cfg = "host" ), - "_go_prefix": attr.label(default = Label( - "//:go_prefix", - relative_to_caller_repository = True, - )), + "_go_prefix": attr.label(default = _go_prefix_default), } ) diff --git a/go/private/library.bzl b/go/private/library.bzl index dc246c672c..3bb71dace1 100644 --- a/go/private/library.bzl +++ b/go/private/library.bzl @@ -161,6 +161,11 @@ def _go_library_impl(ctx): ), ] +def go_prefix_default(importpath): + return (None + if importpath + else Label("//:go_prefix", relative_to_caller_repository = True)) + go_library = rule( _go_library_impl, attrs = { @@ -178,7 +183,7 @@ go_library = rule( ), #TODO(toolchains): Remove _toolchain attribute when real toolchains arrive "_go_toolchain": attr.label(default = Label("@io_bazel_rules_go_toolchain//:go_toolchain")), - "_go_prefix": attr.label(default=Label("//:go_prefix", relative_to_caller_repository = True)), + "_go_prefix": attr.label(default = go_prefix_default), }, fragments = ["cpp"], ) diff --git a/go/private/test.bzl b/go/private/test.bzl index 83816e8ba3..2c6b476f9b 100644 --- a/go/private/test.bzl +++ b/go/private/test.bzl @@ -20,7 +20,13 @@ load("@io_bazel_rules_go//go/private:common.bzl", "NORMAL_MODE", "RACE_MODE", ) -load("@io_bazel_rules_go//go/private:library.bzl", "emit_library_actions", "go_importpath", "emit_go_compile_action", "emit_go_pack_action") +load("@io_bazel_rules_go//go/private:library.bzl", + "emit_go_compile_action", + "emit_go_pack_action", + "emit_library_actions", + "go_importpath", + "go_prefix_default", +) load("@io_bazel_rules_go//go/private:binary.bzl", "emit_go_link_action", "gc_linkopts") load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary", "GoBinary") @@ -131,10 +137,7 @@ go_test = rule( "x_defs": attr.string_dict(), #TODO(toolchains): Remove _toolchain attribute when real toolchains arrive "_go_toolchain": attr.label(default = Label("@io_bazel_rules_go_toolchain//:go_toolchain")), - "_go_prefix": attr.label(default = Label( - "//:go_prefix", - relative_to_caller_repository = True, - )), + "_go_prefix": attr.label(default = go_prefix_default), }, executable = True, fragments = ["cpp"], diff --git a/tests/no_prefix/BUILD b/tests/no_prefix/BUILD new file mode 100644 index 0000000000..336dba9653 --- /dev/null +++ b/tests/no_prefix/BUILD @@ -0,0 +1,28 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") +load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test") + +bazel_test( + name = "no_prefix", + command = "build", + target = "//:go_default_library //:go_default_xtest //:cmd", +) + +go_library( + name = "go_default_library", + srcs = ["no_prefix.go"], + importpath = "github.com/bazelbuild/rules_go/tests/no_prefix", +) + +go_test( + name = "go_default_xtest", + srcs = ["no_prefix_test.go"], + deps = [":go_default_library"], + importpath = "github.com/bazelbuild/rules_go/tests/no_prefix_test", +) + +go_binary( + name = "cmd", + srcs = ["cmd.go"], + deps = [":go_default_library"], + importpath = "github.com/bazelbuild/rules_go/tests/no_prefix/cmd", +) diff --git a/tests/no_prefix/cmd.go b/tests/no_prefix/cmd.go new file mode 100644 index 0000000000..546275b9db --- /dev/null +++ b/tests/no_prefix/cmd.go @@ -0,0 +1,6 @@ +package main + +import _ "github.com/bazelbuild/rules_go/tests/no_prefix" + +func main() { +} diff --git a/tests/no_prefix/no_prefix.go b/tests/no_prefix/no_prefix.go new file mode 100644 index 0000000000..2fc5caf3da --- /dev/null +++ b/tests/no_prefix/no_prefix.go @@ -0,0 +1 @@ +package no_prefix diff --git a/tests/no_prefix/no_prefix_test.go b/tests/no_prefix/no_prefix_test.go new file mode 100644 index 0000000000..14ba318e98 --- /dev/null +++ b/tests/no_prefix/no_prefix_test.go @@ -0,0 +1,3 @@ +package no_prefix_test + +import _ "github.com/bazelbuild/rules_go/tests/no_prefix"