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

Commit

Permalink
Best effort guesses for output package path (#547)
Browse files Browse the repository at this point in the history
I know we need output package path to avoid self-importing. However, this only happens when the mock is output into an already existing package, in which case the caller is responsible for passing -self_package. Without -self_package, guessing output package path should be on best effort basis. There is no requirement for destination to be in a Go module or GOPATH (in fact, it's hard, if possible at all, to do so in Bazel). So parsePackageImport(dstPath) often fails. Such failure should not fail the main program. We should instead be optimistic and leave output package package empty, assuming no self importing would happen.
  • Loading branch information
linzhp committed Apr 16, 2021
1 parent 9336b7e commit 317c030
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mockgen/mockgen.go
Expand Up @@ -136,12 +136,15 @@ func main() {
outputPackagePath := *selfPackage
if outputPackagePath == "" && *destination != "" {
dstPath, err := filepath.Abs(filepath.Dir(*destination))
if err != nil {
log.Fatalf("Unable to determine destination file path: %v", err)
}
outputPackagePath, err = parsePackageImport(dstPath)
if err != nil {
log.Fatalf("Unable to determine destination file path: %v", err)
if err == nil {
pkgPath, err := parsePackageImport(dstPath)
if err == nil {
outputPackagePath = pkgPath
} else {
log.Println("Unable to infer -self_package from destination file path:", err)
}
} else {
log.Println("Unable to determine destination file path:", err)
}
}

Expand Down

0 comments on commit 317c030

Please sign in to comment.