Repository: uber/prototool
Pull request #259 (2018-12-27 18:48:32)

Add linter to outlaw importing gogo.proto
Importing https://github.com/gogo/protobuf/blob/master/gogoproto/gogo.proto implies that you are using the options within this file, which have an effect on the generated code. This will result in incompatible APIs between golang/protobuf and gogo/protobuf. This will be part of the Uber Style Guide v2.0.

codecov-io: # [Codecov](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=h1) Report
> Merging [#259](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=desc) into [dev](https://codecov.io/gh/uber/prototool/commit/c48fecfa7b6c1ca99fa1c97372ef95d8333c3c5d?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/uber/prototool/pull/259/graphs/tree.svg?width=650&token=A9V0DApqZg&height=150&src=pr)](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##              dev     #259      +/-   ##
==========================================
+ Coverage   71.18%   71.21%   +0.03%     
==========================================
  Files          90       91       +1     
  Lines        4512     4517       +5     
==========================================
+ Hits         3212     3217       +5     
  Misses        944      944              
  Partials      356      356
```


| [Impacted Files](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/lint/lint.go](https://codecov.io/gh/uber/prototool/pull/259/diff?src=pr&el=tree#diff-aW50ZXJuYWwvbGludC9saW50Lmdv) | `78.49% <ø> (ø)` | :arrow_up: |
| [internal/lint/check\_gogo\_not\_imported.go](https://codecov.io/gh/uber/prototool/pull/259/diff?src=pr&el=tree#diff-aW50ZXJuYWwvbGludC9jaGVja19nb2dvX25vdF9pbXBvcnRlZC5nbw==) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=footer). Last update [c48fecf...328a12e](https://codecov.io/gh/uber/prototool/pull/259?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/lint/check_gogo_not_imported.go b/internal/lint/check_gogo_not_imported.go
new file mode 100644
index 0000000..0e1d9f8
--- /dev/null
+++ b/internal/lint/check_gogo_not_imported.go
@@ -0,0 +1,50 @@
+// Copyright (c) 2019 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+package lint
+
+import (
+	"strings"
+
+	"github.com/emicklei/proto"
+	"github.com/uber/prototool/internal/text"
+)
+
+var (
+	gogoNotImportedLinter = NewLinter(
+		"GOGO_NOT_IMPORTED",
+		`Verifies that the "gogo.proto" file from gogo/protobuf is not imported.`,
+		checkGogoNotImported,
+	)
+)
+
+func checkGogoNotImported(add func(*text.Failure), dirPath string, descriptors []*proto.Proto) error {
+	return runVisitor(&gogoNotImportedVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
+}
+
+type gogoNotImportedVisitor struct {
+	baseAddVisitor
+}
+
+func (v gogoNotImportedVisitor) VisitImport(element *proto.Import) {
+	if strings.HasSuffix(element.Filename, "gogo.proto") {
+		v.AddFailuref(element.Position, `Importing "gogo.proto" is not allowed.`)
+	}
+}
diff --git a/internal/lint/lint.go b/internal/lint/lint.go
index ae40d26..f1cabbb 100644
--- a/internal/lint/lint.go
+++ b/internal/lint/lint.go
@@ -61,6 +61,7 @@ var (
 		fileOptionsRequireJavaPackageLinter,
 		fileOptionsUnsetJavaMultipleFilesLinter,
 		fileOptionsUnsetJavaOuterClassnameLinter,
+		gogoNotImportedLinter,
 		messageFieldsNotFloatsLinter,
 		messageFieldNamesLowerSnakeCaseLinter,
 		messageFieldNamesLowercaseLinter,


Test patch:
diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go
index 0284762..df9dbc6 100644
--- a/internal/cmd/cmd_test.go
+++ b/internal/cmd/cmd_test.go
@@ -377,7 +377,12 @@ func TestLint(t *testing.T) {
 		`5:1:FILE_OPTIONS_GO_PACKAGE_NOT_LONG_FORM`,
 		"testdata/lint/gopackagelongform/gopackagelongform.proto",
 	)
-
+	assertDoLintFile(
+		t,
+		false,
+		`10:1:GOGO_NOT_IMPORTED`,
+		"testdata/lint/gogonotimported/gogonotimported.proto",
+	)
 	assertDoLintFile(
 		t,
 		false,
diff --git a/internal/cmd/testdata/lint/gogonotimported/gogo/gogo.proto b/internal/cmd/testdata/lint/gogonotimported/gogo/gogo.proto
new file mode 100644
index 0000000..bece06e
--- /dev/null
+++ b/internal/cmd/testdata/lint/gogonotimported/gogo/gogo.proto
@@ -0,0 +1,10 @@
+syntax = "proto3";
+
+package gogo;
+
+option go_package = "gogopb";
+option java_multiple_files = true;
+option java_outer_classname = "GogoProto";
+option java_package = "com.gogo";
+
+message Gogo {}
diff --git a/internal/cmd/testdata/lint/gogonotimported/gogonotimported.proto b/internal/cmd/testdata/lint/gogonotimported/gogonotimported.proto
new file mode 100644
index 0000000..2517e5e
--- /dev/null
+++ b/internal/cmd/testdata/lint/gogonotimported/gogonotimported.proto
@@ -0,0 +1,14 @@
+syntax = "proto3";
+
+package foo;
+
+option go_package = "foopb";
+option java_multiple_files = true;
+option java_outer_classname = "GogonotimportedProto";
+option java_package = "com.foo";
+
+import "gogo/gogo.proto";
+
+message Foo {
+  gogo.Gogo gogo = 1;
+}
diff --git a/internal/cmd/testdata/lint/gogonotimported/prototool.yaml b/internal/cmd/testdata/lint/gogonotimported/prototool.yaml
new file mode 100644
index 0000000..d5fade1
--- /dev/null
+++ b/internal/cmd/testdata/lint/gogonotimported/prototool.yaml
@@ -0,0 +1,4 @@
+lint:
+  rules:
+    add:
+      - GOGO_NOT_IMPORTED