Search code examples
swiftxcodexcode13

Xcode 13 error: input file [...] was modified during the build


Xcode 13 gives me a hard time building my project which consists of targets with build phases that generate code.

E.g. one build phase generates the file Secrets+Generated.swift by simply using a shell script that echoes some code into that file.

enter image description here

The build phase defines that file as an output file. No input file, no input file list and no output file list, as only that one file is created/modified.

Almost all the time when building the project, the build fails:

error: input file '[ProjectPath]/Secrets+Generated.swift' was modified during the build
error: input file '[ProjectPath]/Secrets+Generated.swift' was modified during the build
Command CompileSwiftSources failed with a nonzero exit code

Has anyone encountered that issue or knows what to do? I tried toggling the checkbox "Based on dependency analysis", but that didn't help. I didn't have that issue when using Xcode 12. Interesting to note is the duplicate error message despite having only one build phase generating that particular file.

Btw. I get the same problem when using code generation tools like swiftgen, Sourcery or Cuckoo.

Edit: Here are my build phases: enter image description here The three marked build phases all generate one such file. All of them fail occasionally. I do not know if that makes a difference, but these are defined for only one target (Notification Service Extension) which is a dependency of my main app target, so it gets triggered only once when I build the app.


Solution

  • I'm debugging a similar issue in R.swift, I think I've found the cause of the issue.

    Xcode 13 appears to execute Run Script build phases during index builds. This can cause a file to be written to by an index pass, in parallel with a build.

    To check if this is happening for you, add this to your build script: say $ACTION
    On Xcode 12, it only says "build", but in Xcode 13 I hear it saying "indexbuild" during compilation, whenever I save .swift files or otherwise navigate through my code.

    To prevent your script from running during indexing, add this check:

    if [ $ACTION != "indexbuild" ]; then
      # your script here 
    fi