nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.66k stars 623 forks source link

Swift Package Manager Xcode Command Plugin does not works on 0.52.9 and later #1594

Closed fthdgn closed 6 months ago

fthdgn commented 7 months ago

Xcode Version: 15.0.1 (15A507) MacOS Version: Ventura 13.6.2

Steps

nicklockwood commented 7 months ago

@fthdgn thanks for reporting this - could you perhaps open a pull request to fix it?

fthdgn commented 7 months ago

https://github.com/nicklockwood/SwiftFormat/compare/0.52.8...0.52.9

I checked this diff but cannot see any change may cause this problem. I am planning to run each commit to pinpoint the commit which introduced the issue.

fthdgn commented 7 months ago

I found the problem. In a commit Plugins/SwiftFormatPlugin/SwiftFormatPluginXcode.swift is deleted. The protocol extension inside of this file is necessary to run by Xcode.

https://github.com/nicklockwood/SwiftFormat/commit/496a7cc0943874fcec0f35b9b73ff2fba4e15837#diff-96f7aaf9e74e9bc9781e14791fa1c79e671057a51920b7e597205ca29b1f7f4a

fthdgn commented 7 months ago

When I restore the file, plugin starts to work. But I am not sure why the code is deleted. If it is related to the other changes made in the commit, I don't fully understand them. Thus I am not opening a pull request yet.

@MahdiBM It seems like you authored the commit. Can you clarify the reason why you deleted the file?

MahdiBM commented 7 months ago

@fthdgn It was incompatible with figuring out the local dependencies of a target. I highly doubt this is causing any issues. We have the same code in https://github.com/apple/swift-openapi-generator and i'm not aware of any problems caused by it there.

Let me check more.

MahdiBM commented 7 months ago

@fthdgn I can reproduce this ... now let me see what's the best solution. We can just put back the file, i think that would be pretty fine although it wouldn't work as good as it should on Xcode, because it won't be able to figure out the local dependencies of a target (or maybe I'm mistaken and Xcode-project passes those to the plugin too)

MahdiBM commented 7 months ago

@fthdgn Looking more, i think you can just re-add the whole file that was removed, and it should be fine. Though i haven't tested this, so give it a try too so we know that it works. I can make the PR myself too if you want.

fthdgn commented 7 months ago

I've opened a PR.

It works in my use case. I didn't test the local dependencies case.

What was the purpose of your commit? Does it enable code formatting on the directories added as local swift package?

MahdiBM commented 7 months ago

@fthdgn Pardon me if i mention some obvious stuff that you already know, but i want to be clear:

In a SwiftPM project, we can have multiple targets that do different things. For example we can have a "Core" target, then a "MyApp" and a "MyCLI" target, and the "MayApp" and "MyCLI" targets can depend on the "Core" target that provides the shared-core logic.

When openning a SwiftPM project in Xcode (NOT as an Xcode / iOS / macOS project that IIRC has a .xcproject file), when you want to run the command plugin, it would show a popup to you and ask you to choose any targets from a list of targets to run the plugin for. Those targets that it shows are always only the "product"s, the "executable" targets, or the "test" targets. This means that it might show "MyCLI" as a target to choose, but it won't show "Core" (assuming e.g. we don't export "Core" as a product, which is unusual). This is a problem, because long-story short, we cannot have a way to ask the plugin to be run on all our local dependencies. It will only be run on products, executables, or test targets of a package. But with the changes i made, it will run on all local dependencies of those targets as well (so "Core" included).

That's the reason for that PR.

nicklockwood commented 6 months ago

@fthdgn fix landed in 0.53.0