Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Add ability to override the source-package in mockgen #531

Closed
@choo-stripe

Description

@choo-stripe

Requested feature A clear description of the desired feature and an example of
how it would be used.

There should be a way to specify the source-package such that mockgens requiring structs from the source file can succeed outside of a standard go environment.

For example, I should be able to pull a mockgen binary, and in a fresh docker container run:

$ workdir=$(mktemp)
$ cd $workdir
$ wget my_src.go
$ env - mockgen -source=my_src.go

Today, that'd fail.

Why the feature is needed A clear description of how this feature is not
served by existing functionality in gomock.

My end goal is to write a bazel rule that can mockgen for grpc clients. The great part about this is - as the proto schemas change, bazel will notice the dependency changed and re-run mockgen, ensuring the mock and source stay in-sync.

Currently mockgen uses standard go tooling to find the package of the source file submitted - go modules or poking around in GOPATH. This makes it incompatible with build systems like bazel, which run their own go toolchain to compile go files. Bazel executes builds in isolated build environments, which don't necessarily have access to go modules or GOPATH.

Now, mockgen is a tool. While it does go-like things i.e. codegen, the binary should be portable and able to execute outside of a go environment. The problem, obviously, is that mockgen can't really know the full package path for some arbitrary source file you give it, hence it relies on the go env.

So! If only we could override the package where a source file comes from, it wouldn't require a full-blown go env setup (also the behaviour is a bit too magical right now), and we'd be able to portably call mockgen -source=foo.go -source_package=my/foo, such that the generated mock would properly import from my/foo.

(Optional) Proposed solution A clear description of a proposed method for
adding this feature to gomock.

I wrote up a PR #529

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions