6

I have used "swiftlint" in my iOS project. I also know to type "swiftlint autocorrect" at the project's root path in the terminal to correct some warnings and errros.

Is there a way to run "swiftlint autocorrect" when I Run the project in Xcode?

Edit

The command "swiftlint autocorrect" isn't recommended using this way by the swiftlint.

I found the way to use "swiftlint autocorrect" is to type it in the terminal.

Using "swiftlint autocorrect" when "Run" in the Xcode is a good practice or not?

3
  • Try something like this medium.com/flawless-app-stories/… Commented May 31, 2021 at 6:42
  • @deaton.dg Thank you. Commented May 31, 2021 at 9:47
  • I am not totally clear on why you say this isn't recommended. I posted an answer which copies language from their GitHub page so that you can be confident that this is an endorsed solution. If this answers all your questions, please accept my answer. Otherwise, please explain why you think this isn't recommended, and I will help more. Commented May 31, 2021 at 16:52

4 Answers 4

16

Autocorrect is no longer available. If you run the autocorrect from the terminal, you should see the following message

The swiftlint autocorrect command is no longer available. Please use swiftlint --fix instead.

So if you have installed swiftlint via CocoaPods, the script should look like

${PODS_ROOT}/SwiftLint/swiftlint --fix && ${PODS_ROOT}/SwiftLint/swiftlint
Sign up to request clarification or add additional context in comments.

Comments

1

Just to expand slightly on the answer from @deaton.dg (which I found very useful).

The previous script I was replacing (in the Xcode Build Phases) had been contained in quotation marks ", however using the new script in quotation marks was causing a build error.

It took a little while to work out it was the quotation marks that appeared to be causing issue.

So if you have installed SwiftLint via CocoaPods, and you wish to execute swiftlint autocorrect at build time, I found you need to make sure the script does not include quotation marks (ie. exactly as per the subsequent comment from @deaton.dg ):

${PODS_ROOT}/SwiftLint/swiftlint autocorrect && ${PODS_ROOT}/SwiftLint/swiftlint

Comments

0

The swiftlint project says that you can do this with the following steps:

Xcode

Integrate SwiftLint into your Xcode project to get warnings and errors displayed in the issue navigator.

To do this click the Project in the file navigator, then click the primary app target, and go to Build Phases. Click the + and select "New Run Script Phase". Insert the following as the script:

if which swiftlint >/dev/null; then
    swiftlint
else
    echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

You might want to move your SwiftLint phase directly before 'Compile Sources' step, to detect errors quickly before compiling. However, SwiftLint is designed to run on valid Swift code that cleanly completes the compiler's parsing stage. So running SwiftLint before 'Compile Sources' might yield some incorrect results.

If you wish to autocorrect violations as well, your script could run swiftlint autocorrect && swiftlint instead of just swiftlint. This will mean that all correctable violations are fixed, while ensuring warnings show up in your project for remaining violations.

If you've installed SwiftLint via CocoaPods the script should look like this:

"${PODS_ROOT}/SwiftLint/swiftlint"

I think using swiftlint like this is recommended. The only warning is

You might want to move your SwiftLint phase directly before 'Compile Sources' step, to detect errors quickly before compiling. However, SwiftLint is designed to run on valid Swift code that cleanly completes the compiler's parsing stage. So running SwiftLint before 'Compile Sources' might yield some incorrect results.

which is about the placement of the script and not the script itself. This says that swiftlint is only guaranteed to perform correctly on valid source code, so you are only guaranteed that swiftlint will work correctly if you place the script after the 'Compile Sources' phase so that swiftlint only runs once the compiler has verified your code.

However, in my personal setup, I use vim to run swiftlint when I save files (generally in an uncompilable state) which runs before compiling, and I have experienced no issues other than the occasional strange formatting when I haven't yet closed all of the { which I have opened. I do not think you will have any issues running swiftlint before the 'Compile Sources' phase.

Note: the other warning on the page

⚠️This plugin will not work with Xcode 8 or later without disabling SIP. This is not recommended.

is not about the 'Run Script Phase'. This is referring to an Xcode plugin which will run swiftlint on save like is possible with vim. To use this plugin on recent versions of Xcode, you need to disable the security feature SIP, which is not recommended. But again, this is unrelated to the 'Run Script Phase' solution.

3 Comments

"${PODS_ROOT}/SwiftLint/swiftlint" Can this setup run " swiftlint autocorrect " Automation?
Yes. You can think of ${PODS_ROOT}/SwiftLint/swiftlint and swiftlint as more or less the same thing. The first is how you call swiftlint when it’s installed by CocoaPods, and the second is how you call swiftlint when it is installed to your system through something like Homebrew. You can use ${PODS_ROOT}/SwiftLint/swiftlint autocorrect && ${PODS_ROOT}/SwiftLint/swiftlint to do the autocorrection in a run phase if swiftlint is installed with CocoaPods.
@deato.dg I type "${PODS_ROOT}/SwiftLint/swiftlint autocorrect && ${PODS_ROOT}/SwiftLint/swiftlint" replase "${PODS_ROOT}/SwiftLint/swiftlint". Xcode compile error. I am going to type "swiftlint autocorrect" every time.
0

Yes, Please add the following script to Build Phases before Compile Resources

export PATH="$PATH:/opt/homebrew/bin"

if which swiftlint > /dev/null; then
  swiftlint --fix && swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.