Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aux_files incorrect behavior in source gomocks #3752

Closed
ikavalio opened this issue Nov 14, 2023 · 2 comments · Fixed by #3753
Closed

aux_files incorrect behavior in source gomocks #3752

ikavalio opened this issue Nov 14, 2023 · 2 comments · Fixed by #3753

Comments

@ikavalio
Copy link
Contributor

ikavalio commented Nov 14, 2023

What version of rules_go are you using?

0.42.0

What version of gazelle are you using?

0.32.0

What version of Bazel are you using?

6.4.0

Does this issue reproduce with the latest releases of all the above?

yup!

What operating system and processor architecture are you using?

darwin/arm64, but I can reproduce it on linux/amd64 too

Any other potentially useful information about your toolchain?

the issue can be reproduced on the go toolchain with cgo/cross-compilation disabled

What did you do?

If we add the following code to the gomock rule test suite:

# client_wrapper.go

package client

type ClientWrapper interface {
	Client
	Close() error
}
# client_test.go

package client

var _ Client = (*MockClient)(nil)
var _ ClientWrapper = (*MockClientWrapper)(nil)
# BUILD.bazel

gomock(
    name = "wrapper_mocks",
    out = "wrapper_mock.go",
    library = ":client",
    package = "client",
    self_package = "github.com/bazelbuild/rules_go/gomock/client",
    source = "client_wrapper.go",
    visibility = ["//visibility:public"],
)

# + use the wrapper_mock.go in the test

The bazel build //tests/extras/gomock:wrapper_mocks will legitimately fail with

2023/11/14 13:43:23 Loading input failed: bazel-out/darwin_arm64-fastbuild/bin/tests/extras/gomock/gopath/src/github.com/bazelbuild/rules_go/gomock/client/client_wrapper.go:4:2: could not parse package github.com/bazelbuild/rules_go/gomock/client: go/build: go list github.com/bazelbuild/rules_go/gomock/client: fork/exec GOROOT/bin/go: no such file or directory

mockgen needs the client.go in the bazel's gopath and the only method to do it hermetically right now is to use aux_files like this:

# BUILD.bazel

gomock(
    name = "wrapper_mocks",
    out = "wrapper_mock.go",
    aux_files = {
        "client.go": "github.com/bazelbuild/rules_go/gomock/client",
    },
    library = ":client",
    package = "client",
    self_package = "github.com/bazelbuild/rules_go/gomock/client",
    source = "client_wrapper.go",
    visibility = ["//visibility:public"],
)

Which is what I'm currently trying to do.

What did you expect to see?

bazel build //tests/extras/gomock:wrapper_mocks works and bazel test //tests/extras/gomock:client_test passes, when the changes above are applied.

What did you see instead?

2023/11/14 13:44:48 Loading input failed: open bazel-out/darwin_arm64-fastbuild/bin/tests/extras/gomock/bazel-out/darwin_arm64-fastbuild/bin/tests/extras/gomock/gopath/src/github.com/bazelbuild/rules_go/gomock/client/client.go: no such file or directory

Further details

I think the following patch to the gomock rule fixes the issue:

diff --git a/extras/gomock.bzl b/extras/gomock.bzl
index 3046c9f1..21149c6a 100644
--- a/extras/gomock.bzl
+++ b/extras/gomock.bzl
@@ -55,7 +55,7 @@ def _gomock_source_impl(ctx):
         aux_files = []
         for target, pkg in ctx.attr.aux_files.items():
             f = target.files.to_list()[0]
-            aux = ctx.actions.declare_file(paths.join(gopath, "src", pkg, f.basename))
+            aux = ctx.actions.declare_file(paths.join("gopath", "src", pkg, f.basename))
             ctx.actions.run_shell(
                 outputs = [aux],
                 inputs = [f],
@@ -63,6 +63,7 @@ def _gomock_source_impl(ctx):
             )
             aux_files.append("{0}={1}".format(pkg, aux.path))
             needed_files.append(f)
+            needed_files.append(aux)
         args += ["-aux_files", ",".join(aux_files)]
 
     inputs = (

I can convert it to the PR if it looks alright.

ikavalio added a commit to ikavalio/rules_go that referenced this issue Nov 14, 2023
@ikavalio
Copy link
Contributor Author

ikavalio commented Nov 14, 2023

I'm sorry for combining two different things into a single issue:

  • aux_files path issue (because bazel-out/darwin_arm64-fastbuild/bin/tests/extras/gomock/bazel-out/darwin_arm64-fastbuild/bin/tests/extras/gomock/gopath/src/github.com/bazelbuild/rules_go/gomock/client/client.go is definitely an incorrect path)
  • the absence of other methods to pass source file dependencies to the gomock

just as a side note, the issue is still valid for https://github.com/uber-go/mock.

@ikavalio
Copy link
Contributor Author

I submitted a pr #3753 for the diff above, but feel free to reject it if it makes no sense.

fmeum pushed a commit that referenced this issue Dec 12, 2023
…3753)

* fix aux_files relative paths for gomock source mocks (fix #3752)

* explicitly set GOROOT for the gomock run and disable modules

* remove f from the needed_files in the gomock implementation

* refer to the "gopath" string via the variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant