Skip to content

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

@ankisme

Description

@ankisme

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))
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions