Skip to content

Commit

Permalink
Expand locations in x_defs values (bazelbuild#3473)
Browse files Browse the repository at this point in the history
Allows embedding rlocationpaths into binaries to allow them to find
dependencies at runtime even when run from other tools, meaning that
`args` and `env` can't be used for this purpose.
  • Loading branch information
fmeum authored and jacqueline.lee committed Jul 19, 2023
1 parent 7ed4e46 commit b1a3707
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/go/core/defines_and_stamping.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ value. You can also override stamp values from libraries using `x_defs`
on the `go_binary` rule if needed. The `--[no]stamp` option controls whether
stamping of workspace variables is enabled.

The values of the `x_defs` dictionary are subject to
[location expansion](https://bazel.build/reference/be/make-variables#predefined_label_variables).

**Example**

Suppose we have a small library that contains the current version.
Expand Down
4 changes: 4 additions & 0 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def _library_to_source(go, attr, library, coverage_instrumented):
source["deps"] = _dedup_deps(source["deps"])
x_defs = source["x_defs"]
for k, v in getattr(attr, "x_defs", {}).items():
v = _expand_location(go, attr, v)
if "." not in k:
k = "{}.{}".format(library.importmap, k)
x_defs[k] = v
Expand Down Expand Up @@ -879,6 +880,9 @@ go_config = rule(
def _expand_opts(go, attribute_name, opts):
return [go._ctx.expand_make_variables(attribute_name, opt, {}) for opt in opts]

def _expand_location(go, attr, s):
return go._ctx.expand_location(s, getattr(attr, "data", []))

_LIST_TYPE = type([])

# Used to get attribute values which may have been transitioned.
Expand Down
23 changes: 23 additions & 0 deletions tests/core/go_test/x_defs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ go_library(
visibility = ["//visibility:public"],
x_defs = {"Qux": "Qux"},
)

go_library(
name = "x_defs_lib",
srcs = ["x_defs_lib.go"],
data = ["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)",
},
)

go_test(
name = "x_defs_test",
srcs = ["x_defs_test.go"],
data = ["x_defs_test.go"],
x_defs = {
"BinGo": "$(rlocationpath x_defs_test.go)",
},
deps = [
":x_defs_lib",
"//go/runfiles",
],
)
3 changes: 3 additions & 0 deletions tests/core/go_test/x_defs/x_defs_lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package x_defs_lib

var LibGo = "not set"
34 changes: 34 additions & 0 deletions tests/core/go_test/x_defs/x_defs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package x_defs_lib_test

import (
"os"
"testing"

"github.com/bazelbuild/rules_go/go/runfiles"

"github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/x_defs_lib"
)

var BinGo = "not set"

func TestLibGoPath(t *testing.T) {
libGoPath, err := runfiles.Rlocation(x_defs_lib.LibGo)
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(libGoPath)
if err != nil {
t.Fatal(err)
}
}

func TestBinGoPath(t *testing.T) {
binGoPath, err := runfiles.Rlocation(BinGo)
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(binGoPath)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit b1a3707

Please sign in to comment.