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

Commit 317c030

Browse files
authored
Best effort guesses for output package path (#547)
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.
1 parent 9336b7e commit 317c030

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

mockgen/mockgen.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ func main() {
136136
outputPackagePath := *selfPackage
137137
if outputPackagePath == "" && *destination != "" {
138138
dstPath, err := filepath.Abs(filepath.Dir(*destination))
139-
if err != nil {
140-
log.Fatalf("Unable to determine destination file path: %v", err)
141-
}
142-
outputPackagePath, err = parsePackageImport(dstPath)
143-
if err != nil {
144-
log.Fatalf("Unable to determine destination file path: %v", err)
139+
if err == nil {
140+
pkgPath, err := parsePackageImport(dstPath)
141+
if err == nil {
142+
outputPackagePath = pkgPath
143+
} else {
144+
log.Println("Unable to infer -self_package from destination file path:", err)
145+
}
146+
} else {
147+
log.Println("Unable to determine destination file path:", err)
145148
}
146149
}
147150

0 commit comments

Comments
 (0)