Processes runfiles for libraries in addition to binaries. The behavior
now matches the documented API.

--
Change-Id: I05738cc7fc60164e19e4c13822b37d032155cb06
Reviewed-on: https://bazel-review.git.corp.google.com/#/c/2380
MOS_MIGRATED_REVID=109099720
diff --git a/src/test/shell/bazel/bazel_go_example_test.sh b/src/test/shell/bazel/bazel_go_example_test.sh
index 8183414..fb5fa10 100755
--- a/src/test/shell/bazel/bazel_go_example_test.sh
+++ b/src/test/shell/bazel/bazel_go_example_test.sh
@@ -151,7 +151,53 @@
   test -x ./bazel-bin/ex/runfiles_test || fail "binary not found"
   (./bazel-bin/ex/runfiles_test > out) || fail "binary does not execute"
   grep "Runfile: 12345" out || fail "binary output suspect"
+}
 
+function test_runfiles_lib() {
+    mkdir -p ex/
+    cat <<EOF > ex/m.go
+package main
+import (
+  "fmt"
+  "io/ioutil"
+  "log"
+
+  "prefix/ex"
+)
+func main() {
+  rfcontent, err := ioutil.ReadFile(ex.RunfilePath())
+  if err != nil {
+    log.Fatalf("Runfiles test binary: Error reading from runfile: %v", err)
+  }
+
+  fmt.Printf("Runfile: %s\n", rfcontent)
+}
+
+EOF
+
+    cat <<EOF > ex/l.go
+package ex
+func RunfilePath() string { return "ex/runfile" }
+EOF
+
+  cat <<EOF > ex/runfile
+12345
+EOF
+
+    cat <<EOF > ex/BUILD
+load("/tools/build_rules/go/def", "go_library", "go_binary")
+go_library(name = "go_default_library",
+  data = [ "runfile" ],
+  srcs = [ "l.go"])
+go_binary(name = "m",
+  srcs = [ "m.go" ],
+  deps = [ ":go_default_library" ])
+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 "Runfile: 12345" out || fail "binary output suspect"
 }
 
 run_suite "go_examples"
diff --git a/tools/build_rules/go/def.bzl b/tools/build_rules/go/def.bzl
index f63da7b..4c304f5 100644
--- a/tools/build_rules/go/def.bzl
+++ b/tools/build_rules/go/def.bzl
@@ -210,10 +210,12 @@
   for dep in ctx.attr.deps:
      transitive_libs += dep.transitive_go_library_object
 
+  runfiles = ctx.runfiles(collect_data = True)
   return struct(
     label = ctx.label,
     files = set([out_lib]),
     direct_deps = deps,
+    runfiles = runfiles,
     go_sources = sources,
     go_library_object = out_lib,
     transitive_go_library_object = transitive_libs)