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/osbase/fat32/BUILD.bazel b/osbase/fat32/BUILD.bazel
index d9aec3d..80a6e03 100644
--- a/osbase/fat32/BUILD.bazel
+++ b/osbase/fat32/BUILD.bazel
@@ -20,8 +20,13 @@
         "linux_test.go",
         "structs_test.go",
     ],
-    data = ["@com_github_dosfstools_dosfstools//:fsck"],
+    data = [
+        "@com_github_dosfstools_dosfstools//:fsck",
+    ],
     embed = [":fat32"],
+    x_defs = {
+        "xFsckPath": "$(rlocationpath @com_github_dosfstools_dosfstools//:fsck )",
+    },
     deps = [
         "@com_github_stretchr_testify//assert",
         "@com_github_stretchr_testify//require",
diff --git a/osbase/fat32/fsck_test.go b/osbase/fat32/fsck_test.go
index 27de542..dde3978 100644
--- a/osbase/fat32/fsck_test.go
+++ b/osbase/fat32/fsck_test.go
@@ -11,12 +11,31 @@
 	"github.com/bazelbuild/rules_go/go/runfiles"
 )
 
+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.
+	xFsckPath string
+)
+
+func init() {
+	if os.Getenv("IN_KTEST") == "true" {
+		return
+	}
+
+	var err error
+	for _, path := range []*string{
+		&xFsckPath,
+	} {
+		*path, err = runfiles.Rlocation(*path)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 func testWithFsck(t *testing.T, rootInode Inode, opts Options) {
 	t.Helper()
-	fsckPath, err := runfiles.Rlocation("com_github_dosfstools_dosfstools/fsck")
-	if err != nil {
-		t.Fatalf("unable to get path to fsck: %v", err)
-	}
 	testFile, err := os.CreateTemp("", "fat32-fsck-test")
 	if err != nil {
 		t.Fatal(err)
@@ -29,7 +48,7 @@
 	// as well as perform deep verification (-V)
 	// If the file system is OK (i.e. fsck does not want to fix it) it returns
 	// 0, otherwise 1.
-	fsckCmd := exec.Command(fsckPath, "-n", "-S", "-V", testFile.Name())
+	fsckCmd := exec.Command(xFsckPath, "-n", "-S", "-V", testFile.Name())
 	result, err := fsckCmd.CombinedOutput()
 	if err != nil {
 		t.Errorf("fsck failed: %v", string(result))