Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[packages] Add support for swift-format #41129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stuartmorgan-g opened this issue Sep 23, 2019 · 14 comments · Fixed by flutter/packages#5928
Closed

[packages] Add support for swift-format #41129

stuartmorgan-g opened this issue Sep 23, 2019 · 14 comments · Fixed by flutter/packages#5928
Labels
c: contributor-productivity Team-specific productivity, code health, technical debt. p: tooling Affects the flutter_plugin_tools package P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team

Comments

@stuartmorgan-g
Copy link
Contributor

flutter_plugins supports Dart, Java, and Objective-C formatting. Since we'll be using Swift preferentially for macOS plugins (and probably someday for iOS, even if not yet), we should add support for swift-format too. (There are several Swift formatters, but based on internal discussions that's the one we should use.)

It's a bit fiddly to set up at the moment, but we should at least check if it's on the path and run it if so.

@stuartmorgan-g stuartmorgan-g added plugin p: tooling Affects the flutter_plugin_tools package labels Sep 23, 2019
@escamoteur escamoteur added the c: contributor-productivity Team-specific productivity, code health, technical debt. label Sep 23, 2019
@stuartmorgan-g stuartmorgan-g added the P3 Issues that are less important to the Flutter project label May 24, 2021
@stuartmorgan-g
Copy link
Contributor Author

Actually running this in CI is still problematic:

  • There are still no binaries provided by the project.
  • The version-locking could make it more difficult for us to update the version of Xcode we're using in CI.

@cyanglaz
Copy link
Contributor

cyanglaz commented Sep 22, 2022

Now that we are migrating iOS to swift, we may want to consider bumping up the priority of this?

There are still no binaries provided by the project.

Can you explain more about this? What is the "project"?

The version-locking could make it more difficult for us to update the version of Xcode we're using in CI.

Did you mean https://github.com/apple/swift-format#matching-swift-format-to-your-swift-version-swift-57-and-earlier. Are we using, or plan to use different versions of Swift for different plugins. Can we query the swift version used in build setting and download the formatter accordingly?

@stuartmorgan-g
Copy link
Contributor Author

There are still no binaries provided by the project.

Can you explain more about this? What is the "project"?

That was referring to apple/swift-format.

The version-locking could make it more difficult for us to update the version of Xcode we're using in CI.

Did you mean https://github.com/apple/swift-format#matching-swift-format-to-your-swift-version-swift-57-and-earlier.

Yes, but it looks like it's resolved now! That "Note" section was only added a couple of days ago, and unless I'm misreading it, it's saying we won't have this problem going forward.

Are we using, or plan to use different versions of Swift for different plugins.

The problem was that the version they were referring to there wasn't the Swift syntax version, it was the swift toolchain version. Meaning that it used to be the case that you had to build the specific version of the former for the exact version of Xcode you had. So any time we wanted to update Xcode we would have had to update the formatter. And if there wasn't a formater version yet, we would actually be blocked from updating Xcode temporarily.

(And anyone wanting to format locally would have had to figure all of this out as well, to get and build just the right version.)

@jmagman
Copy link
Member

jmagman commented Sep 22, 2022

It also says swift-format can be used as an API from another swift script, as opposed to using it as a command line tool https://github.com/apple/swift-format#api-usage. That would allow us to use Swift Package Manager to manage the version resolution. That may be easier than building from source https://github.com/apple/swift-format#getting-swift-format

@hellohuanlin hellohuanlin self-assigned this Dec 14, 2022
@stuartmorgan-g stuartmorgan-g changed the title Add support for swift-format [plugins] Add support for swift-format Jan 18, 2023
@hellohuanlin
Copy link
Contributor

In case it's helpful, if you also contribute to the flutter engine, there is an issue with the swift-format under depot_tools. The current workaround is to install and run swift-format on your local machines (swift-format format -i -r /path/to/your/code).

Make sure to set up the export path properly to run your own copy of swift-format instead of the one under depot_tools.

@stuartmorgan-g stuartmorgan-g added package flutter/packages repository. See also p: labels. and removed plugin labels Mar 5, 2023
@stuartmorgan-g stuartmorgan-g changed the title [plugins] Add support for swift-format [packages] Add support for swift-format Mar 21, 2023
@stuartmorgan-g
Copy link
Contributor Author

Now that we're using LUCI, we could probably just by making a cipd package of swift-format, and add it to the dependencies in .ci.yaml.

@hellohuanlin
Copy link
Contributor

cc @tarrinneal

@hellohuanlin hellohuanlin removed their assignment Oct 20, 2023
stuartmorgan-g added a commit to stuartmorgan-g/packages that referenced this issue Oct 23, 2023
Adds support for swift-format in the `format` command. For now this is
optional, so is only triggered if the flag is explictly passed. In the
future, once we have CI support for swift-format, we will likely want to
make it on-by-default as with the other formatters.

Part of flutter/flutter#41129
auto-submit bot pushed a commit to flutter/packages that referenced this issue Oct 23, 2023
Adds support for swift-format in the `format` command. For now this is optional, so is only triggered if the flag is explictly passed. In the future, once we have CI support for swift-format, we will likely want to make it on-by-default as with the other formatters.

Part of flutter/flutter#41129
@jmagman
Copy link
Member

jmagman commented Jan 12, 2024

The packages tooling is now set up to handle swift-format as of flutter/packages#5204, but it still needs to be put into CI.

I see there's already a CIPD package at infra/3pp/tools/swift-format/mac-arm64, which I just tried and it works:

$ echo 'infra/3pp/tools/swift-format/mac-arm64 latest' > ensure_file.txt
$ cipd ensure -ensure-file ensure_file.txt -root ~/foo
$ ~/foo/swift-format path/to/flutter/packages/flutter_tools/templates/app_shared/ios-swift.tmpl/Runner/AppDelegate.swift
import Flutter
import UIKit

@UIApplicationMain
...

@jmagman
Copy link
Member

jmagman commented Jan 12, 2024

Recipe change to add the CIPD package as a dependency: https://flutter-review.googlesource.com/c/recipes/+/54020

@jmagman
Copy link
Member

jmagman commented Jan 13, 2024

That package only runs on macOS. That means we'll need a Mac repo_checks that swift-formats. We could make check_podspecs into a more generic repo_checks that also calls swift-format (it runs really fast).

@Gustl22
Copy link
Contributor

Gustl22 commented Jan 13, 2024

swift-format also works on Ubuntu, but not on Alpine, e.g. https://hub.docker.com/r/mtgto/swift-format/

@jmagman
Copy link
Member

jmagman commented Jan 13, 2024

swift-format also works on Ubuntu, but not on Alpine, e.g. https://hub.docker.com/r/mtgto/swift-format/

Understood. The version we easily have available to us in CI from chromium is only built for Mac. I'd rather rearrange our test than maintain our own versions of this package, if possible.

@stuartmorgan-g
Copy link
Contributor Author

Recipe change to add the CIPD package as a dependency: https://flutter-review.googlesource.com/c/recipes/+/54020

🤦🏻 I forgot to check non-Flutter packages. Thanks for finding that!

We could make check_podspecs into a more generic repo_checks that also calls swift-format (it runs really fast).

Agreed, I was just looking at your other PR and wondering if we should combine it with something else.

We'll probably want to adjust the tooling to allow disabling formatting of each language so that we don't have two failures every time file in any other language fails, but I can add that easily. I'll take a look next week.

auto-submit bot pushed a commit that referenced this issue Jan 16, 2024
`swift-format` alphabetizes imports.  Alphabetize them in swift template files and integration tests.

I found this as part of #41129 running `swift-import` on packages.
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jan 18, 2024
…#5905)

Get ready for a world where `swift-format` is available on the `PATH` https://flutter-review.googlesource.com/c/recipes/+/54020

1. Add `format --clang-format --java --kotlin --swift --dart` flags to decide whether to run specific formatters, as opposed to using the `path`.  Keep `swift-format` optional but default the others to run.  This matches the current behavior on Linux.
2. Add `*-path` variants of each.

This will allow us to run `format --swift --no-clang-format --no-java --no-kotlin --no-dart`  on the macOS bot so it doesn't duplicate same `format` call run on Linux.

Part of flutter/flutter#41129
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jan 19, 2024
Make `swift-format` CIPD package available on the `PATH` https://flutter-review.googlesource.com/c/recipes/+/54020

Run `format --no-clang-format --no-java --no-kotlin --no-dart`  on the macOS builder so it doesn't duplicate same `format` call run on Linux.

Filter out generated files until flutter/flutter#141799 is done.

Also add `swift-format lint` call during `format` command.  Fix the one casing issue it found in a test file.

Failing run:
https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8758535881172004177/+/u/Run_package_tests/Swift_format/stdout

Successful run:
https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8758492350529121249/+/u/Run_package_tests/Swift_format/stdout

Fixes flutter/flutter#41129
auto-submit bot pushed a commit to flutter/packages that referenced this issue Jan 20, 2024
Reworks Pigeon's CI validation of generated files to support multiple hosts, rather than only Linux, with the ability to set specific languages per host, to allow us to run validation for each language on whatever host it is most convenient to set up a formatter for that language, rather than having to support all languages on Linux.

See discussion in #5928

Part of flutter/flutter#41129
Copy link

github-actions bot commented Feb 2, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: contributor-productivity Team-specific productivity, code health, technical debt. p: tooling Affects the flutter_plugin_tools package P3 Issues that are less important to the Flutter project package flutter/packages repository. See also p: labels. team-ecosystem Owned by Ecosystem team triaged-ecosystem Triaged by Ecosystem team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants