treewide: replace hardcoded runfiles paths

We hardcoded some of the runfiles paths to find specific files. This replaces the hardcoded paths by a call to rlocationpath. This prevents running a target without the correct dependencies at build time instead of at runtime

Change-Id: I7ce56935ac80be6b28b824ccb0781ab401bd6521
Reviewed-on: https://review.monogon.dev/c/monogon/+/3301
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/metropolis/node/core/metrics/BUILD.bazel b/metropolis/node/core/metrics/BUILD.bazel
index 30d08a7..9383afb 100644
--- a/metropolis/node/core/metrics/BUILD.bazel
+++ b/metropolis/node/core/metrics/BUILD.bazel
@@ -25,10 +25,12 @@
     name = "metrics_test",
     srcs = ["metrics_test.go"],
     data = [
-        # keep
         "//metropolis/node/core/metrics/fake_exporter",
     ],
     embed = [":metrics"],
+    x_defs = {
+        "xFakeExporterPath": "$(rlocationpath //metropolis/node/core/metrics/fake_exporter )",
+    },
     deps = [
         "//metropolis/node",
         "//metropolis/node/core/curator/proto/api",
diff --git a/metropolis/node/core/metrics/metrics_test.go b/metropolis/node/core/metrics/metrics_test.go
index 06b1d4e..fb5e3dc 100644
--- a/metropolis/node/core/metrics/metrics_test.go
+++ b/metropolis/node/core/metrics/metrics_test.go
@@ -23,9 +23,26 @@
 	"source.monogon.dev/osbase/supervisor"
 )
 
-func fakeExporter(name, value string) *Exporter {
-	path, _ := runfiles.Rlocation("_main/metropolis/node/core/metrics/fake_exporter/fake_exporter_/fake_exporter")
+var (
+	// These are filled by bazel at linking time with the canonical path of
+	// their corresponding file. Inside the init function we resolve it
+	// with the rules_go runfiles package to the real path.
+	xFakeExporterPath string
+)
 
+func init() {
+	var err error
+	for _, path := range []*string{
+		&xFakeExporterPath,
+	} {
+		*path, err = runfiles.Rlocation(*path)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
+func fakeExporter(name, value string) *Exporter {
 	p, closer, err := freeport.AllocateTCPPort()
 	if err != nil {
 		panic(err)
@@ -36,7 +53,7 @@
 	return &Exporter{
 		Name:       name,
 		Port:       port,
-		Executable: path,
+		Executable: xFakeExporterPath,
 		Arguments: []string{
 			"-listen", "127.0.0.1:" + port.PortString(),
 			"-value", value,