R.swift: Cannot archive project in Xcode 13 - input file 'R.generated.swift' was modified during the build

Greetings! A project that worked perfectly fine in Xcode 12.5.1 and earlier doesn’t archive anymore in Xcode 13 Beta 5. It fails with this error.

CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler (in target 'Vectornator' from project 'Vectornator')
<Build command>
error: input file '/PrivatePath/Other Sources/R.generated.swift' was modified during the build
error: input file '/PrivatePath/Other Sources/R.generated.swift' was modified during the build
Command CompileSwiftSources failed with a nonzero exit code

The project is an iOS app with a Catalyst version. iOS builds, runs and archives fine. Catalyst builds and runs fine, but fails to archive. Here’s the build phase setup. Bildschirmfoto 2021-09-13 um 18 18 31

Does anyone have any idea what’s going wrong? Bildschirmfoto 2021-09-13 um 18 15 40

I will retest with Xcode 13 RC tomorrow and update the issue.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 36 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I think (hope) I’ve found the source of “modified during the build” errors; Xcode 13 now appears to run scripts during an Index Build, while Xcode 12 didn’t do that. This can cause R.swift to run again during Indexing, while a Build is simultaneously compiling R.generated.swift.

The way to prevent this is to add this check in the Run Script:

if [ $ACTION != "indexbuild" ]; then
   rswift generate my_dir/R.generated.swift
fi
Screenshot 2021-10-07 at 13 58 06

Note: This works with the existing R.swift version, no need to update to the alpha.

If this turns out to be a stable solution, I’ll build this check for the Index Build into the rswift tool itself, so it doesn’t need to be done in the script.

I’m also experiencing the same issue with the official release of Xcode 13.

I have the same issue archiving iOS using XCode 13 release.

Update 2021-10-08: Deleted reference to proposed workaround that didn’t work

I filed a TSI with Apple Developer Technical Support and got a reply from the same engineer who helped three years ago with the update to Xcode 10. They reiterated that a Run Script build phase is not intended to be used for code generation, it is not a supported scenario.

Apple DTS made two suggestions:

  1. Place a script in a Build pre-action in the schema
  2. Consider using a custom build rule, as presented in this years WWDC: https://developer.apple.com/videos/play/wwdc2021/10210/?time=379

I’m not a fan of the pre-action in the schema, because I maintain multiple schemas (but you can use it if you like). And the build rule is not re-run on incremental builds, if none of the inputs change.


~External Build System target~

Note: I thought I could workaround the issue by using an External Build System target. Unfortunately it turned out this didn’t work on CI systems. So I’ve deleted that part of this post. See my post below for a workaround that does seem to work.

I’ve been looking into this issue, unfortunately this appears to be a bug in Xcode 13.

I’ve tried the workarounds mentioned in this thread as well what was mentioned in this stackoverflow question and this WooCommerce PR. None of them work reliably.

What does not fix the issue on Xcode 13:

  • Toggling “For install builds”, and “Based on dependency analysis” in the build phase
  • Using Output File Lists (.xcfilelist) instead of Output Files directly
  • Not specifying the Output File

The issue appears to be that Xcode 13 executes “Run Script” build phase way to often, occasionally in parallel with “Compile Sources” build phase. See this example project: XcodeScriptBuildPhaseIssue.

I will file a TSI with Apple, asking for a workaround, but this appears to be a bug in Xcode 13.

I’ve released R.swift 6.0.0. This has build-in support for checking against Index builds.

So updating the script is no longer necessary. Although some updates to the build phase are needed: https://github.com/mac-cain13/R.swift/blob/master/Documentation/Migration.md#upgrading-to-60

@tomlokhorst could you please share the steps you took to get it to work using the External Build System target?

I’ve found what appears to be a stable workaround:

Create a separate target of type “External Build System” that does the call to R.swift, and configure that target as a dependency of the regular target. I’ll investigate a bit further to see if this works in multiple situations. If it works in all cases, I’ll update the documentation with detailed steps.

Note: This requires 5.5.0-alpha.8, because it needs to pass in some extra arguments (infoPlistFile and target) via commandline arguments.