Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise how modules imports are done to better support import public #1539

Merged
merged 2 commits into from
Jan 11, 2024

Conversation

thomasvl
Copy link
Collaborator

@thomasvl thomasvl commented Jan 8, 2024

Historically Swift didn't have a good way to model import public in .proto
files. But by using @_exported import, we can get the majority of the way
there. This makes restructuring of proto files less likely to break Swift source
code; meaning Swift is less of a "special case" compared to other languages.

If generating with public or package visibility, then instead of importing
the whole module of for an import public, explicitly re-export the imports of
the types from that file. That will make them usable for the given source file
but will also let the types continue to be used by someone just importing this
module.

This also means a file with no import public usages becomes must simpler as it
can just rely on its direct dependencies to provide all the types that could be
used.

- Add basic infrastructure to have Compile Tests.

- Add a specific MultiModule CompileTest.
  - This ensure that with `import public` is properly handle and the generated
    code compiles even when a needed type is coming from a public import.
  - So the any relevant code changes are more visible, check the the generated
    code so the diffs for an changes related to the test will be easily
    reviewed.
  - The SPM plugin doesn't support multi module generation
    (apple#1450), which is also why the
    code needs to be checked in, otherwise, it might simplify the tests.

Note: The whole concept of CompileTests doesn't have to be a set up as distinct
Swift Packages, but if we put the required targets in the main Package.swift,
all projects using swift-protobuf would end up having the "overhead" of those
test only targets, so I've errored on the side of a distinct package to make
sure there is no impact of these tests on the folks using swift-protobuf.
@thomasvl thomasvl requested a review from tbkka January 8, 2024 19:12
@thomasvl
Copy link
Collaborator Author

thomasvl commented Jan 8, 2024

I split the setup of the compile test infra into the first comment, so the actually changes for import public are better isolated to hopefully make reviewing a comment at a time better to see the specific changes.

@thomasvl
Copy link
Collaborator Author

thomasvl commented Jan 8, 2024

fyi @FranzBusch @Lukasa @allevato

@tbkka
Copy link
Contributor

tbkka commented Jan 8, 2024

The new CompileTest looks intriguing.

Historically Swift didn't have a good way to model `import public` in .proto
files. But by using `@_exported import`, we can get the majority of the way
there. This makes restructuring of proto files less likely to break Swift source
code; meaning Swift is less of a "special case" compared to other languages.

If generating with `public` or `package` visibility, then instead of importing
the whole module of for an `import public`, explicitly re-export the imports of
the types from that file. That will make them usable for the given source file
but will also let the types continue to be used by someone just importing this
module.

This also means a file with no `import public` usages becomes must simpler as it
can just rely on its direct dependencies to provide all the types that could be
used.
@thomasvl
Copy link
Collaborator Author

thomasvl commented Jan 9, 2024

The new CompileTest looks intriguing.

This could all just be in the main Package.swift, but I didn't like the idea that it impacted everyone just trying to use swift-protobuf, that's mainly why I spun it out on its own. We can always revisit that in the future. We could also consider combining the directory with the PluginExample one; but since that's both a test and an example, I didn't do that to start with.

Copy link
Member

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

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

I like the changes here. The only thing I would like us to reconsider is checking in yet more generated proto files. In the PluginExamples, directory I have already used the Swift PM plugin to generate Swift files on the fly and it also does a compile check. The CI for that is already setup correctly and works.

Maybe we can merge the two setups into one since they are aiming to do the same thing. The added benefit of that would be that we make sure everything is doable via the Swift PM plugin as well.

@thomasvl
Copy link
Collaborator Author

I like the changes here. The only thing I would like us to reconsider is checking in yet more generated proto files. In the PluginExamples, directory I have already used the Swift PM plugin to generate Swift files on the fly and it also does a compile check. The CI for that is already setup correctly and works.

I tried to cover this in CompileTests/MultiModule/README.md -

...
This can't use the SwiftPM Plugin as that currently doesn't have support for the module mappings.

We have the request for this in #1450, but I thought the opinion was to not do this and instead wait on the hope that SwiftPM would eventually expose something so the SwiftPM plugin could eventually generate this info itself.

Maybe we can merge the two setups into one since they are aiming to do the same thing. The added benefit of that would be that we make sure everything is doable via the Swift PM plugin as well.

Since we don't support the mappings, I don't believe a developer could create such a set up.

@FranzBusch
Copy link
Member

I tried to cover this in CompileTests/MultiModule/README.md -

Oh sorry I missed that part. Makes sense then! Thanks for clarifying.

@thomasvl
Copy link
Collaborator Author

The new CompileTest looks intriguing.

@tbkka did you want to look at this PR in a little more detail, or am I good to merge it?

@tbkka
Copy link
Contributor

tbkka commented Jan 11, 2024

If Franz and Tony are both okay with it, then please go ahead!

@thomasvl
Copy link
Collaborator Author

Thanks!

@thomasvl thomasvl merged commit 0e70157 into apple:main Jan 11, 2024
9 checks passed
@thomasvl thomasvl deleted the public_imports branch January 11, 2024 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants