diff options
-rwxr-xr-x | src/test/shell/bazel/bazel_go_example_test.sh | 49 | ||||
-rw-r--r-- | tools/build_rules/go/README.md | 4 | ||||
-rw-r--r-- | tools/build_rules/go/def.bzl | 2 |
3 files changed, 53 insertions, 2 deletions
diff --git a/src/test/shell/bazel/bazel_go_example_test.sh b/src/test/shell/bazel/bazel_go_example_test.sh index 964c5bdae5..e3224342af 100755 --- a/src/test/shell/bazel/bazel_go_example_test.sh +++ b/src/test/shell/bazel/bazel_go_example_test.sh @@ -202,4 +202,53 @@ EOF grep "Runfile: 12345" out || fail "binary output suspect" } + +function test_empty_prefix() { + setup_go + + cat <<EOF > BUILD +load("/tools/build_rules/go/def", "go_prefix") +go_prefix("") +EOF + + rm -rf ex + mkdir -p ex/ + cat <<EOF > ex/m.go +package main +import ( + "fmt" + + "library" +) +func main() { + fmt.Println(library.F()) +} +EOF + cat <<EOF > ex/BUILD +load("/tools/build_rules/go/def", "go_library", "go_binary") +go_binary(name = "m", + srcs = [ "m.go" ], + deps = [ "//library:go_default_library" ]) +EOF + + mkdir -p library + + cat <<EOF > library/BUILD +package(default_visibility=["//visibility:public"]) +load("/tools/build_rules/go/def", "go_library", "go_binary") +go_library(name = "go_default_library", + srcs = [ "l.go"]) +EOF + + cat <<EOF > library/l.go +package library +func F() int { return 42 } +EOF + + assert_build //ex:m + test -x ./bazel-bin/ex/m || fail "binary not found" + (./bazel-bin/ex/m > out) || fail "binary does not execute" + grep "42" out || fail "binary output suspect" +} + run_suite "go_examples" diff --git a/tools/build_rules/go/README.md b/tools/build_rules/go/README.md index 347aee621a..b649fe0e33 100644 --- a/tools/build_rules/go/README.md +++ b/tools/build_rules/go/README.md @@ -39,7 +39,9 @@ They currently do not support (in order of importance): go_repositories() ``` -* Add a `BUILD` file to the top of your workspace, containing +* Add a `BUILD` file to the top of your workspace, declaring the name of your + workspace using `go_prefix`. It is strongly recommended that the prefix is not + empty. ```python load("/tools/build_rules/go/def", "go_prefix") diff --git a/tools/build_rules/go/def.bzl b/tools/build_rules/go/def.bzl index 2563e9ebd8..fc611410be 100644 --- a/tools/build_rules/go/def.bzl +++ b/tools/build_rules/go/def.bzl @@ -49,7 +49,7 @@ def _go_prefix_impl(ctx): def _go_prefix(ctx): """slash terminated go-prefix""" prefix = ctx.attr.go_prefix.go_prefix - if not prefix.endswith("/"): + if prefix != "" and not prefix.endswith("/"): prefix = prefix + "/" return prefix |