🧟‍♂️ adventures in software development: first run of swiftlint analyze
Tags: swift, tooling, codequality
Published: 2021-08-15 14:29:56

Effective, efficient, safe, easy to reason about and maintain code is what professionals strive towards.  In essence, clean.  Using tools that can help us clean-up code should be common practice.  

For Swift, the compiler finds many issues.  To go beyond a valid build based on legal swift and allowed Xcode configurations when building on Mac, adding linting and AST static analysis is not hard, and raises the bar towards more idiomatic code.  While Apple continues to refine and expand the basic built in tooling, the community has several tools to augment the build process in the meantime.  One of those tools is [swiftlint](https://realm.github.io/SwiftLint/)

The instructions on the website for installing and running the linter (both from the command line, and as a build step inside Xcode) are straightforward.  I won't go into installing or linting here.  How to run your first AST static analysis using `swiftlint`, which is experimental, is what I will describe from hands-on-experience beyond the somewhat sparse instructions on the tool's site.

To run the `swiftlint` AST static analysis, you must provide a successful, full-build log (incremental builds will not work, supposedly).  Let me underline this point: Xcode build logs are required, so you won't be doing this outside of a Mac at this time.  Normally, the build log from running the build process from the IDE is not exposed or kept in the project directory, so you have to do things a bit differently.  

Creating a project-local build log is possible, and configuring it to do as a default process is possible, but beyond the scope of this article.  There are reasons to and  not to include a build log by default locally.  At this time, Apple feels the convention should be to exclude them locally, so that is how we will roll.

Using the Xcode command-line tools (v12.5) in the terminal from the project root directory, issue the following command:

```
xcodebuild -list
```

Example output:

```
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -list

User defaults from command line:
    IDEPackageSupportUseBuiltinSCM = YES

Information about project "DifferenceOfSquares":
    Targets:
        DifferenceOfSquares
        DifferenceOfSquaresPackageDescription
        DifferenceOfSquaresPackageTests
        DifferenceOfSquaresTests

    Build Configurations:
        Debug
        Release

    If no build configuration is specified and -scheme is not passed then "Release" is used.

    Schemes:
        DifferenceOfSquares-Package

```

The relevant parts:

```
...
    Schemes:
        DifferenceOfSquares-Package
...
```  

Using the information from that list of schemes, you can now create a build log that can be passed to the analyzer.

Using the Xcode command-line tools (v12.5) in the terminal from the project root directory, issue the following commands:

```
xcodebuild -scheme DifferenceOfSquares-Package clean build > build.log
swiftlint analyze --compiler-log-path ./build.log
```

Example output:

```
Analyzing Swift files in current working directory
Analyzing 'DifferenceOfSquares.swift' (1/1)
Done analyzing! Found 0 violations, 0 serious in 1 file.

```

There you have it.  A static-analysis on my exercism project, 'DifferenceOfSquares' in the swift track.