From 09b89494836382a2bcddeac9d38fa6314cf98f34 Mon Sep 17 00:00:00 2001 From: Son Luong Ngoc Date: Tue, 6 Feb 2024 13:57:40 +0100 Subject: [PATCH 1/2] go_test: ensure external source compilation has data go_test is split into 2 compilation actions, one for internal test, where package name of the test is the same with library package. ```bash package foo package foo ``` and external test where the package name is different. ```bash package foo package foo_test ``` Ensure that the compilation for external test has access to go_test's data attribute. --- go/private/rules/test.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl index 1283776a95..2eb5094102 100644 --- a/go/private/rules/test.bzl +++ b/go/private/rules/test.bzl @@ -74,6 +74,7 @@ def _go_test_impl(ctx): ) external_source = go.library_to_source(go, struct( srcs = [struct(files = go_srcs)], + data = ctx.attr.data, embedsrcs = [struct(files = internal_source.embedsrcs)], deps = internal_archive.direct + [internal_archive], x_defs = ctx.attr.x_defs, From e92d04430f629ba4129acd245f54114fa3ab6437 Mon Sep 17 00:00:00 2001 From: Son Luong Ngoc Date: Tue, 6 Feb 2024 14:04:58 +0100 Subject: [PATCH 2/2] add test --- tests/core/go_test/x_defs/BUILD.bazel | 18 ++++++++++++++++-- tests/core/go_test/x_defs/x_defs_lib.go | 2 ++ tests/core/go_test/x_defs/x_defs_test.go | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tests/core/go_test/x_defs/BUILD.bazel b/tests/core/go_test/x_defs/BUILD.bazel index 7edc48853e..5d86aac387 100644 --- a/tests/core/go_test/x_defs/BUILD.bazel +++ b/tests/core/go_test/x_defs/BUILD.bazel @@ -49,22 +49,36 @@ go_library( x_defs = {"Qux": "Qux"}, ) +genrule( + name = "data_dep", + outs = ["data_dep.txt"], + cmd = "touch $@", +) + go_library( name = "x_defs_lib", srcs = ["x_defs_lib.go"], - data = ["x_defs_lib.go"], + data = [ + "data_dep.txt", + "x_defs_lib.go", + ], importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/x_defs_lib", x_defs = { "LibGo": "$(rlocationpath x_defs_lib.go)", + "DataDep": "$(rlocationpath :data_dep.txt)", }, ) go_test( name = "x_defs_test", srcs = ["x_defs_test.go"], - data = ["x_defs_test.go"], + data = [ + "data_dep.txt", + "x_defs_test.go", + ], x_defs = { "BinGo": "$(rlocationpath x_defs_test.go)", + "DataDep": "$(rlocationpath :data_dep.txt)", }, deps = [ ":x_defs_lib", diff --git a/tests/core/go_test/x_defs/x_defs_lib.go b/tests/core/go_test/x_defs/x_defs_lib.go index e7794b33cf..811f51e293 100644 --- a/tests/core/go_test/x_defs/x_defs_lib.go +++ b/tests/core/go_test/x_defs/x_defs_lib.go @@ -1,3 +1,5 @@ package x_defs_lib var LibGo = "not set" + +var DataDep = "not set" diff --git a/tests/core/go_test/x_defs/x_defs_test.go b/tests/core/go_test/x_defs/x_defs_test.go index 0832c2478c..35b7ba956a 100644 --- a/tests/core/go_test/x_defs/x_defs_test.go +++ b/tests/core/go_test/x_defs/x_defs_test.go @@ -11,6 +11,8 @@ import ( var BinGo = "not set" +var DataDep = "not set" + func TestLibGoPath(t *testing.T) { libGoPath, err := runfiles.Rlocation(x_defs_lib.LibGo) if err != nil { @@ -20,6 +22,15 @@ func TestLibGoPath(t *testing.T) { if err != nil { t.Fatal(err) } + + dataPath, err := runfiles.Rlocation(x_defs_lib.DataDep) + if err != nil { + t.Fatal(err) + } + _, err = os.Stat(dataPath) + if err != nil { + t.Fatal(err) + } } func TestBinGoPath(t *testing.T) { @@ -31,4 +42,13 @@ func TestBinGoPath(t *testing.T) { if err != nil { t.Fatal(err) } + + dataPath, err := runfiles.Rlocation(DataDep) + if err != nil { + t.Fatal(err) + } + _, err = os.Stat(dataPath) + if err != nil { + t.Fatal(err) + } }