Browse Source

Default `UseAccessLevelOnImports` to `false` (#2047)

## Motivation

After the discussion on
https://github.com/grpc/grpc-swift/pull/2042#discussion_r1746796122, I
played a bit more with `InternalImportsByDefault` and the different
generator options, got to the following conclusions:

- If you add `import Foo` to some file, where `Foo` is also being
imported with an explicit `internal` access-level modifier _in the
generated code_, it will work as long as `InternalImportsByDefault` is
enabled.
- If you disable `InternalImportsByDefault` for the corresponding target
in `Package.swift`, you get an `"Ambiguous implicit access level for
import of 'Foo'; it is imported as 'internal' elsewhere"` **error** (not
a warning). This means that if the code generator plugin(s) begin adding
the access level modifiers by default based on how they're built, they
could cause source-breakages for users unintentionally.
- _This isn't any different between language mode 5 or 6_ - I tried
changing the target's language mode and the behaviour is the same as
described above in either case.

Given all this, defaulting `UseAccessLevelOnImports` to `false`
**always** for now may be the easiest (and least surprising, from a
users' perspective) thing to do, until `InternalImportsByDefault` are
enabled by default in a future Swift >6.0 version (as the proposal
states), where we can default to `true` again:
```
#if compiler(>=6.x) // where x is the version where internal imports by default is enabled
// default to true
#else
// default to false
#endif
```

The rationale behind this is that adding access levels to imports on
your code is currently totally optional. If you choose to start adding
them explicitly, then it's okay to also have to tell your
tooling/generators that they should also add them explicitly. If you
don't, they'll keep generating things the exact same way they've been
doing it, which is what users of the generator would expect.

## Modifications
- Default `UseAccessLevelOnImports` to `false` always.
- Regenerate protos
- Remove `InternalImportsByDefault` from test and executable targets,
since it doesn't make a lot of sense to have access level modifiers on
imports here anyways as these targets cannot be imported.
Gus Cairo 1 year ago
parent
commit
d3ef09d71e
24 changed files with 65 additions and 76 deletions
  1. 2 2
      Examples/v2/echo/Generated/echo.grpc.swift
  2. 4 4
      Examples/v2/echo/Subcommands/Collect.swift
  3. 2 2
      Examples/v2/hello-world/Generated/helloworld.grpc.swift
  4. 1 1
      Examples/v2/hello-world/HelloWorld.swift
  5. 3 3
      Examples/v2/hello-world/Subcommands/Greet.swift
  6. 3 3
      Examples/v2/hello-world/Subcommands/Serve.swift
  7. 2 2
      Examples/v2/route-guide/Generated/route_guide.grpc.swift
  8. 4 8
      Package@swift-6.swift
  9. 0 1
      Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec03-step04-gen-grpc.txt
  10. 4 4
      Sources/performance-worker/BenchmarkClient.swift
  11. 2 2
      Sources/performance-worker/BenchmarkService.swift
  12. 2 2
      Sources/performance-worker/Generated/grpc_testing_benchmark_service.grpc.swift
  13. 2 2
      Sources/performance-worker/Generated/grpc_testing_worker_service.grpc.swift
  14. 5 5
      Sources/performance-worker/PerformanceWorker.swift
  15. 3 3
      Sources/performance-worker/RPCStats.swift
  16. 0 6
      Sources/protoc-gen-grpc-swift/Options.swift
  17. 1 1
      Tests/GRPCHTTP2TransportTests/ControlService.swift
  18. 2 2
      Tests/GRPCHTTP2TransportTests/Generated/control.grpc.swift
  19. 5 5
      Tests/GRPCHTTP2TransportTests/HTTP2TransportNIOPosixTests.swift
  20. 4 4
      Tests/GRPCHTTP2TransportTests/HTTP2TransportNIOTransportServicesTests.swift
  21. 6 6
      Tests/GRPCHTTP2TransportTests/HTTP2TransportTests.swift
  22. 5 5
      Tests/GRPCHTTP2TransportTests/Test Utilities/HTTP2StatusCodeServer.swift
  23. 1 1
      Tests/GRPCHTTP2TransportTests/Test Utilities/XCTest+Utilities.swift
  24. 2 2
      Tests/GRPCHTTP2TransportTests/XCTestCase+Vsock.swift

+ 2 - 2
Examples/v2/echo/Generated/echo.grpc.swift

@@ -21,8 +21,8 @@
 // For information on using the generated types, please see the documentation:
 //   https://github.com/grpc/grpc-swift
 
-internal import GRPCCore
-internal import GRPCProtobuf
+import GRPCCore
+import GRPCProtobuf
 
 internal enum Echo_Echo {
     internal static let descriptor = GRPCCore.ServiceDescriptor.echo_Echo

+ 4 - 4
Examples/v2/echo/Subcommands/Collect.swift

@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-internal import ArgumentParser
-private import GRPCCore
-private import GRPCHTTP2Core
-private import GRPCHTTP2TransportNIOPosix
+import ArgumentParser
+import GRPCCore
+import GRPCHTTP2Core
+import GRPCHTTP2TransportNIOPosix
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
 struct Collect: AsyncParsableCommand {

+ 2 - 2
Examples/v2/hello-world/Generated/helloworld.grpc.swift

@@ -21,8 +21,8 @@
 // For information on using the generated types, please see the documentation:
 //   https://github.com/grpc/grpc-swift
 
-internal import GRPCCore
-internal import GRPCProtobuf
+import GRPCCore
+import GRPCProtobuf
 
 internal enum Helloworld_Greeter {
     internal static let descriptor = GRPCCore.ServiceDescriptor.helloworld_Greeter

+ 1 - 1
Examples/v2/hello-world/HelloWorld.swift

@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-internal import ArgumentParser
+import ArgumentParser
 
 @main
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)

+ 3 - 3
Examples/v2/hello-world/Subcommands/Greet.swift

@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-internal import ArgumentParser
-internal import GRPCHTTP2Transport
-internal import GRPCProtobuf
+import ArgumentParser
+import GRPCHTTP2Transport
+import GRPCProtobuf
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
 struct Greet: AsyncParsableCommand {

+ 3 - 3
Examples/v2/hello-world/Subcommands/Serve.swift

@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-internal import ArgumentParser
-internal import GRPCHTTP2Transport
-internal import GRPCProtobuf
+import ArgumentParser
+import GRPCHTTP2Transport
+import GRPCProtobuf
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
 struct Serve: AsyncParsableCommand {

+ 2 - 2
Examples/v2/route-guide/Generated/route_guide.grpc.swift

@@ -21,8 +21,8 @@
 // For information on using the generated types, please see the documentation:
 //   https://github.com/grpc/grpc-swift
 
-internal import GRPCCore
-internal import GRPCProtobuf
+import GRPCCore
+import GRPCProtobuf
 
 internal enum Routeguide_RouteGuide {
     internal static let descriptor = GRPCCore.ServiceDescriptor.routeguide_RouteGuide

+ 4 - 8
Package@swift-6.swift

@@ -346,8 +346,7 @@ extension Target {
       ],
       swiftSettings: [
         .swiftLanguageMode(.v6),
-        .enableUpcomingFeature("ExistentialAny"),
-        .enableUpcomingFeature("InternalImportsByDefault")
+        .enableUpcomingFeature("ExistentialAny")
       ]
     )
   }
@@ -710,8 +709,7 @@ extension Target {
       path: "Examples/v2/echo",
       swiftSettings: [
         .swiftLanguageMode(.v6),
-        .enableUpcomingFeature("ExistentialAny"),
-        .enableUpcomingFeature("InternalImportsByDefault")
+        .enableUpcomingFeature("ExistentialAny")
       ]
     )
   }
@@ -773,8 +771,7 @@ extension Target {
       ],
       swiftSettings: [
         .swiftLanguageMode(.v6),
-        .enableUpcomingFeature("ExistentialAny"),
-        .enableUpcomingFeature("InternalImportsByDefault")
+        .enableUpcomingFeature("ExistentialAny")
       ]
     )
   }
@@ -837,8 +834,7 @@ extension Target {
       ],
       swiftSettings: [
         .swiftLanguageMode(.v6),
-        .enableUpcomingFeature("ExistentialAny"),
-        .enableUpcomingFeature("InternalImportsByDefault")
+        .enableUpcomingFeature("ExistentialAny")
       ]
     )
   }

+ 0 - 1
Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec03-step04-gen-grpc.txt

@@ -2,5 +2,4 @@ $ protoc --plugin=.build/debug/protoc-gen-grpc-swift \
   -I Protos \
   --grpc-swift_out=Sources/Generated \
   --grpc-swift_opt=_V2=true \
-  --grpc-swift_opt=UseAccessLevelOnImports=false \
   Protos/route_guide.proto

+ 4 - 4
Sources/performance-worker/BenchmarkClient.swift

@@ -14,10 +14,10 @@
  * limitations under the License.
  */
 
-private import Foundation
-internal import GRPCCore
-private import NIOConcurrencyHelpers
-private import Synchronization
+import Foundation
+import GRPCCore
+import NIOConcurrencyHelpers
+import Synchronization
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
 final class BenchmarkClient: Sendable {

+ 2 - 2
Sources/performance-worker/BenchmarkService.swift

@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-internal import GRPCCore
-private import Synchronization
+import GRPCCore
+import Synchronization
 
 import struct Foundation.Data
 

+ 2 - 2
Sources/performance-worker/Generated/grpc_testing_benchmark_service.grpc.swift

@@ -24,8 +24,8 @@
 // For information on using the generated types, please see the documentation:
 //   https://github.com/grpc/grpc-swift
 
-internal import GRPCCore
-internal import GRPCProtobuf
+import GRPCCore
+import GRPCProtobuf
 
 internal enum Grpc_Testing_BenchmarkService {
     internal static let descriptor = GRPCCore.ServiceDescriptor.grpc_testing_BenchmarkService

+ 2 - 2
Sources/performance-worker/Generated/grpc_testing_worker_service.grpc.swift

@@ -24,8 +24,8 @@
 // For information on using the generated types, please see the documentation:
 //   https://github.com/grpc/grpc-swift
 
-internal import GRPCCore
-internal import GRPCProtobuf
+import GRPCCore
+import GRPCProtobuf
 
 internal enum Grpc_Testing_WorkerService {
     internal static let descriptor = GRPCCore.ServiceDescriptor.grpc_testing_WorkerService

+ 5 - 5
Sources/performance-worker/PerformanceWorker.swift

@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-internal import ArgumentParser
-private import GRPCCore
-private import GRPCHTTP2Core
-private import GRPCHTTP2TransportNIOPosix
-private import NIOPosix
+import ArgumentParser
+import GRPCCore
+import GRPCHTTP2Core
+import GRPCHTTP2TransportNIOPosix
+import NIOPosix
 
 @main
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)

+ 3 - 3
Sources/performance-worker/RPCStats.swift

@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-private import Foundation
-internal import GRPCCore
-internal import NIOConcurrencyHelpers
+import Foundation
+import GRPCCore
+import NIOConcurrencyHelpers
 
 /// Stores the real time latency histogram and error code count dictionary,
 /// for the RPCs made by a particular GRPCClient. It gets updated after

+ 0 - 6
Sources/protoc-gen-grpc-swift/Options.swift

@@ -74,16 +74,10 @@ struct GeneratorOptions {
   private(set) var gRPCModuleName = "GRPC"
   private(set) var swiftProtobufModuleName = "SwiftProtobuf"
   private(set) var generateReflectionData = false
-
   #if compiler(>=6.0)
   private(set) var v2 = false
   #endif
-
-  #if compiler(>=6.0)
-  private(set) var useAccessLevelOnImports = true
-  #else
   private(set) var useAccessLevelOnImports = false
-  #endif
 
   init(parameter: any CodeGeneratorParameter) throws {
     try self.init(pairs: parameter.parsedPairs)

+ 1 - 1
Tests/GRPCHTTP2TransportTests/ControlService.swift

@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-internal import GRPCCore
+import GRPCCore
 
 import struct Foundation.Data
 

+ 2 - 2
Tests/GRPCHTTP2TransportTests/Generated/control.grpc.swift

@@ -22,8 +22,8 @@
 // For information on using the generated types, please see the documentation:
 //   https://github.com/grpc/grpc-swift
 
-internal import GRPCCore
-internal import GRPCProtobuf
+import GRPCCore
+import GRPCProtobuf
 
 internal enum Control {
     internal static let descriptor = GRPCCore.ServiceDescriptor.Control

+ 5 - 5
Tests/GRPCHTTP2TransportTests/HTTP2TransportNIOPosixTests.swift

@@ -14,13 +14,13 @@
  * limitations under the License.
  */
 
-private import GRPCCore
-private import GRPCHTTP2Core
-private import GRPCHTTP2TransportNIOPosix
-internal import XCTest
+import GRPCCore
+import GRPCHTTP2Core
+import GRPCHTTP2TransportNIOPosix
+import XCTest
 
 #if canImport(NIOSSL)
-private import NIOSSL
+import NIOSSL
 #endif
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)

+ 4 - 4
Tests/GRPCHTTP2TransportTests/HTTP2TransportNIOTransportServicesTests.swift

@@ -15,10 +15,10 @@
  */
 
 #if canImport(Network)
-private import GRPCCore
-private import GRPCHTTP2Core
-internal import GRPCHTTP2TransportNIOTransportServices
-internal import XCTest
+import GRPCCore
+import GRPCHTTP2Core
+import GRPCHTTP2TransportNIOTransportServices
+import XCTest
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
 final class HTTP2TransportNIOTransportServicesTests: XCTestCase {

+ 6 - 6
Tests/GRPCHTTP2TransportTests/HTTP2TransportTests.swift

@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-internal import GRPCCore
-private import GRPCHTTP2Core
-private import GRPCHTTP2TransportNIOPosix
-private import GRPCHTTP2TransportNIOTransportServices
-private import GRPCProtobuf
-internal import XCTest
+import GRPCCore
+import GRPCHTTP2Core
+import GRPCHTTP2TransportNIOPosix
+import GRPCHTTP2TransportNIOTransportServices
+import GRPCProtobuf
+import XCTest
 
 @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
 final class HTTP2TransportTests: XCTestCase {

+ 5 - 5
Tests/GRPCHTTP2TransportTests/Test Utilities/HTTP2StatusCodeServer.swift

@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-internal import GRPCHTTP2Core
-private import NIOCore
-private import NIOHPACK
-private import NIOHTTP2
-private import NIOPosix
+import GRPCHTTP2Core
+import NIOCore
+import NIOHPACK
+import NIOHTTP2
+import NIOPosix
 
 /// An HTTP/2 test server which only responds to request headers by sending response headers and
 /// then closing. Each stream will be closed with the ":status" set to the value of the

+ 1 - 1
Tests/GRPCHTTP2TransportTests/Test Utilities/XCTest+Utilities.swift

@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-private import XCTest
+import XCTest
 
 func XCTAssertThrowsError<T, E: Error>(
   ofType: E.Type,

+ 2 - 2
Tests/GRPCHTTP2TransportTests/XCTestCase+Vsock.swift

@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-private import NIOPosix
-internal import XCTest
+import NIOPosix
+import XCTest
 
 extension XCTestCase {
   func vsockAvailable() -> Bool {