From 837f20ad6bc3c8d1ad2128760cd812f2c95ef699 Mon Sep 17 00:00:00 2001 From: r-hang Date: Tue, 5 Sep 2023 14:40:26 -0700 Subject: [PATCH] Fix go generate ./... issues under root (#76) Running `go generate ./...` on current main fails for a couple of reasons (1) The go:generate command in /mockgen/internal/tests/aux_imports_embedded_interface/bugreport.go has an -aux_file argument that specifies a package `faux` that is not in the stdlib and therefore not discoverable in GOROOT. Fixing this with a fully qualified path causes another failure that is known and documented in mockgen/internal/tests/aux_imports_embedded_interface/README.md. Because this failure is known, I've opted to comment out the command as documentation for reproducing the error. (2) mockgen/internal/tests/internal_pkg/generate.go attempts to mock an interface package with reflect mode and runs into fundamental go restrictions of `go build` with internal package paths outside of the internal package scope allowed by go. I've removed the generate directive here since this isn't a supported feature as the implementation of reflect mode creates a tempoorary directory and invokes `go build` there. ref: [reflect.go#L135](github.com/uber-go/mock/blob/8cd1a4cf46a6b1cf232b6862f2f11e81fe77a4e4/mockgen/reflect.go#L135) ref: [#29](github.com/golang/mock/issues/29) Not a failure fix but I've also changed type aliases of `any` back to type aliases of `interface {}`. Looking at the parsing logic, [parse.go#L752](github.com/uber-go/mock/blob/8cd1a4cf46a6b1cf232b6862f2f11e81fe77a4e4/mockgen/parse.go#L752) `interface{}` -> `any` causes *ast.TypeSpec to no longer type check to an *ast.InterfaceType and ends up removing generate mock code meant to test against regressions. ref: [#48](github.com/uber-go/mock/pull/48) --- mockgen/internal/tests/add_generate_directive/mock.go | 2 +- .../tests/aux_imports_embedded_interface/bugreport.go | 3 ++- mockgen/internal/tests/copyright_file/input.go | 2 +- mockgen/internal/tests/defined_import_local_name/mock.go | 4 ++-- mockgen/internal/tests/empty_interface/input.go | 2 +- mockgen/internal/tests/internal_pkg/generate.go | 1 - 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mockgen/internal/tests/add_generate_directive/mock.go b/mockgen/internal/tests/add_generate_directive/mock.go index ab00a30..7a6169c 100644 --- a/mockgen/internal/tests/add_generate_directive/mock.go +++ b/mockgen/internal/tests/add_generate_directive/mock.go @@ -46,7 +46,7 @@ func (m *MockFoo) Bar(arg0 []string, arg1 chan<- Message) { } // Bar indicates an expected call of Bar. -func (mr *MockFooMockRecorder) Bar(arg0, arg1 interface{}) *gomock.Call { +func (mr *MockFooMockRecorder) Bar(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), arg0, arg1) } diff --git a/mockgen/internal/tests/aux_imports_embedded_interface/bugreport.go b/mockgen/internal/tests/aux_imports_embedded_interface/bugreport.go index f49532d..c5b84cf 100644 --- a/mockgen/internal/tests/aux_imports_embedded_interface/bugreport.go +++ b/mockgen/internal/tests/aux_imports_embedded_interface/bugreport.go @@ -1,6 +1,7 @@ package bugreport -//go:generate mockgen -aux_files faux=faux/faux.go -destination bugreport_mock.go -package bugreport -source=bugreport.go Example +// Reproduce the issue described in the README.md with +// $ go:generate mockgen -aux_files faux=faux/faux.go -destination bugreport_mock.go -package bugreport -source=bugreport.go Example import ( "log" diff --git a/mockgen/internal/tests/copyright_file/input.go b/mockgen/internal/tests/copyright_file/input.go index 3448f26..93b75a8 100644 --- a/mockgen/internal/tests/copyright_file/input.go +++ b/mockgen/internal/tests/copyright_file/input.go @@ -2,4 +2,4 @@ package empty_interface //go:generate mockgen -package empty_interface -destination mock.go -source input.go -copyright_file=mock_copyright_header -type Empty any +type Empty interface{} // migrating interface{} -> any does not resolve to an interface type dropping test generation added in b391ab3 diff --git a/mockgen/internal/tests/defined_import_local_name/mock.go b/mockgen/internal/tests/defined_import_local_name/mock.go index 5b44939..3cd3498 100644 --- a/mockgen/internal/tests/defined_import_local_name/mock.go +++ b/mockgen/internal/tests/defined_import_local_name/mock.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: ./mockgen/internal/tests/defined_import_local_name/input.go +// Source: input.go // // Generated by this command: // -// C:\Users\snapp\AppData\Local\Temp\go-build3951283790\b001\exe\mockgen.exe -package defined_import_local_name -destination ./mockgen/internal/tests/defined_import_local_name/mock.go -source ./mockgen/internal/tests/defined_import_local_name/input.go -imports b_mock=bytes,c_mock=context +// mockgen -package defined_import_local_name -destination mock.go -source input.go -imports b_mock=bytes,c_mock=context // // Package defined_import_local_name is a generated GoMock package. package defined_import_local_name diff --git a/mockgen/internal/tests/empty_interface/input.go b/mockgen/internal/tests/empty_interface/input.go index 7d6eea2..08d82a4 100644 --- a/mockgen/internal/tests/empty_interface/input.go +++ b/mockgen/internal/tests/empty_interface/input.go @@ -2,4 +2,4 @@ package empty_interface //go:generate mockgen -package empty_interface -destination mock.go -source input.go -type Empty any +type Empty interface{} // migrating interface{} -> any does not resolve to an interface type. diff --git a/mockgen/internal/tests/internal_pkg/generate.go b/mockgen/internal/tests/internal_pkg/generate.go index 3e3e17f..840a6e8 100644 --- a/mockgen/internal/tests/internal_pkg/generate.go +++ b/mockgen/internal/tests/internal_pkg/generate.go @@ -1,4 +1,3 @@ package test -//go:generate mockgen -destination subdir/internal/pkg/reflect_output/mock.go go.uber.org/mock/mockgen/internal/tests/internal_pkg/subdir/internal/pkg Intf //go:generate mockgen -source subdir/internal/pkg/input.go -destination subdir/internal/pkg/source_output/mock.go