jmhodges / bazel_gomock

Code to create Go mocks for bazel targets using mockgen
MIT License
58 stars 30 forks source link

running gazelle breaks BUILD file containing gomock rules #51

Open symbiont-ji opened 3 years ago

symbiont-ji commented 3 years ago

Let me qualify "breaks":

At master (dd57d5599d64ebde72aff1c91df41ef32baece51):

Test everything works:

$ bazel test //...
INFO: Analyzed 14 targets (64 packages loaded, 7078 targets configured).
INFO: Found 13 targets and 1 test target...
INFO: Elapsed time: 4.534s, Critical Path: 3.45s
INFO: 75 processes: 14 internal, 61 linux-sandbox.
INFO: Build completed successfully, 75 total actions
//tests:go_default_test                                                  PASSED in 0.1s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 75 total actions

As does, of course:

$ bazel test //tests:go_default_test
INFO: Analyzed target //tests:go_default_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
Target //tests:go_default_test up-to-date:
  bazel-bin/tests/go_default_test_/go_default_test
INFO: Elapsed time: 0.084s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
//tests:go_default_test                                         (cached) PASSED in 0.1s

Executed 0 out of 1 test: 1 test passes.
INFO: Build completed successfully, 1 total action

Now, run gazelle:

$ bazel run //:gazelle -- update
INFO: Analyzed target //:gazelle (1 packages loaded, 2 targets configured).
INFO: Found 1 target...
Target //:gazelle up-to-date:
  bazel-bin/gazelle-runner.bash
  bazel-bin/gazelle
INFO: Elapsed time: 0.244s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   tests/BUILD.bazel

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel
index 9dad7cd..017bf1b 100644
--- a/tests/BUILD.bazel
+++ b/tests/BUILD.bazel
@@ -46,8 +46,8 @@ go_test(
     srcs = [
         "gomock_test.go",
         ":helloer_reflect_mock",
-        ":helloer_source_mock",
         ":helloer_reflect_mock_with_mock_names",
+        ":helloer_source_mock",
     ],
     args = [
         "-srcWithCopyright=$(location :helloer_mock_with_copyright)",
@@ -59,7 +59,25 @@ go_test(
     ],
     deps = [
         "@com_github_golang_mock//gomock:go_default_library",
-        "@io_bazel_rules_go//go/tools/bazel:go_default_library",
         "@com_github_google_go_cmp//cmp:go_default_library",
+        "@io_bazel_rules_go//go/tools/bazel:go_default_library",
+    ],
+)
+
+go_library(
+    name = "go_default_library",
+    srcs = [
+        "hello_mock.go",
+        "hello_mock_renamed.go",
+        "helloer_copyright.go",
+        "helloer_source_mock.go",
     ],
+    importpath = "github.com/jmhodges/bazel_gomock/tests",
+    visibility = ["//visibility:private"],
+)
+
+go_binary(
+    name = "tests",
+    embed = [":go_default_library"],
+    visibility = ["//visibility:public"],
 )

Now, while bazel test //tests: succeeds:

$ bazel test //tests:go_default_test
INFO: Analyzed target //tests:go_default_test (2 packages loaded, 22 targets configured).
INFO: Found 1 test target...
Target //tests:go_default_test up-to-date:
  bazel-bin/tests/go_default_test_/go_default_test
INFO: Elapsed time: 0.831s, Critical Path: 0.70s
INFO: 7 processes: 1 internal, 6 linux-sandbox.
INFO: Build completed successfully, 7 total actions
//tests:go_default_test                                                  PASSED in 0.1s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 7 total actions

The additional stuff that gazelle put (that it should not have put) in the BUILD file causes the global tests to fail:

$ bazel test //...
INFO: Analyzed 16 targets (27 packages loaded, 301 targets configured).
INFO: Found 15 targets and 1 test target...
ERROR: /home/ji/bazel_gomock/tests/BUILD.bazel:67:11: GoCompilePkg tests/go_default_library.a failed (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix linux_amd64 -src bazel-out/k8-fastbuild/bin/tests/hello_mock.go -src ... (remaining 17 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix linux_amd64 -src bazel-out/k8-fastbuild/bin/tests/hello_mock.go -src ... (remaining 17 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
compilepkg: missing strict dependencies:
        /home/ji/.cache/bazel/_bazel_ji/7a27ddd7c8683af35cb30bc7964830b9/sandbox/linux-sandbox/35/execroot/com_github_jmhodges_bazel_gomock/bazel-out/k8-fastbuild/bin/tests/hello_mock.go: import of "github.com/golang/mock/gomock"
        /home/ji/.cache/bazel/_bazel_ji/7a27ddd7c8683af35cb30bc7964830b9/sandbox/linux-sandbox/35/execroot/com_github_jmhodges_bazel_gomock/bazel-out/k8-fastbuild/bin/tests/hello_mock_renamed.go: import of "github.com/golang/mock/gomock"
        /home/ji/.cache/bazel/_bazel_ji/7a27ddd7c8683af35cb30bc7964830b9/sandbox/linux-sandbox/35/execroot/com_github_jmhodges_bazel_gomock/bazel-out/k8-fastbuild/bin/tests/helloer_copyright.go: import of "github.com/golang/mock/gomock"
        /home/ji/.cache/bazel/_bazel_ji/7a27ddd7c8683af35cb30bc7964830b9/sandbox/linux-sandbox/35/execroot/com_github_jmhodges_bazel_gomock/bazel-out/k8-fastbuild/bin/tests/helloer_source_mock.go: import of "github.com/golang/mock/gomock"
No dependencies were provided.
Check that imports in Go sources match importpath attributes in deps.
INFO: Elapsed time: 0.599s, Critical Path: 0.14s
INFO: 7 processes: 5 internal, 2 linux-sandbox.
FAILED: Build did NOT complete successfully
//tests:go_default_test                                               NO STATUS

FAILED: Build did NOT complete successfully
bizrad commented 2 years ago

If you don't want gazelle to modify build files you can use keep directives