Skip to content

Conversation

gjcairo
Copy link
Collaborator

@gjcairo gjcairo commented Sep 3, 2024

This PR enables InternalImportsByDefault on GRPCHealth and InteroperabilityTests.
It also regenerates the protos with the latest version of swift-protobuf (v1.28.1), and increases its min version to 1.28.1 to make sure that we don't get build failures when using earlier generators.

@gjcairo gjcairo requested a review from glbrntt September 3, 2024 15:19

for proto in "${protos[@]}"; do
generate_message "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "FileNaming=DropPath"
generate_message "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "FileNaming=DropPath" "UseAccessLevelOnImports=true"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the default for protobuf?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the default only if compiling with Swift 6 (it defaults to false on <6). Since we use this only in the v2 modules I could get rid of it, but I thought it was clearer. I can get rid of it though, I don't think it matters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. I think we should be consistent with generate_grpc, so either explicitly set both or set neither.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, protobuf uses Swift 6 language mode:

https://github.com/apple/swift-protobuf/blob/beeb4142e775f40fa4162b52af32ed29ade4db1e/Sources/protoc-gen-swift/GeneratorOptions.swift#L82-L86

We default to true but ignore it in v1:

private(set) var useAccessLevelOnImports = true

This means if you use gRPC Swift v2 with a Swift 6 compiler but in Swift 5 language mode you get imports for gRPC but not Protobuf (unless you specify otherwise). Would you mind changing our options to match Protobuf?

Copy link
Collaborator Author

@gjcairo gjcairo Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch! Fixed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half fixed 🙃

Can we have the args to generate_message and generate_grpc be consistent? Either both explicitly set "UseAccessLevelOnImports=true" or have neither of them set it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so I've been playing around with this and I think we've got a bit of a problem.
Because the compiler flag dance swift-protobuf did (and we copied) is on the language version, it doesn't matter what compiler we're using: we'll default this option to false because both have their language modes set to 5 for the plugin target.

We could get around this on gRPC and get the right default if, in the Package@swift-6.swift definition of the protoc target, we enable v6 language mode. However, this doesn't affect the swift-protobuf generation, because language mode is still v5 for swift-protobuf's plugin.

I'm starting to think we should just change the option defaults to use #if compiler instead of #if swift both here and on protobuf. I've made this change on gRPC, but for now I'll also explicitly set the options for both in generate.sh because we'll need this to change in swift-protobuf too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I think the behaviour we really want is dependent on the language mode of the module into which the code is being generated which isn't possible for us to know.

Failing that I think doing it based on the compiler version is reasonable.

@glbrntt glbrntt added the version/v2 Relates to v2 label Sep 4, 2024
@gjcairo gjcairo requested a review from glbrntt September 5, 2024 12:55
Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit got missed but looks good otherwise


for proto in "${protos[@]}"; do
generate_message "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "FileNaming=DropPath"
generate_message "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "FileNaming=DropPath" "UseAccessLevelOnImports=true"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half fixed 🙃

Can we have the args to generate_message and generate_grpc be consistent? Either both explicitly set "UseAccessLevelOnImports=true" or have neither of them set it?

@gjcairo gjcairo force-pushed the explicit-access-level-imports branch from 90fce86 to 02f4c93 Compare September 6, 2024 09:24
@gjcairo gjcairo force-pushed the explicit-access-level-imports branch from 02f4c93 to 4a51a89 Compare September 6, 2024 10:39
Copy link
Collaborator

@glbrntt glbrntt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks Gus!

@glbrntt glbrntt merged commit 1b060a3 into grpc:main Sep 6, 2024
15 of 17 checks passed
@gjcairo gjcairo deleted the explicit-access-level-imports branch September 6, 2024 14:31
gjcairo added a commit that referenced this pull request Sep 9, 2024
## Motivation

After the discussion on
#2042 (comment), 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.
glbrntt pushed a commit to grpc/grpc-swift-protobuf that referenced this pull request Sep 20, 2024
## Motivation

After the discussion on
grpc/grpc-swift#2042 (comment), 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.
glbrntt pushed a commit to grpc/grpc-swift-nio-transport that referenced this pull request Sep 20, 2024
## Motivation

After the discussion on
grpc/grpc-swift#2042 (comment), 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

version/v2 Relates to v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants