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

Add import paths to SPM Plugin #1373

Merged
merged 4 commits into from
Feb 14, 2023
Merged

Conversation

samisuteria
Copy link
Contributor

@samisuteria samisuteria commented Feb 13, 2023

Added an optional parameter to the config file so users can configure import paths for protoc.

Useful in a mono repo where the proto file is not in the target directory and requires imports that are defined relative to the proto.

@thomasvl
Copy link
Collaborator

@FranzBusch

Plugins/SwiftProtobufPlugin/plugin.swift Outdated Show resolved Hide resolved
Comment on lines 102 to 103
// We include the target directory as the default
importPaths = [target.directory]
Copy link
Member

Choose a reason for hiding this comment

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

I think we probably should always include the target.directory even when importPaths are specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds fair to me

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.

LGTM! Thanks

@samisuteria
Copy link
Contributor Author

@FranzBusch by the way theres a matching PR in grpc/grpc-swift#1568 for the same thing and I made the same change there.

@thomasvl thomasvl merged commit 2ad9bad into apple:main Feb 14, 2023
thomasvl pushed a commit to thomasvl/swift-protobuf that referenced this pull request Feb 14, 2023
* add import paths to config

* add comment

* Update Plugins/SwiftProtobufPlugin/plugin.swift

Co-authored-by: Franz Busch <privat@franz-busch.de>

* always include target.directory in import paths

---------

Co-authored-by: Franz Busch <privat@franz-busch.de>
thomasvl pushed a commit that referenced this pull request Feb 14, 2023
* add import paths to config

* add comment

* Update Plugins/SwiftProtobufPlugin/plugin.swift

Co-authored-by: Franz Busch <privat@franz-busch.de>

* always include target.directory in import paths

---------

Co-authored-by: Franz Busch <privat@franz-busch.de>
@FranzBusch
Copy link
Member

@samisuteria I am just coming back to this since I wanted to try this out myself and actually noticed that this behaves differently than I assumed during review. (I will get some tests in place here in another PR).
How are you using this property in your config? Because as far as I can see it only takes absolute paths which makes it very hard to use in general.

@samisuteria
Copy link
Contributor Author

@FranzBusch I actually could not get it to work the way I expected it to. I wanted to reference a proto file that was outside the Sources directory and when running protoc manually with the flags it generated code correctly but I have not been able to get it to work with the plugin. I've resorted to soft linking the shared proto folder into each target that needs it.

@FranzBusch
Copy link
Member

@samisuteria Thanks for replying. The plugin really requires all files to be in one of the targets directory since SPM needs to be able to include them in the build. Symlinking is definitely a valid approach another one could have been to add a target with a custom path.

Is there a reason you cannot move the files to a targets source folder?

Also do I understand it correctly that you are not using the new option you added after all?

@samisuteria
Copy link
Contributor Author

@FranzBusch We have a monorepo with multiple iOS/macOS apps and Swift packages so copying files to the target source would mean copying them to many target sources and updating them would be a manual process. We use a git submodule for all proto files and soft link that folder into the targets that need it.

Correct, I am not using the new option I added. I have not had the time to come back and fix it to the behavior I was expecting.

FranzBusch added a commit to FranzBusch/swift-protobuf that referenced this pull request Mar 29, 2023
# Motivation
In apple#1373 (comment), we added a new option to the SPM plugin config file that allows passing custom import paths (aka search paths) to protoc. Recently, I wanted to try this out but found out that this was not working as expected.

1. The option is configured "globally" across invocations. That doesn't really compose with the way we have structured the other settings
2. The option expects absolute paths right now which makes it almost impossible to use.

I propose to remove this option again from the main branch so we are not shipping it with the 2.0 release. In the PR, I asked the adopter if he is actually using that and confirmed that it isn't working like he expected it to work and wasn't using it.

# Modification
Remove the `importPaths` option again.
@FranzBusch
Copy link
Member

Thanks for explaining!

I think your use-case is a valid one and IMO submodule and soft linking is a good approach for this. Other ways that I have seen in the past to solve this in mono repos:

  1. Put a Package.swift into the repo that contains all proto files which configures the plugin for every target.
  2. Do code generation on a CI and publish the code into separate repos. This has the benefit of avoiding duplicated code generation but comes with the down maintaining repos and CI

I also just opened a PR to remove the option you added here again on main due to the fact that it is not really working. #1389

thomasvl pushed a commit that referenced this pull request Mar 29, 2023
# Motivation
In #1373 (comment), we added a new option to the SPM plugin config file that allows passing custom import paths (aka search paths) to protoc. Recently, I wanted to try this out but found out that this was not working as expected.

1. The option is configured "globally" across invocations. That doesn't really compose with the way we have structured the other settings
2. The option expects absolute paths right now which makes it almost impossible to use.

I propose to remove this option again from the main branch so we are not shipping it with the 2.0 release. In the PR, I asked the adopter if he is actually using that and confirmed that it isn't working like he expected it to work and wasn't using it.

# Modification
Remove the `importPaths` option again.
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.

3 participants