Skip to content

Commit

Permalink
go_path: support go:embed of generated files (#3285)
Browse files Browse the repository at this point in the history
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'
  • Loading branch information
S-Chan authored Nov 24, 2022
1 parent 1408983 commit 9c8d83b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go/private/tools/path.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
10 changes: 10 additions & 0 deletions tests/core/go_path/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -49,6 +58,7 @@ go_test(
":archive_path",
":copy_path",
":link_path",
":transition_path",
":nodata_path",
":notransitive_path",
],
Expand Down
11 changes: 11 additions & 0 deletions tests/core/go_path/cmd/bin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
8 changes: 8 additions & 0 deletions tests/core/go_path/pkg/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -10,13 +11,20 @@ go_library(
],
embedsrcs = [
"embedded_src.txt",
"renamed_embedded_src.txt",
"template/index.html.tmpl",
],
importpath = "example.com/repo/pkg/lib",
visibility = ["//visibility:public"],
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 = [
Expand Down
3 changes: 3 additions & 0 deletions tests/core/go_path/pkg/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c8d83b

Please sign in to comment.