Skip to content

Commit 17f45aa

Browse files
committed
Revise empty file support for import public
If the file defines no types, but it does have `import public` directives, then it still needs provide those imports so code only depending on this module gets the types.
1 parent 788f1b0 commit 17f45aa

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// DO NOT EDIT.
2+
// swift-format-ignore-file
3+
// swiftlint:disable all
4+
//
5+
// Generated by the Swift generator plugin for the protocol buffer compiler.
6+
// Source: Sources/ReExportAOnly/reexport_a_only.proto
7+
//
8+
// For information on using the generated types, please see the documentation:
9+
// https://github.com/apple/swift-protobuf/
10+
11+
// Use of 'import public' causes re-exports:
12+
@_exported import enum ModuleA.E
13+
@_exported import let ModuleA.Extensions_ext_str
14+
@_exported import struct ModuleA.A
15+
16+
// This file contained no messages, enums, or extensions.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import public "Sources/ModuleA/a.proto";

Protos/CompileTests/MultiModule/module_mappings.pbascii

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ mapping {
1010
module_name: "ImportsImportsAPublicly"
1111
proto_file_path: "Sources/ImportsImportsAPublicly/imports_imports_a_publicly.proto"
1212
}
13+
mapping {
14+
module_name: "ReExportAOnly"
15+
proto_file_path: "Sources/ReExportAOnly/reexport_a_only.proto"
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// DO NOT EDIT.
2+
// swift-format-ignore-file
3+
// swiftlint:disable all
4+
//
5+
// Generated by the Swift generator plugin for the protocol buffer compiler.
6+
// Source: Sources/ReExportAOnly/reexport_a_only.proto
7+
//
8+
// For information on using the generated types, please see the documentation:
9+
// https://github.com/apple/swift-protobuf/
10+
11+
// Use of 'import public' causes re-exports:
12+
@_exported import enum ModuleA.E
13+
@_exported import let ModuleA.Extensions_ext_str
14+
@_exported import struct ModuleA.A
15+
16+
// This file contained no messages, enums, or extensions.

Sources/protoc-gen-swift/FileGenerator.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,42 @@ class FileGenerator {
9595
}
9696
}
9797

98-
// If there is nothing to generate, then just record that and be done (usually means
99-
// there just was one or more services).
100-
let generateEmpty = fileDescriptor.enums.isEmpty && fileDescriptor.messages.isEmpty && fileDescriptor.extensions.isEmpty
101-
guard !generateEmpty else {
102-
p.print("// This file contained no messages, enums, or extensions.")
103-
return
104-
}
98+
let fileDefinesTypes = !fileDescriptor.enums.isEmpty || !fileDescriptor.messages.isEmpty || !fileDescriptor.extensions.isEmpty
10599

100+
var hasImports = false
106101
if fileDescriptor.needsFoundationImport {
107102
p.print("\(generatorOptions.importDirective.snippet) Foundation")
103+
hasImports = true
108104
}
109105

110106
if fileDescriptor.isBundledProto {
111107
p.print("// 'import \(namer.swiftProtobufModuleName)' suppressed, this proto file is meant to be bundled in the runtime.")
112-
} else {
108+
hasImports = true
109+
} else if fileDefinesTypes {
113110
p.print("\(generatorOptions.importDirective.snippet) \(namer.swiftProtobufModuleName)")
111+
hasImports = true
114112
}
115113

116114
let neededImports = fileDescriptor.computeImports(
117115
namer: namer,
118116
directive: generatorOptions.importDirective,
119117
reexportPublicImports: generatorOptions.visibility != .internal)
120118
if !neededImports.isEmpty {
121-
p.print()
119+
if hasImports {
120+
p.print()
121+
}
122122
p.print(neededImports)
123+
hasImports = true
124+
}
125+
126+
// If there is nothing to generate, then just record that and be done (usually means
127+
// there just was one or more services).
128+
guard fileDefinesTypes else {
129+
if hasImports {
130+
p.print()
131+
}
132+
p.print("// This file contained no messages, enums, or extensions.")
133+
return
123134
}
124135

125136
p.print()

0 commit comments

Comments
 (0)