Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swiftlint autocorrect command not working

I have installed swiftlint using cocoapods and it is working properly

But when I am trying to use swiftlint autocorrect command in the terminal, it is giving below error

-bash: swiftlint: command not found

Should I need to do any other setup to make the command work in Terminal?

Note: Cant use the SwiftLint package due to some restriction on Mac.

like image 690
pradeep Avatar asked Jul 23 '18 11:07

pradeep


People also ask

How to run swiftlint AutoCorrect command in Xcode?

Running swiftlint autocorrect command in Terminal over and over again is cumbersome. To avoid this, you should integrate the auto-fix feature into your Xcode to run automatically. Go to your Xcode project navigator. Choose Build Phases and add a new Run Script phase: 2. Now you see the new Run Script in the list.

Do I need to use swiftlint --fix or auto correct?

but if its 0.43.0 or higher then you need to use swiftlint --fix it will auto correct the white space, trailing space, initial formating, bracket in if else. etc. which will not affect on your logics or codes. Not all SwiftLint rules are correctable, which mean that they will remain after you run swiftlint autocorrect.

How do I use swiftlint to warn me about style guidelines?

You can use Swiftlint to warn you about the style guidelines you are breaking instead of fixing them: This can be done by modifying the Run Script phase to call swiftlint to show the warnings instead of fixing them with swiftlint autocorrect. To achieve this, head to the Run Script phase and change the script to:

Why linting is disabled when correcting a Swift file?

Standard linting is disabled while correcting because of the high likelihood of violations (or their offsets) being incorrect after modifying a file while applying corrections. The swiftlint analyze command can lint Swift files using the full type-checked AST.


2 Answers

Pods are installed in a folder local to your project, not in any global folder that would already be defined in the terminals $PATH variable, so the terminal is unable to find the script.

There are a few options, but it mostly comes down to being more specific about where the executable is, presuming (for example) your project folder is located at /Users/John/Documents/MyiOsProject/

The pods are likely installed in Pods/SwiftLint/bin

so you should be able to run /Users/John/Documents/MyiOsProject/Pods/SwiftLint/bin/swiftlint autocorrect

This can obviously get tedious very quickly, so just alias it. (Another option would be a Symlink)

open up the file ~/.bashrc for editing and add

alias swiftlint='/Users/John/Documents/MyiOsProject/Pods/SwiftLint/bin/swiftlint'

then you should be able to just type swiftlint autocorrect.

Note: I dont know your exact paths, so you may need to make some minor changes.

like image 132
Scriptable Avatar answered Sep 29 '22 10:09

Scriptable


Installing swiftlint as a cocoapod does not modify the $PATH variable to include the path to the swiftlint command. Here is a guide to do it.

The path to the swiftlint command is probably $HOME/Pods/SwiftLint or sth like that.

like image 33
Fabian Avatar answered Sep 29 '22 10:09

Fabian