Skip to content

Commit

Permalink
go_path: support go:embed of generated files
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 committed Sep 9, 2022
1 parent 384289e commit 5d14ccd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go/private/tools/path.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ def _go_path_impl(ctx):
dst = pkg.dir + "/" + f.basename
_add_manifest_entry(manifest_entries, manifest_entry_map, inputs, f, dst)
for f in pkg.embedsrcs:
embedpath = f.path
if src_dir == None:
fail("cannot relativize {}: src_dir is unset".format(f.path))
dst = pkg.dir + "/" + paths.relativize(f.path, src_dir)
if f.dirname.startswith(ctx.bin_dir.path):
embedpath = paths.relativize(f.path, ctx.bin_dir.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
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 5d14ccd

Please sign in to comment.