google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Swift] Support specifying FlatBuffers dependency as implementation only

snowp opened this issue · comments

We're trying to use the generated Swift files within a Bazel build, wherein we have to mark the FlatBuffers import as @_implmentationOnly and avoid public types. For now we're working around this with https://github.com/envoyproxy/envoy-mobile/pull/2141/files#diff-c5cff54722ae3eae49696d086c379a7599f6bfd28a715470e933e3e1db7fe22dR36-R43, but it would be awesome to be able to do this directly without this post-processing.

I'm not sure what the canonical way to do this would be - perhaps via a file-level attribute?

@keith

Hey,
Thanks for opening the issue.
I'm not that familiar with Bazel, however what is it that you want to do exactly? Because we have a flag that removes the import completely if that's what you are looking for. And for the public APIs I believe that compiler can also generate internal APIs.

Cc: @snowp

The Java code generator uses the "private" attribute on structs and enums to make them "package-private" in the Java sense. Perhaps that could be coopted?

The Swift API does support the private API, so we should be good to go there. However I'm not sure how well it does it. If you face any issues let us know and we will fix it.

struct_def.attributes.Lookup("private") .

Awesome to see that we can just add the private attribute to avoid them being exposed!

For the import, we want it to generate @_implementationOnly import FlatBuffers in order to prevent the build to attempt to expose the import. AFAIK omitting the import is not going to do the right thing

For the import, we want it to generate @_implementationOnly import FlatBuffers in order to prevent the build to attempt to expose the import. AFAIK omitting the import is not going to do the right thing

I'm not sure how would that works since building the Flatbuffers test framework with that flag gives the following error:

Cannot use conformance of 'UInt8' to 'Scalar' here; 'FlatBuffers' has been imported as implementation-only

If possible can you provide like an actual example that builds so we can try it out.

Can you provide the example file you're using to generate that? We're doing this successfully on https://github.com/envoyproxy/envoy-mobile/pull/2141/files now by post processing the file.

The overall goal here is we're vendoring a library that depends on flatbuffers, and it's a limitation of Swift that a single vendored framework cannot contain multiple swiftmodule files for consumption. So we either have to treat it as an implementation detail like we're trying to do here, or we have to force our consumers to depend on flatbuffers indirectly, which is potentially fine as well, but potentially more work

Just opened a PR for this, let's see if everything can be merged and done

Awesome! (for reference: #7202)

One thing is that things likely cannot be public if you do this, do you think this option should imply that, or should it be up to the user to specify the right attributes for that too?

One thing is that things likely cannot be public if you do this, do you think this option should imply that, or should it be up to the user to specify the right attributes for that too?

If that's the case, we will need to force everything to be internal.