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

Avoid calling go list #419

Open
linzhp opened this issue Mar 26, 2020 · 1 comment
Open

Avoid calling go list #419

linzhp opened this issue Mar 26, 2020 · 1 comment

Comments

@linzhp
Copy link
Contributor

linzhp commented Mar 26, 2020

@plakhotnii @codyoss

go list is quite slow because it has to download external dependencies. This becomes more problematic under Bazel execution environment when GOCACHE is not available (bazel-contrib/rules_go#2366). I found all mockgen need from go list is the package path. Why not deduce the package path from source file or directory in mockgen, using logic like:

If GO111MODUE==off: 
   find the relative path from GOPATH
else:
  find the closest go.mod and add the relative path from the go.mod to the module name
  if no go.mod exist and GO111MODUE != on:
    find the relative path from GOPATH

Related to #396

@codyoss
Copy link
Member

codyoss commented Mar 26, 2020

Hey,

I am not against not using go list but using it fixed a couple bugs for use. The advantages we gained by it were:

  1. It is module aware.
  2. It works for resolving import paths that are as followed: Have an import path of example.com/foo but the package in foo is actually bar.
  3. A special case example of # 2 is projects that use go modules >v1: import path of example.com/foo/v2 where the package in v2 is bar(or this could be foo as well).

If we can find a solution that covers these cases, tests should exist for these, and does not use go list I am all for it. It is not great having to exec something from a binary, but it was a simple solution built into the tooling that fixed some bugs.

I have not looked into this yet, but there could also be extra flags we pass into go list to filter down the amount of packages it scans.

@linzhp linzhp mentioned this issue Mar 26, 2020
3 tasks
codyoss pushed a commit that referenced this issue May 19, 2020
Partially fixing #419 

It turn out there are two places calling `go list`:

* [createPackageMap](https://github.com/golang/mock/blob/0b87a54da2167cf3446363bb4b00c6de7677ceaa/mockgen/mockgen.go#L629)
* [parsePackageImport](https://github.com/golang/mock/blob/0b87a54da2167cf3446363bb4b00c6de7677ceaa/mockgen/parse.go#L552), which calls `packages.Load`, which calls `go list` by default.

This PR avoids calling `packages.Load` and saves one of the `go list` calls.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants