realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.58k stars 2.22k forks source link

Adding SwiftLint Using SPM Includes Extra Packages #5434

Closed faizan-ali-mlk closed 2 months ago

faizan-ali-mlk commented 7 months ago

Hi,

I am trying to add SwiftLint using Swift Package Manager (SPM), but I don't know why it contains extra packages with it. I tested with CocoaPods, but there it is not adding any extra packages. I also tried to install other packages using SPM, like Alamofire, but that also did not add extra packages with it. I searched the issue logs, but I don't think anyone else is experiencing this issue. Please see the screenshots for the issue display.

Swiftlint using package manager

Screenshot 2024-01-21 at 14 41 47 Screenshot 2024-01-21 at 14 42 00

AlamoreFire Using Package manager

Screenshot 2024-01-21 at 14 42 17 Screenshot 2024-01-21 at 14 42 21
SimplyDanny commented 7 months ago

Actually, it shouldn't be surprising that Swift packages depend on other packages transitively. However, on macOS, the SwiftLint plugin depends on a compiled binary only. So the sources shouldn't be required. There seems to be a bug in Xcode/Swift Package Manager, though, so that still all dependencies are loaded and compiled.

5404 appears to fix this. Please have a try with the current main branch and let us know if it works.

faizan-ali-mlk commented 7 months ago

Thank you for the quick reply. I tested it by setting the main branch in SPM, but the result is the same.

Screenshot 2024-01-21 at 18 16 41 Screenshot 2024-01-21 at 18 17 54

I am also confused about one more flow. When I add the package, I get this popup.

Screenshot 2024-01-21 at 18 15 28

If I set the target for my app, I encounter the following errors, and my project does not compile. However, it works without selecting the target. I am sharing this information in case you can help me understand why that popup is appearing.

Screenshot 2024-01-21 at 18 16 32

> Note: I am using Xcode 15.2 and Macbook Pro with M1 Chip

SimplyDanny commented 7 months ago

If I set the target for my app, I encounter the following errors, and my project does not compile.

SwiftLint can also be consumed as a library (e.g. to develop your own version with extensions). It's a normal Swift package that provides three products: the executable, the library and the plugin.

To use the plugin only you must not assign the other products to any of your targets. Otherwise, they will be seen as dependencies that must be compiled for the target platform. But SwiftLint only builds on macOS (and Linux).

I tested it by setting the main branch in SPM, but the result is the same.

Xcode and SPM behave a bit surprising with respect to binary dependencies. All source dependencies are still downloaded and checked out even though they won't be used in later steps. That's why you see all the packages listed in Xcode. Yet, the build should be fast (at least on main), because the binary SwiftLint framework is consumed to actually build/link the plugin.

faizan-ali-mlk commented 7 months ago

ok @SimplyDanny, so will this issue be resolve or I should switch to Pod, Because i am not liking it that i have not 1 or 2 but almost 8 other libraries which i am not intended to use it.

SimplyDanny commented 7 months ago

ok @SimplyDanny, so will this issue be resolve or I should switch to Pod, Because i am not liking it that i have not 1 or 2 but almost 8 other libraries which i am not intended to use it.

I don't think we can do anything on the SwiftLint side. This might rather be an issue with SPM or Xcode. If you can reproduce it with a small example, you may open a ticket on their side.

filiplazov commented 4 months ago

Hello @SimplyDanny, how about providing a binary only repository for the plugin? I think its a pretty common use case that swiftlint is consumed by iOS / Mac developers and they don't necessarily need to import the actual framework. In our company, the main reason we are using the plugin is for an SDK project (a swift package with many modules) and we don't have build phases to invoke swiftlint. Of course we can wrap the SDK in an app and run the lint from there but its very inneficient as compared to running an individual swift module. Do you think this is a worthy use case?

filiplazov commented 4 months ago

What I asked above is for the purposes of avoiding "spilling out" internal Swiftlint dependencies so they are never resolved by the consumer.

mildm8nnered commented 4 months ago

Xcode and SPM behave a bit surprising with respect to binary dependencies. All source dependencies are still downloaded and checked out even though they won't be used in later steps. That's why you see all the packages listed in Xcode. Yet, the build should be fast (at least on main), because the binary SwiftLint framework is consumed to actually build/link the plugin.

I've been bitten by this before - thinking that this unnecessarily downloaded source was the source for what was executing, when it was not, and I find this SPM behaviour generally quite annoying.

I did try playing around a while ago with making a binary only package, but I don't think I got it to the point where it was working. Because of the limitations of SPM I think it might require a dedicated repo, which is also a pain. I think it would be less confusing for the user though.

doroshenko commented 3 months ago

I would like to second the request by @filiplazov to create a separate repo for build plugin distribution.

After integration SwiftLint as a build plugin in our projects, we have noticed that SPM is extremely slow when doing the initial package fetch operation. This is because it is downloading an entire git repository with all commit history, branches and tags, which is case of SwiftLint is more than 1.5 GB in size just for the main repository.

There is a long-standing issue about this on SPM Github page, but it doesn't look like it's going to be resolved any time soon: https://github.com/apple/swift-package-manager/issues/6062

As could be seen in the issue description, Lottie had the similar problem, and they were able to work around the SPM limitations it by switching to a separate repo for binary releases.

SimplyDanny commented 2 months ago

For your interest: I'm currently working on a separate repository for the plugins that stays in sync with the main SwiftLint repository without any manual maintenance effort. I think the benefit of it will be significant compared to the hassle that's caused by the aggregation in one repository and the way SPM treats it.

SimplyDanny commented 2 months ago

SwiftLintPlugins should be ready to use now. Let me know if this matches what you were asking for. Also give a shout if something doesn't work properly.

DD-P commented 2 months ago

This looks perfect. Thank you @SimplyDanny..

doroshenko commented 2 months ago

@SimplyDanny I switched my projects to your repo, and everything works perfectly! Thanks a lot!