Xcode SwiftGen: build fails in project with multiple targets - ios

How should I configure SwiftGen when using Cocoapods and multiple targets?
I have a project with two targets (MyProject and MyProject Dev) which has SwiftGen integrated with Cocoapods.
I can build the first target with no problems whatsoever. However the 'Dev' target always fails. The script phase is the last step in Build Phases, however running it earlier or later seems to make no difference.
On failure, I've observed the following:
The generated .swift file has an import statement, 'import MyProject' which shouldn't be there
'No type named [ClassName] in module [MyProject]' OR 'No such module [MyProject]'
SwiftGen version I'm using is 5.2.1
Xcode 9.2, targeting iOS 10.0
SwiftGen from version 5 onwards requires a configuration file, so I've set one up as follows:
output_paths: Sources/Generated
storyboards:
- paths: MyProject/Storyboards/Base.lproj
templateName: swift4
output: MyProject/Storyboards/Storyboards.swift
params:
ignoreTargetModule: true
So far I've looked up documentation and the following issue:
https://github.com/SwiftGen/SwiftGen/issues/273
However the difference is that I'm using a different type of SG installation and my project fails to build (rather than simply not generating any resources)
Cleaning project, build folder and deleting derived data had no effect. I'm assuming that I may have missed something in configuration setup, but I can't see what that would be.
First question on here, so apologies if I missed something, will be happy to edit.

I had the same issue with generating Storyboard file. Try to modify your config file:
params:
ignoreTargetModule: true
module: <YOUR_PROJECT_NAME>
Hope it will help.

Hesitant to call this a solution, it's more of a workaround, but the following approach has at least allowed the projects to be built and run.
Remove the SwiftGen script phase from the 'Dev' target
Build the project using the main scheme that includes SwiftGen to generate all the necessary objects and constructs
Switch to Dev scheme and run the project.
The Strings.swift file created by SwiftGen is linked to both targets, so even after running the script in one scheme the resources within that file will be available to both regular and Dev targets.
Hope this helps someone.

Related

No such module 'RxBlocking' in test target

I'm using Carthage for setting up my project's RxSwift dependency. And recently, I tried to use XCFrameworks instead with the command carthage update --platform ios --use-xcframeworks. I've set up all that is needed to be set (eg. changing the .frameworks to .xcframeworks in the Link Binary With Libraries in the targets' Build Phase, etc). It worked and I can run or test in the simulator, until today that is. When I was trying to do the tests this morning, all of a sudden it failed (although it worked fine before) with the error No such module 'RxBlocking'. I've double-checked it and the xcframework has already been built in the Carthage/Build folder, and the xcframework has already been added to the test target's settings.
I've tried all of these but it all failed:
Setting the $(PROJECT_DIR)/Carthage/Build to the Framework Search Paths for the target
Deleting DerivedData/ and/or Carthage/ folders
Resetting the Mac
Can anybody help me to fix this? BTW, I'm on XCode 12.4 and Swift 5. Thanks.
This is actually because of the Apple tools that can't search for the correct Frameworks Search Path. The fix is in this link: https://github.com/ReactiveX/RxSwift/issues/2292#issuecomment-804045119

Build Libraries for Distribution” into ”Yes” Swift

In my project I have enabled the Build Libraries for Distribution” into ”Yes” into my SDK build setting, to prevent swift upgrades from causing a problem for third party libraries. In this case, When I Build with the project for SDK, I have set legacy mode but I am facing issue like below:


The legacy does not support Build Libraries for Distribution” into ”Yes”.
How to fix this? I can able to build the project new build system instead of legacy mode
The reason for setting legacy mode is When I set a new build system and prepare to build I am getting this below error. To fix this I am using legacy mode
Multiple commands produce '/Path/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/GoogleUtilities.framework':
1) Target 'GoogleUtilities-00567490' has create directory command with output '/Path//IntermediateBuildFilesPath/UninstalledProducts/iphoneos/GoogleUtilities.framework'
2) Target 'GoogleUtilities-54e75ca4' has create directory command with output '/Path//IntermediateBuildFilesPath/UninstalledProducts/iphoneos/GoogleUtilities.framework'
The workaround for this build system issue is to explicitly add all the needed GoogleUtilities subspecs to the Podfile. See the Podfile.lock file to get the list. Full details at https://github.com/firebase/firebase-ios-sdk/issues/4087

Build phase script is running before needed files are created in Xcode 10

https://github.com/realm/realm-cocoa/blob/master/scripts/strip-frameworks.sh
We are always using this script to remove architectures for dynamic frameworks. It has worked with Xcode 9.
In Xcode 10, it didn't remove the architectures.
Anything in Xcode 10 that could break this?
We have a theory that it is related to the new build system
Solution
The new build system is now running scripts in parallel if its dependency is already available or if it doesn't have any dependency.
The solution is to provide input files to tell "run scripts" not to run yet without those dependencies.
This is more elaborated in this post
Alternative Solution:
We used the legacy build system as a work around.
File > Workspace Settings

Project Builds and Runs, but does not Archive

I have a project which builds and runs fine on the simulator and device, but fails when archiving.
The project is very old, but I have created a separate component which I have added in the workspace.
The main project is written in Objective-C, the new project is written in Swift 4. The project also uses CocoaPods and also includes another subproject written in Swift 3.2
There are various errors which all boil down to the same thing, the sub project is not producing any output when it is compiled. OR it is just not compiled. I see no errors in the code itself, just when trying to reference it:
//1
error: /Users/<user>/Library/Developer/Xcode/DerivedData/<id>/Build/Intermediates.noindex/ArchiveIntermediates/<app name>/BuildProductsPath/Release-iphoneos/Framework.framework: No such file or directory`
//2
Signing Identity: "iPhone Developer: <redacted>“
/Users/<user>/Library/Developer/Xcode/DerivedData/<id>/Build/Intermediates.noindex/ ArchiveIntermediates/<app name>/InstallationBuildProductsLocation/Applications/<app name>/Frameworks/<framework>.framework: No such file or directory
Command /usr/bin/codesign failed with exit code 1
//3
#import ModuleName;
Module ‘ModuleName’ not found
Here’s what I’ve tried:
Clear derived data
Restart Mac
Add $(SRCROOT) to Main Target > Build Settings > Framework Search Paths > Release
Confirmed project is present in Embedded Binaries
Confirmed project is present in Linked Frameworks and Libraries
Removing and re-adding project to embedded binaries and frameworks and libraries
Removing the import declaration
I am opening the workspace and not the project
Skip install set to YES in Subproject build settings
Comparing build settings from the working sub project with the failing one (they are the same)
Running pod update
Changing Always Embed Standard Swift Libraries makes no difference either way
Other notes:
In /Users/<user>/Library/Developer/Xcode/DerivedData/<id>/Build/ Intermediates.noindex/ArchiveIntermediates/<app name>/ InstallationBuildProductsLocation/Applications/<app name>.app/Frameworks/ there is a .framework file for the other sub project and all the pods, but not for this one
In podfile, use_frameworks! is present
Update:
After running an archive today I am only seeing the error:
Module 'ModuleName' not found
The other errors are gone
The issue was to do with the iOS Deployment Target setting:
The main project: iOS 10
Sub project: iOS 11
In Debug it is building only for the current architecture, in Release it builds for all. Obvious once you know.
Setting the sub project to build for iOS 10 fixed the issue.
The most frustrating part: I double checked the build log and it doesn't mention the version issue anywhere :(
It's impossible to tell what exactly causing it to fail archiving. BUT I'm pretty sure I can give you the correct direction =]
Running on simulator or even a real device - compiles the project for "Debug"
Archiving tough, compiles for "Release"
I bet that if you set that running on simulator will compile on Release mode, it will fail!
Check it!!
If I correct you just need to set some of the Build Settings for Release to match Debug
My first guess is: All the search paths (Framework search path, and Runpath search path)
I saw a similar behavior here
I got it to archive after all.
It looked like the SDK-Project was missing a build configuration
Adhoc. Which Project used to archive the project for a specific build
scheme. I think the compiler was looking for modules in the
$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) path
I was having this error, and while my deployment targets did not match, that wasn't actually the fix for me. I had added a new build configuration that I was trying to use for my archive, but I forgot to run pod install after adding it. After running pod install, I was able to archive my app just fine, even though the deployment targets for some of the Pods are iOS 10 while the main app target is iOS 11.
Might be not relevant to you, but helpful to others:
If you using multi-modular SwiftPM-based architecture, then you should read errors carefully and find out if some of your package targets import some other one in sources without integrating it in the Package.swift file. In this case, even the release build will resolve dependencies somehow and succeed if some other package contains your dependency, but the archive will fail.
Package graph examples:
––––––––––
App
├– PackageA [target1(import PackageC.target1)]
└– PackageB [target1(import PackageC.target1)]
└– PackageC [target1]
👆 Builds with success, archives with errors (Can't find module PackageC.target1 in PackageA.target1)
––––––––––
App
├– PackageA [target1(import PackageC.target1)]
| └– PackageC [target1]
└– PackageB [target1(import PackageC.target1)]
└– PackageC [target1]
👆 Builds and archives with success
Cocoapods and Architecture settings
I had a Podfile that set EXCLUDED_ARCHS to arm64 in a post-install script and this was consistent with my main target. This setting was ok for building on Debug mode (x86_64) but didn't work for release building (Archives). I realised the script set EXCLUDED_ARCHS for the Pod targets even on physical devices (Any iOS SDK) rather than only on Simulators.
Steps
I ran pod deintegrate.
I deleted Derived Data.
I deleted Podfile.lock (now we have a fresh start).
I updated the Podfile iOS version to be the lowest supported version in each Pod.
Refactor EXCLUDED_ARCHS script.
Refactoring EXCLUDED_ARCHS script
I refactored:
config.build_settings['EXCLUDED_ARCHS'] = 'arm64'
from the Podfile post-install script to the following:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
...
config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
end
end
end
I haven't done watchOS here because it wasn't a watchOS app, however, we need to do this for all supported simulated device targets.
I left EXCLUDED_ARCHS to arm64 to Any iOS Simulator SDK in the main target for Release mode in the main target. The same logic here applies to other sims.
pod install
Archive again. :D

No such module only when archiving

I'm using xcode 7.3.1 at the moment. I have a workspace set up with an iOS app project and two framework projects.
MyiOSApp (imports 2 and 3)
AppSharedFramework (imports 3)
CompanySharedFramework (This we will split into its own thing one day)
I can build each of these projects fine when running on the simulator, I can also build then all for Profiling (which uses the release build). But as soon as I try archive either 1 or 2 the build fails with "No such module 'CompanySharedFramework'"
I've been struggling to get this to archive for quite some time, what could cause this issue during archive, but not during run/profile builds.
Any thoughts?
In my main project, a long time ago, I created a new "configuration schema" called "AppStore" that was duplicated from Xcode's default "Release" configuration. I added an entire Xcode project (a framework project) to my project. I was able to #import MyFramework; when "Running" because both projects had "Debug" as the schema for "Running" the app. When I went to Archive it, my main project was setup to use "AppStore" config (as opposed to the default of "Release") and my sub project did not have an "AppStore" config. Once I added "AppStore" to the sub project based on "Release", when I went to archive it worked great, built fully, because both projects now had the AppStore schema and the main project is the one saying to use that config when archiving, so when I just created it in the sub project things started working like you'd expect.
If you are using pod file in your project, please run pod install command even if you already run the command.
Good lucky!
#John Erck and #Victor Poroshenko inspired me.
Just one rule, if main project has a config named "AppStore", sub-projects in the same workspace which used by your main project also need a config named "AppStore".
Two situation in my case:
For the sub project you imported into the workspace manually, add the "AppStore" for it manually.
For pod files, just run pod install, it will generate the missing Pods-PROJECT_NAME.appstore.xcconfig for you.
Try deleting
Pods folder, xcworkspace, Podfile.Lock
and reinstalling Pods
pod install
When receiving "Module not found" error (while using #import) make sure module target version is not higher than main (application) target

Resources