workspace: support workspace-relative embeds in rules_go
rules_go constrains itself by default to includes from the same
directory or its subdirectories, just like the standard Go compiler.
With Bazel however including things from other packages is very common.
Aliases don't help as Bazel doesn't actually copy the artifacts for
efficiency.
This patches rules_go to also accept Bazel-style embeds.
Change-Id: I8fc9479492da00e463297e11b99ff2a9b88bbfde
Reviewed-on: https://review.monogon.dev/c/monogon/+/820
Tested-by: Jenkins CI
Reviewed-by: Mateusz Zalega <mateusz@monogon.tech>
diff --git a/WORKSPACE b/WORKSPACE
index 222586b..6a47504 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -31,6 +31,10 @@
http_archive(
name = "io_bazel_rules_go",
sha256 = "685052b498b6ddfe562ca7a97736741d87916fe536623afb7da2824c0211c369",
+ patch_args = ["-p1"],
+ patches = [
+ "//third_party/go/patches:rules_go_absolute_embedsrc.patch",
+ ],
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.33.0/rules_go-v0.33.0.zip",
diff --git a/third_party/go/patches/rules_go_absolute_embedsrc.patch b/third_party/go/patches/rules_go_absolute_embedsrc.patch
new file mode 100644
index 0000000..e6c0609
--- /dev/null
+++ b/third_party/go/patches/rules_go_absolute_embedsrc.patch
@@ -0,0 +1,20 @@
+This adds support for workspace-relative Go embeds.
+
+diff --git a/go/tools/builders/compilepkg.go b/go/tools/builders/compilepkg.go
+index 09e3ef6e..4623f803 100644
+--- a/go/tools/builders/compilepkg.go
++++ b/go/tools/builders/compilepkg.go
+@@ -379,6 +379,13 @@ func compileArchive(
+ }
+ }
+ }
++ // Sort by length descenting to not get wrong roots
++ sort.Slice(embedRoots, func(i, j int) bool {
++ return len(embedRoots[i]) > len(embedRoots[j])
++ })
++ for _, root := range embedRoots {
++ embedRootDirs = append(embedRootDirs, abs(root))
++ }
+ embedcfgPath, err := buildEmbedcfgFile(srcs.goSrcs, embedSrcs, embedRootDirs, workDir)
+ if err != nil {
+ return err