Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call command.Generate(req *plugin.CodeGeneratorRequest) twice could cause bug. #759

Open
ankisme opened this issue Oct 18, 2022 · 0 comments

Comments

@ankisme
Copy link

ankisme commented Oct 18, 2022

Here is the code below.

Look at the TestOK(), I call MyGenerate() once, so the test passed.

Look at the TestFail(), I call MyGenerate() twice, so the test failed.

And I think the reason is that var plugins pluginSlice in generator.go is a global variable.

So if I use gogo as a binary(such as .exe) through command line console, then gogo works well.

But if I import gogo and try to use gogo in my go code, then gogo could not work.

package adminProtoComponent_test

import (
	"encoding/json"
	"fmt"
	"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
	"github.com/gogo/protobuf/protoc-gen-gogo/generator"
	"github.com/gogo/protobuf/vanity"
	"github.com/gogo/protobuf/vanity/command"
	"github.com/stretchr/testify/assert"
	"testing"

	gogoplugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin"
)

/*
// test.proto
syntax = "proto3";

message Foo {
	int32 A = 1;
	int32 B = 2;
}
*/

func GetFd() *descriptor.FileDescriptorProto {
	var fd descriptor.FileDescriptorProto
	jsonText := `{"name":"test.proto","message_type":[{"name":"Foo","field":[{"name":"A","number":1,"label":1,"type":5,"json_name":"A"},{"name":"B","number":2,"label":1,"type":5,"json_name":"B"}]}],"source_code_info":{"location":[{"span":[1,0,6,1]},{"path":[12],"span":[1,0,18]},{"path":[4,0],"span":[3,0,6,1]},{"path":[4,0,1],"span":[3,8,11]},{"path":[4,0,2,0],"span":[4,2,14]},{"path":[4,0,2,0,5],"span":[4,2,7]},{"path":[4,0,2,0,1],"span":[4,8,9]},{"path":[4,0,2,0,3],"span":[4,12,13]},{"path":[4,0,2,1],"span":[5,2,14]},{"path":[4,0,2,1,5],"span":[5,2,7]},{"path":[4,0,2,1,1],"span":[5,8,9]},{"path":[4,0,2,1,3],"span":[5,12,13]}]},"syntax":"proto3"}`
	fmt.Println(jsonText)

	err := json.Unmarshal([]byte(jsonText), &fd)
	if err != nil {
		panic(err)
	}

	return &fd
}

func MyGenerate(protoFileNames []string, fdProtos []*descriptor.FileDescriptorProto) *gogoplugin.CodeGeneratorResponse {
	g := generator.New()
	g.Request = &gogoplugin.CodeGeneratorRequest{
		ProtoFile: fdProtos,
	}
	g.Request.FileToGenerate = protoFileNames

	files := g.Request.GetProtoFile()
	files = vanity.FilterFiles(files, vanity.NotGoogleProtobufDescriptorProto)

	vanity.ForEachFile(files, vanity.TurnOnMarshalerAll)
	vanity.ForEachFile(files, vanity.TurnOnSizerAll)
	vanity.ForEachFile(files, vanity.TurnOnUnmarshalerAll)

	vanity.ForEachFieldInFilesExcludingExtensions(vanity.OnlyProto2(files), vanity.TurnOffNullableForNativeTypesWithoutDefaultsOnly)
	vanity.ForEachFile(files, vanity.TurnOffGoUnrecognizedAll)
	vanity.ForEachFile(files, vanity.TurnOffGoUnkeyedAll)
	vanity.ForEachFile(files, vanity.TurnOffGoSizecacheAll)

	vanity.ForEachFile(files, vanity.TurnOffGoEnumPrefixAll)
	vanity.ForEachFile(files, vanity.TurnOffGoGettersAll)
	vanity.ForEachFile(files, vanity.TurnOnMessageNameAll)

	resp := command.Generate(g.Request)
	//command.Write(resp)
	return resp
}

// --- PASS: TestOK (0.00s)
func TestOK(t *testing.T) {
	fd := GetFd()
	resp := MyGenerate([]string{*fd.Name}, []*descriptor.FileDescriptorProto{fd})
	assert.Equal(t, 7276, len(*resp.File[0].Content))
}

/*
    GogoProto_test.go:87:
        	Error Trace:	D:\proj\mine\gopath\src\gitee-server\web\admin\adminProto\adminProtoComponent\GogoProto_test.go:87
        	Error:      	Not equal:
        	            	expected: 7276
        	            	actual  : 2591
        	Test:       	TestFail
--- FAIL: TestFail (0.01s)
*/
func TestFail(t *testing.T) {
	for i := 1; i <= 2; i++ {
		fd := GetFd()
		resp := MyGenerate([]string{*fd.Name}, []*descriptor.FileDescriptorProto{fd})
		assert.Equal(t, 7276, len(*resp.File[0].Content))
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant