From f1dab6ae80a27b7009117ec2cf3858d1e4c35f72 Mon Sep 17 00:00:00 2001 From: Stephen Chan Date: Thu, 24 Nov 2022 09:57:48 -0800 Subject: [PATCH] go_path: support go:embed of generated files (#3285) When embedded files are generated, relativize fails because the file path starts with the output directory. Without the change in path.bzl, the test case fails as follows: Error in fail: Path 'bazel-out/darwin-fastbuild/bin/tests/core/go_path/pkg/lib/renamed_embedded_src.txt' is not beneath 'tests/core/go_path/pkg/lib' --- go/private/tools/path.bzl | 3 ++- tests/core/go_path/BUILD.bazel | 10 ++++++++++ tests/core/go_path/cmd/bin/BUILD.bazel | 11 +++++++++++ tests/core/go_path/pkg/lib/BUILD.bazel | 8 ++++++++ tests/core/go_path/pkg/lib/lib.go | 3 +++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/go/private/tools/path.bzl b/go/private/tools/path.bzl index 864d794628..5ce23c2566 100644 --- a/go/private/tools/path.bzl +++ b/go/private/tools/path.bzl @@ -83,7 +83,8 @@ def _go_path_impl(ctx): for f in pkg.embedsrcs: if src_dir == None: fail("cannot relativize {}: src_dir is unset".format(f.path)) - dst = pkg.dir + "/" + paths.relativize(f.path, src_dir) + embedpath = paths.relativize(f.path, f.root.path) + dst = pkg.dir + "/" + paths.relativize(embedpath, src_dir) _add_manifest_entry(manifest_entries, manifest_entry_map, inputs, f, dst) if ctx.attr.include_pkg: for pkg in pkg_map.values(): diff --git a/tests/core/go_path/BUILD.bazel b/tests/core/go_path/BUILD.bazel index a0ea9db20f..c2c7040d30 100644 --- a/tests/core/go_path/BUILD.bazel +++ b/tests/core/go_path/BUILD.bazel @@ -18,6 +18,15 @@ test_suite(name = "go_path") ], ) for mode in ("archive", "copy", "link")] +go_path( + name = "transition_path", + testonly = True, + data = ["extra.txt"], + include_pkg = True, + mode = "copy", + deps = ["//tests/core/go_path/cmd/bin:pie"], +) + go_path( name = "nodata_path", testonly = True, @@ -49,6 +58,7 @@ go_test( ":archive_path", ":copy_path", ":link_path", + ":transition_path", ":nodata_path", ":notransitive_path", ], diff --git a/tests/core/go_path/cmd/bin/BUILD.bazel b/tests/core/go_path/cmd/bin/BUILD.bazel index d0a058d3af..65d942cdad 100644 --- a/tests/core/go_path/cmd/bin/BUILD.bazel +++ b/tests/core/go_path/cmd/bin/BUILD.bazel @@ -8,6 +8,17 @@ go_binary( visibility = ["//visibility:public"], ) +go_binary( + name = "pie", + srcs = ["bin.go"], + importpath = "example.com/repo/cmd/bin", + visibility = ["//visibility:public"], + linkmode = "pie", + deps = [ + "//tests/core/go_path/pkg/lib:go_default_library" + ] +) + go_binary( name = "cross", srcs = ["bin.go"], diff --git a/tests/core/go_path/pkg/lib/BUILD.bazel b/tests/core/go_path/pkg/lib/BUILD.bazel index 0b40e2bffd..50e00f574e 100644 --- a/tests/core/go_path/pkg/lib/BUILD.bazel +++ b/tests/core/go_path/pkg/lib/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@bazel_skylib//rules:copy_file.bzl", "copy_file") go_library( name = "go_default_library", @@ -10,6 +11,7 @@ go_library( ], embedsrcs = [ "embedded_src.txt", + "renamed_embedded_src.txt", "template/index.html.tmpl", ], importpath = "example.com/repo/pkg/lib", @@ -17,6 +19,12 @@ go_library( deps = [":transitive_lib"], ) +copy_file( + name = "rename_embedded_src.txt", + src = ":embedded_src.txt", + out = "renamed_embedded_src.txt", +) + go_test( name = "go_default_test", srcs = [ diff --git a/tests/core/go_path/pkg/lib/lib.go b/tests/core/go_path/pkg/lib/lib.go index e159d053de..1434db9ff6 100644 --- a/tests/core/go_path/pkg/lib/lib.go +++ b/tests/core/go_path/pkg/lib/lib.go @@ -8,5 +8,8 @@ import ( //go:embed embedded_src.txt var embeddedSource string +//go:embed renamed_embedded_src.txt +var renamedEmbeddedSource string + //go:embed template/index.html.tmpl var indexTmpl string