SwiftLint
SwiftLint copied to clipboard
Cannot add SwiftLint as package for Xcode plugin
New Issue Checklist
- [ ] Updated SwiftLint to the latest version
- [ ] I searched for existing GitHub issues
Describe the bug
I am trying to add SwiftLint as package dependency to run it as a build plugin. It fails resolution with invalid manifest.
Environment
-
SwiftLint version (run
swiftlint versionto be sure)? Trying to pull latest -
Installation method used (Homebrew, CocoaPods, building from source, etc)? SPM
-
Which Xcode version are you using (check
xcodebuild -version)?
Build version 14B47b```
I can look into this if you share more information, steps to reproduce, exact issue you're seeing, etc.
@jpsim This is what I see when I tried to add it:

Not tried anything more than just add the SwiftLint package dependency and it failed with the above error.
We’re not using BrightFuture or FileKit, so those errors are coming from something else.
This only happens when I add SwiftLint package. I even tried with the commit from the latest version:
When I try to add it, I see it come up with version 0.9.2:
I think your SwiftPM caches may be out of date. Please clear those and try again.
I cleaned up the directory and retried, same issue, it still shows version 0.9.2. I also noticed the following:

Version 0.9.2 is from March 2016: https://github.com/realm/SwiftLint/releases/0.9.2
Please try again with 0.50.3.
I understand it is an old version but everytime I add the URL for https://github.com/realm/SwiftLint it always comes up with 0.9.2, so there is something about it that is defaulting to that version.
But even changing the version to 0.50.3 doesn't work:

I don't know what else to try.
Please clear your SwiftPM caches.
This StackOverflow answer has some steps to do that: https://stackoverflow.com/a/68876107/373262
I saw this a bunch of times at Apollo: There's a bug with 0.x versions where SPM doesn't understand that 0.50 is greater than 0.9. You have to manually set it to 0.50.3.
For what it's worth I'm also seeing this if I've set the version to 0.50.3 / up to next minor:

Update: I think it's because the Package.swift file's dependencies are still pointed at a specific commit instead of a tag, which is causing the whole thing to fail at the point of package resolution, even though SwiftLint is being included as a binary in the plugin.
SPM doesn't even get to the point of analysis where it goes "Oh that's a binary", it's just looking at the graph and refusing to proceed once it sees a commit hash rather than a tag.
I have the same issue. I have cleared the package cache and told Xcode to fetch the exact version 0.50.3. And I get the same error message:
Failed to resolve dependencies Dependencies could not be resolved because package 'swiftlint' is required using a stable-version but 'swiftlint' depends on an unstable-version package 'swift-syntax' and root depends on 'swiftlint' 0.50.3.
The same is true for version 0.50.1.
Failed to resolve dependencies Dependencies could not be resolved because package 'swiftlint' is required using a stable-version but 'swiftlint' depends on an unstable-version package 'swift-syntax' and root depends on 'swiftlint' 0.50.1.
I also did this in a fresh project. I use Xcode build version: 14B47b
I have tried different variations but have not been able to make it work yet. I tried using main instead of a version and that got me closer but I started having issues with linking and some other side effects that came with it. I tried removing SwiftLint from linking step but didn't resolve it, so I backed it out and I am still using the script version instead of plugin.
I'm not interested in maintaining this plugin. It's nothing but a headache for me so far. I might remove it from the next release and folks can provide their own SwiftLint plugins if they want to deal with these issues. cc @technocidal
I can imagine this is getting a little maddening. I think it's probably worth supporting with the following caveats in the README:
- You have to point to a commit or branch to use it due to these SPM "stability" requirements. If you don't like the stability requirements, file a bug with SPM, but there's nothing SwiftLint can do about them.
- This means developers cannot use it on libraries/frameworks which are brought in by other applications, only directly on the applications themselves, and you should use whatever you were using before if you're using it for a lib.
I think that at least gives people using it directly on Applications the ability to use it, but keeps you from having to deal with all this nonsense.
I'm able to add it to the project by pointing to main or to a commit. When I do this, after adding the Run Build Tool Plug-ins phase, SwiftLint does work and reports code violations as expected. However the build fails at the linking phase with this message:
Entry point (_main) undefined. for architecture arm64
I haven't been able to make sense why adding the plugin affects the linker.
@sroycoursera
That's probably related to https://github.com/realm/SwiftLint/issues/4628. We've reported this issue to Apple via a Feedback. They are actively looking into it but right now there isn't much we can do from our side.
It's because the Package.swift targets a specific revision for swift-syntax here which is forbidden by swift package. It requires to be updated and point to a tag. Looks like swift-syntax 0.50800.0 is in pre-release. When final tag will be released it's will work. It's what "unstable version" in the error message reports.
@jpsim Please don't remove the plugin, it's nice having done it. swift-syntax pre-release is 8hours ago you'll have a fix soon I guess.
I agree with @kokluch on the explanation and request to continue to support the plugin.
Maybe just add a note to the README to tell people to consume using branch "main" if they want to use the plugin until the issue is resolved.
It's because the Package.swift targets a specific revision for swift-syntax here which is forbidden by swift package. It requires to be updated and point to a tag. Looks like swift-syntax 0.50800.0 is in pre-release. When final tag will be released it's will work. It's what "unstable version" in the error message reports.
@jpsim Please don't remove the plugin, it's nice having done it. swift-syntax pre-release is 8hours ago you'll have a fix soon I guess.
with .package(url: "https://github.com/apple/swift-syntax", exact: "0.50800.0-SNAPSHOT-2022-12-20-a") does not work(
I'll take a look at the swift-syntax prerelease today. Maybe I can figure something out.
@shnmxvi
That's correct, but the reason isn't swift-syntx but rather exact keyword in the dependency definition. This will always fail.
@jpsim
It does however work perfectly fine when defining it like this:
.package(url: "https://github.com/apple/swift-syntax", from: "0.50800.0-SNAPSHOT-2022-12-20-a").
This means that we could cut a new SwiftLint release using this dependency definition and people would be able to use that release instead of main .
Here is my PR: https://github.com/realm/SwiftLint/pull/4662
@technocidal
hm, when i use .package(url: "https://github.com/apple/swift-syntax", from: "0.50800.0-SNAPSHOT-2022-12-20-a").i have error Entry point (_main) undefined. for architecture arm64 (
@shnmxvi This might be related to #4628 and it seems to be a bug in Xcode.
Yeah, looks like. And even if modify plugin to define class with @main in it - then iOS app will not launch, think because of two entry points.
But I have found a temporary workaround by changing .buildCommand to .prebuildCommand for XcodeProjectPlugin.
You can check my commit here
Also there I write a crunch to hide warning "no rule to process file ..." that appears on Xcode 14.2.
The invalid manifest issues should hopefully be fixed with https://github.com/realm/SwiftLint/pull/4674
The Entry point (_main) undefined issue is entirely separate. @evfemist moving the plugin from .buildCommand to .prebuildCommand seems reasonable to me as a solution. Could you please open a PR with your changes from https://github.com/evfemist/SwiftLint/commit/bec2c95cda8cb43414ac710b7662b97072926e10? @technocidal what do you think?
@jpsim @evfemist
That seems like a really good solution and, looking at the documentation, also probably more suitable for SwiftLint in the first place. buildCommand really seems to be more focussed on generating output.
With .prebuildCommand for BuildToolPlugin and XcodeProjectPlugin code looks much better.
Also I found that cache files was added to bundle, this is also fixed in my PR
@evfemist Looks like this hasn't been merged yet... merge conflict? Would be great to use your fix. Your fork works great.
Checking on the status of this. @evfemist branch and pr seems to fix the Xcode build plugin issue when pointing to a specific commit or branch and it would be great if that could make it into the main branch.