Incremental build skipping changed file after adding new compiler flag
0 reputation · 23 Nov 2023, 01:09 UTC
0 reputation · 23 Nov 2023, 01:09 UTC
Rebuild a modified source file when a new compiler flag is added to a target in Xcode 14 with Swift 5.9.
After changing a compiler flag (e.g., adding a custom Swift compiler option), Xcode sometimes continues to skip the modified file during incremental builds, even though the file’s timestamp and content have changed. The build log shows the usual Incremental build messages but no recompilation of the altered file.
34775 reputation · 23 Nov 2023, 03:52 UTC
To force Xcode to recognize a new compiler flag and recompile modified source files, you must invalidate the build graph for the affected target. The most reliable method is to perform a Clean Build or manually purge the Derived Data folder for the project.
Xcode's build system tracks inputs for each CompileSwift action, including the source file timestamp and the OTHER_SWIFT_FLAGS setting. While a file change usually triggers a rebuild, adding a compiler flag changes the parameters of the build action. If the build system fails to propagate this setting change to the specific target's dependency graph, it may incorrectly determine that the existing object file is still valid despite the source change.
Note on Versioning: There is a likely mismatch in your environment description; Swift 5.9 is typically bundled with Xcode 15. Xcode 14 generally uses Swift 5.8. If you are using a custom toolchain (Swift 5.9 inside Xcode 14), the build system may experience synchronization issues between the IDE's project settings and the external compiler's expectations.
Swift Compiler > Custom Flags, rather than just the Project level, to ensure the dependency graph is updated for that specific binary.Product > Clean Build Folder (Cmd+Shift+K) to remove intermediate object files.~/Library/Developer/Xcode/DerivedData. This forces a complete recalculation of the module cache and dependency graph.xcodebuild clean build -scheme YourSchemeName -configuration Debug
To confirm the fix, check the build logs for the specific file:
CompileSwift action for the modified file.Are you using a custom Swift toolchain or a .xcconfig file to manage these flags? If flags are injected via .xcconfig, Xcode may not always trigger a target-wide invalidation upon file modification unless the .xcconfig file itself is saved/timestamped.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.