Failed to determine package path when source file is not inside GOPATH
disksing opened this issue · 2 comments
disksing commented
Now the package path is calculated based on the relative relationship between GOPATH and the directory where the file is located:
Lines 130 to 150 in 51ce9a7
With the go mod support after go1.11, source files can be outside of GOPATH. So when I run gofail, I can see the error: "missing package for %q; using %q as failpoint path".
I can think of several ways to get the package import path more accurately:
- Search parent directories for the
go.mod
file which contains the import path of the module. - Use
cmd.Exec
to callgo list
to get the import path. - Use reflect in generated
foo.fail.go
to determine import path.
I personally think that the third method is better as it relies less on the executing environment. It can be done by generating such code:
type __fp_foo struct{}
var __fp_fooBar *runtime.Failpoint = runtime.NewFailpoint(reflect.TypeOf(__fp_foo{}).PkgPath(), "fooBar")