skip to Main Content

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?

4

Answers


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

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search