SwiftLint
SwiftLint copied to clipboard
Make SwiftLint available as a build tool plugin
Motivation
SwiftLint is an important part as a build phase for many projects. Depending on where the project is being build the path where swiftlint is located on a machine can differ. One example is the recent Homebrew path change when Apple introduced Apple Silicon machines. Using SwiftLint as a plugin means that there is no need for an external package manager to install it, less setup during initial project setup and a more portable project overall.
Open Questions
- Do I need to add additional parameters to the actual
swiftlintcall?
| 1 Message | |
|---|---|
| :book: | Skipping OSSCheck because SwiftLint hasn't changed compared to 'main' |
Generated by :no_entry_sign: Danger
Great initiative, a suggestion is that the plugin should look for a configuration file in the project path such as the SwiftGenPlugin does https://github.com/SwiftGen/SwiftGenPlugin/blob/stable/Plugins/SwiftGenPlugin/Plugin.swift
Thanks for the PR! Does the SwiftPM plugin interface allow for any user configuration, like having users specify the path to the config file? If so, supporting that could be nice.
By default, SwiftLint will look at the root of the project for a .swiftlint.yml file and will traverse the directory tree to look for child configurations.
If we add this, there should also be a section in the readme that explains how to configure it for people's projects.
@tboogh @jpsim
Thanks for your feedback!
I'll research the configuration options and will include some version of the configuration file discovery similar to SwiftGen.
Anything in particular I'll need to keep in mind regarding the Readme?
Anything in particular I'll need to keep in mind regarding the Readme?
Basic instructions for how to get it up and running in projects would be helpful. Via Package.swift and Xcode ideally, since I suspect those setup processes to differ a bit.
Great initiative, a suggestion is that the plugin should look for a configuration file in the project path such as the
SwiftGenPlugindoes https://github.com/SwiftGen/SwiftGenPlugin/blob/stable/Plugins/SwiftGenPlugin/Plugin.swift
@tboogh
There is a small difference between SwiftGen and our implementation. SwiftGen only supports Swift packages while I'd like to support SwiftLint as a Xcode Build Tool Plugin. There are some slight differences in the API.
SwiftLint has it's own child configuration discovery which means that we probably don't need to look for any configuration if we find a .swiftlint.yml at the project root. The API for Xcode plugins only gives you the target which includes a list of files assigned to this target. SwiftLint configuration files are generally not assigned to a target so they don't show up here. Targets are also not required to have their own root directory like Swift packages. That makes a little tricky to figure out where to look for a potential configuration.
Regarding the whole configuration thing: I can't find an obvious way to pass in the path to a configuration file.
This means that we can either add this in this pretty barebones state and explain it really well or wait until the situation improves.
Regarding the whole configuration thing: I can't find an obvious way to pass in the path to a configuration file.
Fair enough, SwiftPM/Xcode must not expose any way to configure the build tool, it just has to work by default, which I think in the common case should work fine for SwiftLint. Maybe extensibility will come in the future...
I feel a little bit bad about this but I'm not confident that the Xcode Plugin integration is ready. I'm unable to get the plug-in to run consistently with my work project which I used as a test bed. There are a couple of issues:
-
Adding and subsequently removing the plug-in from the project results in Xcode still trying to run the plug-in even though it's no longer configured as a build phase. Only a very thorough clean (clearing Derived Data etc.) gets rid of that issue. I'll report this as a radar and will post the link here as well.
-
The plug-in seems to work fine when SwiftLint reports errors but throws a very weird error when only warnings are reported. Something about non-zero exit Code during execution. This is particularly weird as the linting up until that point runs smoothly and reports the appropriate warnings.
Using it as a plug-in for a Swift Package seems to work a lot better so I'll keep that integration around. I'll push these changes later today.
If anyone wants to take a look, feel free! I'm at my wits end to be honest.
I feel a little bit bad about this but I'm not confident that the Xcode Plugin integration is ready. I'm unable to get the plug-in to run consistently with my work project which I used as a test bed. There are a couple of issues:
- Adding and subsequently removing the plug-in from the project results in Xcode still trying to run the plug-in even though it's no longer configured as a build phase. Only a very thorough clean (clearing Derived Data etc.) gets rid of that issue. I'll report this as a radar and will post the link here as well.
- The plug-in seems to work fine when SwiftLint reports errors but throws a very weird error when only warnings are reported. Something about non-zero exit Code during execution. This is particularly weird as the linting up until that point runs smoothly and reports the appropriate warnings.
Using it as a plug-in for a Swift Package seems to work a lot better so I'll keep that integration around. I'll push these changes later today.
If anyone wants to take a look, feel free! I'm at my wits end to be honest.
Hey @technocidal, we have been able to implement a plugin at work that works quite well for us. It ships with our default .swiftlint.yml (which obviously works in our case but wouldn't be desirable in this case) and is configurable.
On top of that, we scan the target's root directory for a .swiftlint.yml as just running the executable didn't pick up the config file, we had to explicitly declare it. For Xcode targets it's a bit different as we've had to make sure we include .swiftlint.yml as a file reference to the Xcode target.
To make our plugin work we had to pass the --in-process-sourcekit flag that I can't see on this PR, maybe something worth considering? This picks the source kit version that runs in process to comply with the plugin's sandboxing rules.
Also, I am happy to share code (or even add a commit) for how we do the configuration discovery, albeit not very ideal for Xcode projects. It could potentially just be added to the package version for now?
Also, great work on this! I'm so excited to see more and more plugins making it into open source projects π π
@pol-piella Can you please share the code? I'm sure it'll be useful for a lot of us wanting to add SwiftLint as a plugin to our projects. Thanks!
Not sure what you need @jcabreram, but I'm using SwiftLint as Xcode plugin, see here.
@pol-piella Can you please share the code? I'm sure it'll be useful for a lot of us wanting to add SwiftLint as a plugin to our projects. Thanks!
I think the main change that made it work for us was passing the --in-process-sourcekit to the executable. It's a shame I can't commit to this PR, but could try open a new one adding this flag and link it here?
@technocidal @jpsim I have opened a new PR (as I can't push to this one) https://github.com/realm/SwiftLint/pull/4259 preserving all the changes the @technocidal has made and just adding the extra flag that makes Sourcekit run in-process. We have been using this config at work with no issues so far. Also happy to look at any test/CI failures that come up and get this ready to be merged if people are happy with it π
@pol-piella Your PR looks great and thank you for taking this feature over the finish line π
@jpsim I have fixed the CI issues and merge conflicts with the PR now https://github.com/realm/SwiftLint/pull/4259 . Are we happy to merge the changes that @technocidal made and my flag addition? Happy to make any more changes if required π
@jpsim You seem to have closed #4259 due to --in-process-sourcekit being deprecated. Any suggestion for moving this forward?
@jpsim You seem to have closed #4259 due to
--in-process-sourcekitbeing deprecated. Any suggestion for moving this forward?
Is this PR still the one you all want to propose to merge? We shouldn't use --in-process-sourcekit because it's deprecated and done nothing. SourceKit is always run in-process now.
There are failing CI jobs here, but I think they were infrastructure errors? I'm re-running the failed jobs now.
Merged in 3fd1573c572f5d960d8e8b3bc46bc229c969ddcb. Thanks for your contributions and patience everyone!
Thanks for taking the time to look into it. In my opinion a basic implementation now is way better than a more advanced one in the future. Xcode will probably evolve the build plugin support over time and perhaps yield a solution for configuration that makes sense for SwiftLint.On 15 Sep 2022, at 20:42, Johannes Ebeling @.***> wrote:ο»Ώ Regarding the whole configuration thing: I can't find an obvious way to pass in the path to a configuration file. This means that we can either add this in this pretty barebones state and explain it really well or wait until the situation improves.
βReply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>
Hi folks, before I open an issue: I'm struggling to get the build plugin to find/resolve my .swiftlint.yml - it's ignoring the ones that are in my project root and local package root.
Is there a trick to this?