[bazel] Gazelle-generated build targets for //bazel/...

Shifted a few files around to make their corresponding build targets more amenable to Gazelle's conventions.

Bug: skia:11110
Change-Id: Ieafb9f3aff4141fac6488cecc41ea16d34136dac
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/353699
Reviewed-by: Leandro Lovisolo <lovisolo@google.com>
Commit-Queue: Leandro Lovisolo <lovisolo@google.com>
Auto-Submit: Leandro Lovisolo <lovisolo@google.com>
diff --git a/Makefile b/Makefile
index 04783bd..67e7bb6 100644
--- a/Makefile
+++ b/Makefile
@@ -148,10 +148,14 @@
 	cd status && $(MAKE) test-frontend-ci
 
 # Directories with Go code that is built using Gazelle-generated BUILD files.
+#
+# Eventually this should be replaced with just the root directory, i.e. we should automatically
+# generate build targets for all Go code in our repository.
 GAZELLE_DIRS=\
 	./go \
 	./machine \
 	./task_scheduler \
+	./bazel \
 
 .PHONY: update-go-bazel-files
 update-go-bazel-files:
diff --git a/bazel/test_on_env/BUILD.bazel b/bazel/test_on_env/BUILD.bazel
index 1c3b75e..62ac947 100644
--- a/bazel/test_on_env/BUILD.bazel
+++ b/bazel/test_on_env/BUILD.bazel
@@ -1,5 +1,3 @@
-load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
-load("//infra-sk:index.bzl", "nodejs_mocha_test")
 load(":test_on_env.bzl", "test_on_env")
 
 exports_files(
@@ -7,39 +5,14 @@
     visibility = ["//visibility:public"],
 )
 
-go_binary(
-    name = "example_env",
-    srcs = ["example_env.go"],
-)
-
-go_test(
-    name = "example_go_test",
-    srcs = [
-        "example_env.go",
-        "example_test.go",
-    ],
-    tags = ["manual"],
-    deps = ["//go/testutils/unittest"],
-)
-
-nodejs_mocha_test(
-    name = "example_nodejs_test",
-    srcs = ["example_test.ts"],
-    tags = ["manual"],
-    deps = [
-        "@infra-sk_npm//@types/puppeteer",
-        "@infra-sk_npm//puppeteer",
-    ],
-)
-
 test_on_env(
     name = "test_on_env_go_test",
-    env = ":example_env",
-    test = ":example_go_test",
+    env = "//bazel/test_on_env/examples:env",
+    test = "//bazel/test_on_env/examples/go:go_test",
 )
 
 test_on_env(
     name = "test_on_env_nodejs_test",
-    env = ":example_env",
-    test = ":example_nodejs_test",
+    env = "//bazel/test_on_env/examples:env",
+    test = "//bazel/test_on_env/examples/typescript:typescript_test",
 )
diff --git a/bazel/test_on_env/examples/BUILD.bazel b/bazel/test_on_env/examples/BUILD.bazel
new file mode 100644
index 0000000..0396920
--- /dev/null
+++ b/bazel/test_on_env/examples/BUILD.bazel
@@ -0,0 +1,14 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+
+go_library(
+    name = "env_lib",
+    srcs = ["env.go"],
+    importpath = "go.skia.org/infra/bazel/test_on_env/examples",
+    visibility = ["//visibility:private"],
+)
+
+go_binary(
+    name = "env",
+    embed = [":env_lib"],
+    visibility = ["//visibility:public"],
+)
diff --git a/bazel/test_on_env/example_env.go b/bazel/test_on_env/examples/env.go
similarity index 100%
rename from bazel/test_on_env/example_env.go
rename to bazel/test_on_env/examples/env.go
diff --git a/bazel/test_on_env/examples/go/BUILD.bazel b/bazel/test_on_env/examples/go/BUILD.bazel
new file mode 100644
index 0000000..dcd239e
--- /dev/null
+++ b/bazel/test_on_env/examples/go/BUILD.bazel
@@ -0,0 +1,9 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_test")
+
+go_test(
+    name = "go_test",
+    srcs = ["go_test.go"],
+    tags = ["manual"],
+    visibility = ["//bazel/test_on_env:__pkg__"],
+    deps = ["//go/testutils/unittest"],
+)
diff --git a/bazel/test_on_env/example_test.go b/bazel/test_on_env/examples/go/go_test.go
similarity index 97%
rename from bazel/test_on_env/example_test.go
rename to bazel/test_on_env/examples/go/go_test.go
index 91a5f08..0616f03 100644
--- a/bazel/test_on_env/example_test.go
+++ b/bazel/test_on_env/examples/go/go_test.go
@@ -13,6 +13,8 @@
 	"go.skia.org/infra/go/testutils/unittest"
 )
 
+const envPortFileBaseName = "port"
+
 // mustReadPort reads environment server's TCP port from $ENV_DIR/port.
 func mustReadPort() int {
 	envDir := os.Getenv("ENV_DIR")
diff --git a/bazel/test_on_env/examples/typescript/BUILD.bazel b/bazel/test_on_env/examples/typescript/BUILD.bazel
new file mode 100644
index 0000000..187bd31
--- /dev/null
+++ b/bazel/test_on_env/examples/typescript/BUILD.bazel
@@ -0,0 +1,12 @@
+load("//infra-sk:index.bzl", "nodejs_mocha_test")
+
+nodejs_mocha_test(
+    name = "typescript_test",
+    srcs = ["typescript_test.ts"],
+    tags = ["manual"],
+    visibility = ["//bazel/test_on_env:__pkg__"],
+    deps = [
+        "@infra-sk_npm//@types/puppeteer",
+        "@infra-sk_npm//puppeteer",
+    ],
+)
diff --git a/bazel/test_on_env/example_test.ts b/bazel/test_on_env/examples/typescript/typescript_test.ts
similarity index 100%
rename from bazel/test_on_env/example_test.ts
rename to bazel/test_on_env/examples/typescript/typescript_test.ts
diff --git a/infra-sk/index.bzl b/infra-sk/index.bzl
index 32dfe83..5486567 100644
--- a/infra-sk/index.bzl
+++ b/infra-sk/index.bzl
@@ -9,7 +9,7 @@
 load("//infra-sk/html_insert_nonce_attribute:index.bzl", "html_insert_nonce_attribute")
 load("//bazel/test_on_env:test_on_env.bzl", "test_on_env")
 
-def nodejs_mocha_test(name, srcs = [], deps = [], tags = [], args = None):
+def nodejs_mocha_test(name, srcs = [], deps = [], tags = [], args = None, visibility = None):
     """Runs a NodeJS unit test using the Mocha test runner.
 
     For tests that should run in the browser, please use karma_mocha_test instead.
@@ -20,6 +20,7 @@
       deps: Any ts_library dependencies.
       tags: Tags for the generated nodjs_test rule.
       args: Additional command-line arguments for the mocha test runner.
+      visibility: Visibility of the generated nodejs_test rule.
     """
     if args == None:
         args = ["$(rootpath %s)" % src for src in srcs]
@@ -42,6 +43,7 @@
             "--colors",
         ] + args,
         tags = tags,
+        visibility = visibility,
     )
 
 def sk_element_puppeteer_test(name, srcs, sk_demo_page_server, deps = []):