Skip to content

Commit

Permalink
protoc-gen-go: stop generating package "// import" comment
Browse files Browse the repository at this point in the history
Usage of the import comment is being deprecated in favor of a go.mod
file as the Go ecosystem moves towards modules.

Fixes #678
  • Loading branch information
neild committed Sep 13, 2018
1 parent 0f2620f commit 7682d32
Show file tree
Hide file tree
Showing 35 changed files with 34 additions and 98 deletions.
2 changes: 1 addition & 1 deletion proto/test_proto/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/descriptor/descriptor.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions protoc-gen-go/generator/generator.go
Expand Up @@ -1208,12 +1208,7 @@ func (g *Generator) generateHeader() {
}
g.P()

importPath, _, _ := g.file.goPackageOption()
if importPath == "" {
g.P("package ", g.file.packageName)
} else {
g.P("package ", g.file.packageName, " // import ", GoImportPath(g.ImportPrefix)+importPath)
}
g.P("package ", g.file.packageName)
g.P()

if loc, ok := g.file.comments[strconv.Itoa(packagePath)]; ok {
Expand Down
61 changes: 1 addition & 60 deletions protoc-gen-go/golden_test.go
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"flag"
"fmt"
"go/build"
"go/parser"
"go/token"
Expand Down Expand Up @@ -150,7 +149,7 @@ func TestParameters(t *testing.T) {
wantPackageB: "test_beta",
wantImportsA: map[string]bool{
"github.com/golang/protobuf/proto": true,
"beta": true,
"beta": true,
},
}, {
parameters: "import_prefix=prefix",
Expand Down Expand Up @@ -323,64 +322,6 @@ func TestParameters(t *testing.T) {
}
}

func TestPackageComment(t *testing.T) {
workdir, err := ioutil.TempDir("", "proto-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(workdir)

var packageRE = regexp.MustCompile(`(?m)^package .*`)

for i, test := range []struct {
goPackageOption string
wantPackage string
}{{
goPackageOption: ``,
wantPackage: `package proto_package`,
}, {
goPackageOption: `option go_package = "go_package";`,
wantPackage: `package go_package`,
}, {
goPackageOption: `option go_package = "import/path/of/go_package";`,
wantPackage: `package go_package // import "import/path/of/go_package"`,
}, {
goPackageOption: `option go_package = "import/path/of/something;go_package";`,
wantPackage: `package go_package // import "import/path/of/something"`,
}, {
goPackageOption: `option go_package = "import_path;go_package";`,
wantPackage: `package go_package // import "import_path"`,
}} {
srcName := filepath.Join(workdir, fmt.Sprintf("%d.proto", i))
tgtName := filepath.Join(workdir, fmt.Sprintf("%d.pb.go", i))

buf := &bytes.Buffer{}
fmt.Fprintln(buf, `syntax = "proto3";`)
fmt.Fprintln(buf, `package proto_package;`)
fmt.Fprintln(buf, test.goPackageOption)
if err := ioutil.WriteFile(srcName, buf.Bytes(), 0666); err != nil {
t.Fatal(err)
}

protoc(t, []string{"-I" + workdir, "--go_out=paths=source_relative:" + workdir, srcName})

out, err := ioutil.ReadFile(tgtName)
if err != nil {
t.Fatal(err)
}

pkg := packageRE.Find(out)
if pkg == nil {
t.Errorf("generated .pb.go contains no package line\n\nsource:\n%v\n\noutput:\n%v", buf.String(), string(out))
continue
}

if got, want := string(pkg), test.wantPackage; got != want {
t.Errorf("unexpected package statement with go_package = %q\n got: %v\nwant: %v", test.goPackageOption, got, want)
}
}
}

// parseFile returns a file's package name and a list of all packages it imports.
func parseFile(source string) (packageName string, imports []string, err error) {
fset := token.NewFileSet()
Expand Down
Binary file removed protoc-gen-go/protoc-gen-go
Binary file not shown.
2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/deprecated/deprecated.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/extension_base/extension_base.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/extension_user/extension_user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/grpc/grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/import_public/a.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/import_public/b.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/import_public/sub/a.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/import_public/sub/b.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/fmt/m.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_a_1/m1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_a_1/m2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_a_2/m3.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_a_2/m4.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_b_1/m1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_b_1/m2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_import_a1m1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_import_a1m2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/imports/test_import_all.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/multi/multi1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/multi/multi2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/multi/multi3.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/my_test/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-go/testdata/proto3/proto3.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ptypes/any/any.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ptypes/duration/duration.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ptypes/empty/empty.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ptypes/struct/struct.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ptypes/timestamp/timestamp.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ptypes/wrappers/wrappers.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7682d32

Please sign in to comment.