Skip to content

Commit b2a7692

Browse files
committed
protoc-gen-go: avoid importing own package
Normally, protoc-gen-go recommends certain conventions to avoid cyclic imports between generated go files. Some of these are documented in: golang#67 This change adds one more mechanism to avoid cycles: if protoc-gen-go was invoked with import_path parameter set, then it will not generate import statements for any packages with identical import_path. This ends up being a simple way of avoiding cycles when all other options are hard to implement. For example, the codebase that I'm working with has 100s of proto files shared between code written in 4 different languages including go. It's difficult for us to pass all proto files in a directory to protoc. Other alternatives documented in the above issue are even more complex for us to adopt. In contrast, import_path is easy to set.
1 parent a5e9c41 commit b2a7692

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

protoc-gen-go/generator/generator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,11 @@ func (g *Generator) generateImports() {
12661266
importPath = substitution
12671267
}
12681268
importPath = g.ImportPrefix + importPath
1269+
// If we know the import path for our own file, and if that
1270+
// matches the import path for this dependency, skip.
1271+
if g.PackageImportPath != "" && g.PackageImportPath == importPath {
1272+
continue
1273+
}
12691274
// Skip weak imports.
12701275
if g.weak(int32(i)) {
12711276
g.P("// skipping weak import ", fd.PackageName(), " ", strconv.Quote(importPath))

0 commit comments

Comments
 (0)