Define //:errcheck Bazel target and use it everywhere.

Bug: skia:13588
Change-Id: I9c28cfeb9f48ff893ec7bccefd7f2e6ef318866d
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/562955
Auto-Submit: Leandro Lovisolo <lovisolo@google.com>
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
Commit-Queue: Joe Gregorio <jcgregorio@google.com>
diff --git a/BUILD.bazel b/BUILD.bazel
index 2389976..ee95fb2 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -198,6 +198,24 @@
     visibility = ["//visibility:public"],
 )
 
+############
+# Errcheck #
+############
+
+# Sample usage: "bazel run //:errcheck --run_under="cd $PWD &&" -- go.skia.org/infra/...".
+#
+# Note: The "errcheck go.skia.org/infra/..." command fails when executed outside of the
+# repository's root directory. The "bazel run" command executes binaries with their working
+# directory set to their runfiles tree (see https://stackoverflow.com/a/70265855). As a workaround,
+# be sure to run Bazel from the repository's root directory and to pass --run_under="cd $PWD &&" in
+# your "bazel run" invocation (see https://bazel.build/docs/user-manual#run-under) as in the sample
+# usage above.
+alias(
+    name = "errcheck",
+    actual = "@com_github_kisielk_errcheck//:errcheck",
+    visibility = ["//visibility:public"],
+)
+
 ###########
 # Mockery #
 ###########
diff --git a/infra/bots/task_drivers/bazel_build_all/bazel_build_all.go b/infra/bots/task_drivers/bazel_build_all/bazel_build_all.go
index f6060f9..f1d99ec 100644
--- a/infra/bots/task_drivers/bazel_build_all/bazel_build_all.go
+++ b/infra/bots/task_drivers/bazel_build_all/bazel_build_all.go
@@ -2,6 +2,7 @@
 
 import (
 	"flag"
+	"fmt"
 	"path"
 
 	"go.skia.org/infra/go/exec"
@@ -71,15 +72,6 @@
 	}
 	failIfNonEmptyGitDiff()
 
-	// Run "errcheck" and fail if there are any findings.
-	//
-	// For some reason, exec.RunCwd cannot find the errcheck binary without an absolute path, which
-	// is weird because /mnt/pd0/s/w/ir/gopath/bin is included in $PATH, so we provide an absolute
-	// path to the errcheck binary.
-	if _, err := exec.RunCwd(ctx, path.Join(workDir, "repo"), path.Join(workDir, "gopath", "bin", "errcheck"), "-ignore", ":Close", "go.skia.org/infra/..."); err != nil {
-		td.Fatal(ctx, err)
-	}
-
 	// Set up Bazel.
 	var (
 		bzl        *bazel.Bazel
@@ -103,7 +95,12 @@
 		td.Fatal(ctx, err)
 	}
 
-	// Run "go fmt" and fail it there are any diffs.
+	// Run "errcheck" and fail it there are any findings.
+	if _, err := bzl.Do(ctx, "run", "//:errcheck", fmt.Sprintf("--run_under=cd %s &&", gitDir.Dir()), "--", "-ignore", ":Close", "go.skia.org/infra/..."); err != nil {
+		td.Fatal(ctx, err)
+	}
+
+	// Run "gofmt" and fail it there are any diffs.
 	if _, err := bzl.Do(ctx, "run", "//:gofmt", "--", "-s", "-w", "."); err != nil {
 		td.Fatal(ctx, err)
 	}