Skip to content

Commit

Permalink
Make go_prefix optional (#733)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jayconrod authored Aug 17, 2017
1 parent d623f84 commit ac3fefb
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
23 changes: 13 additions & 10 deletions go/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"],
Expand Down
14 changes: 9 additions & 5 deletions go/private/gazelle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand All @@ -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 = {
Expand All @@ -45,17 +51,15 @@ _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,
single_file = True,
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),
}
)

Expand Down
7 changes: 6 additions & 1 deletion go/private/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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"],
)
Expand Down
13 changes: 8 additions & 5 deletions go/private/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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"],
Expand Down
28 changes: 28 additions & 0 deletions tests/no_prefix/BUILD
Original file line number Diff line number Diff line change
@@ -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",
)
6 changes: 6 additions & 0 deletions tests/no_prefix/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import _ "github.com/bazelbuild/rules_go/tests/no_prefix"

func main() {
}
1 change: 1 addition & 0 deletions tests/no_prefix/no_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package no_prefix
3 changes: 3 additions & 0 deletions tests/no_prefix/no_prefix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package no_prefix_test

import _ "github.com/bazelbuild/rules_go/tests/no_prefix"

0 comments on commit ac3fefb

Please sign in to comment.