Xcode 10, sourcery & swiftlint build phases order

2.6k views Asked by At

After updating to Xcode 10 there are some issues with initial project configurations. The steps look like this:

  1. Generating some files using Sourcery
  2. Linting with SwiftLint
  3. Build

And configuration works like this:

enter image description here

And this was working fine in Xcode 9, but apparently, it's not working under Xcode 10 build system. The issue is if I download repository (.generated files are not a part of the repository). And hit build it will show me results like:

... Using configuration file at '.sourcery.yml' Scanning sources... Found 239 types. Loading templates... Loaded 9 templates. Generating code... Finished. Processing time 0.491948962211609 seconds ...

So sourcery works ok, then linting:

Linting 'FromResponse.generated.swift' (1/186)

Works fine as well as a process, but in the end, build will fail with:

error: Build input files cannot be found: '/path/Generated/FromResponse.generated.swift', ...

So it's strange since a files are physically there and according to build settings file should be compiled later than generating them. How should I approach sourcery in build process then?

3

There are 3 answers

0
Yuchen On BEST ANSWER

We are able to "fix" this in a similar way as this Github issue:

https://github.com/mac-cain13/R.swift/issues/438#issuecomment-416982928

We have to add each of the generated files by Sourcery to the output folder and it will get picked up by the Xcode correctly.

enter image description here

0
Stavr0s On

I was having a similar problem with a buildphase calling mogenerator to build my data model classes. The approach given by @Yuchen should work.

@Andrew: To force always (re-)running this build phase, I add the line

touch .alwaysRun

as the last line in the shell command to run and mark the build phase "Input Files" to have $(SRCROOT)/.alwaysRun. That seems to do the trick.

1
AudioBubble On

This relates to changes in the New Xcode Build System that has been enabled by default in Xcode 10. If a build phase creates files which are needed as an input to a later build phase then it needs to specify them explicitly in the Output Files or Output File List.

In your example the Sourcery build phase is generating Swift source file(s) which are needed as input to the Compile Sources phase.

However, the issue at this point is that after you specify the output files for Sourcery, the build phase doesn't re-run every time as it sees the output file is already there. So far I haven't worked out a useable solution to this part, beyond running tools like Sourcery and SwiftGen manually or keeping the generated files in Git so that they are always present.