SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Cannot add SwiftLint as package for Xcode plugin

Open fmoraes74 opened this issue 2 years ago • 30 comments

New Issue Checklist

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 version to 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```


fmoraes74 avatar Dec 12 '22 18:12 fmoraes74

I can look into this if you share more information, steps to reproduce, exact issue you're seeing, etc.

jpsim avatar Dec 12 '22 19:12 jpsim

@jpsim This is what I see when I tried to add it: image

Not tried anything more than just add the SwiftLint package dependency and it failed with the above error.

fmoraes74 avatar Dec 12 '22 19:12 fmoraes74

We’re not using BrightFuture or FileKit, so those errors are coming from something else.

jpsim avatar Dec 12 '22 19:12 jpsim

This only happens when I add SwiftLint package. I even tried with the commit from the latest version:

image image

When I try to add it, I see it come up with version 0.9.2:

image

fmoraes74 avatar Dec 12 '22 19:12 fmoraes74

I think your SwiftPM caches may be out of date. Please clear those and try again.

jpsim avatar Dec 12 '22 19:12 jpsim

I cleaned up the directory and retried, same issue, it still shows version 0.9.2. I also noticed the following: image

fmoraes74 avatar Dec 12 '22 19:12 fmoraes74

Version 0.9.2 is from March 2016: https://github.com/realm/SwiftLint/releases/0.9.2

Please try again with 0.50.3.

jpsim avatar Dec 12 '22 19:12 jpsim

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: image

I don't know what else to try.

fmoraes74 avatar Dec 12 '22 20:12 fmoraes74

Please clear your SwiftPM caches.

This StackOverflow answer has some steps to do that: https://stackoverflow.com/a/68876107/373262

jpsim avatar Dec 12 '22 20:12 jpsim

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.

designatednerd avatar Dec 13 '22 03:12 designatednerd

For what it's worth I'm also seeing this if I've set the version to 0.50.3 / up to next minor: Screen Shot 2022-12-12 at 9 52 55 PM

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.

designatednerd avatar Dec 13 '22 03:12 designatednerd

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

Brudus avatar Dec 13 '22 07:12 Brudus

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.

fmoraes74 avatar Dec 13 '22 14:12 fmoraes74

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

jpsim avatar Dec 13 '22 20:12 jpsim

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.

designatednerd avatar Dec 14 '22 01:12 designatednerd

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 avatar Dec 21 '22 22:12 sroycoursera

@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.

technocidal avatar Dec 22 '22 07:12 technocidal

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.

kokluch avatar Dec 27 '22 19:12 kokluch

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.

dherschberger avatar Dec 29 '22 22:12 dherschberger

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(

shnmxvi avatar Dec 30 '22 11:12 shnmxvi

I'll take a look at the swift-syntax prerelease today. Maybe I can figure something out.

technocidal avatar Dec 31 '22 08:12 technocidal

@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 avatar Dec 31 '22 16:12 technocidal

@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 avatar Jan 03 '23 09:01 shnmxvi

@shnmxvi This might be related to #4628 and it seems to be a bug in Xcode.

nkristek avatar Jan 03 '23 10:01 nkristek

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.

evfemist avatar Jan 09 '23 17:01 evfemist

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 avatar Jan 09 '23 19:01 jpsim

@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.

technocidal avatar Jan 09 '23 20:01 technocidal

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 avatar Jan 10 '23 13:01 evfemist

@evfemist Looks like this hasn't been merged yet... merge conflict? Would be great to use your fix. Your fork works great.

hunterleebrown-cbs avatar Jan 18 '23 00:01 hunterleebrown-cbs

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.

ryanjfar avatar Jan 25 '23 07:01 ryanjfar