apollo-ios: Unable to run code generation

Bug report

Installing Apollo 1.0.0 via Cocoapods results in code generation error.

Versions

Please fill in the versions you’re currently using:

  • apollo-ios SDK version: 1.0.0
  • Xcode version: 14.0.1
  • Swift version: 5.7
  • Package manager: N/A
  • Cocoapods version: 1.11.3

Steps to reproduce

pod init

Add pod 'Apollo'

pod install

./Pods/Apollo/apollo-ios-cli init --schema-name MySchema ./Pods/Apollo/apollo-ios-cli generate

ApolloCodegenLib/resource_bundle_accessor.swift:11: Fatal error: could not load resource bundle: from /Users/xxx/ProjectName/Pods/Apollo/Apollo_ApolloCodegenLib.bundle or /Users/xxx/Library/Caches/CocoaPods/Pods/Release/Apollo/1.0.0-95555/.build/arm64-apple-macosx/release/Apollo_ApolloCodegenLib.bundle
zsh: trace trap  ./Pods/Apollo/apollo-ios-cli generate

Further details

Note that cocoapods is installed using Hombrew.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 63 (28 by maintainers)

Most upvoted comments

I’ll be honest; I find the instructions to get code generation to work in 1.0 to be very confusing. Looking at the available options listed in the documentation to install the codegen CLI:

  1. Swift PM: I thought this would work out for our project, but since we manage our Swift packages via Xcode, it doesn’t seem like this solution applies? (If so, I haven’t found out how).
  2. Cocoapods: We don’t use it so, not an option.
  3. Manual install: Not a fan of the fact that the CLI will not be tied to the Apollo library version but figured I’d give it a try so that I can at least be unblocked. Followed the instructions and got this:
% git clone https://github.com/apollographql/apollo-ios.git
% cd apollo-ios
% make build-cli
swift build --product apollo-ios-cli -c release
error: 'apollo-ios': Invalid manifest
/Users/jeannicolas/Code/swift/apollo-ios/Package.swift:4:8: error: no such module 'PackageDescription'
import PackageDescription
       ^
make: *** [build-cli] Error 1]

Any help would be appreciated…

It looks like in order to make network requests, the CLI may need to be run with the sandbox disabled. This was not clear to us from Apple’s (very limited) documentation on the switch package plug-in API. We’re going to do some more digging and update the documentation. Thanks do much to everyone who has helped dig into this issue so far!

I’m going to look into if there is a better/easier way for use to deploy the CLI. We felt that a swift package plug-in was the most user friendly way to do this, but it’s looking like the SPM plug-in system is still not mature enough to provide an experience here that is up to our standards.

I genuinely apologize to all of you who have started to attempt the migration to what we felt was a stable 1.0 version and have hit these issues. We’re going to spend time this coming week figuring out what the right option is for making code generation as seamless as possible for all of you.

After bit more digging—including closed issues (thanks Andy Kent)—figured it was a sandbox issue, and this sorts it:

swift package --disable-sandbox --allow-writing-to-package-directory apollo-fetch-schema -v --path "Sources/GraphQLClient/apollo-codegen-config.json"

(Note that --disable-sandbox must come before --allow-writing-to-package-directory.)

Anyone who’s watching this issue for a fix to the original issue of Fatal error: could not load resource bundle when using CocoaPods; the fix (#2548) has been merged with so if you point your Pod to the HEAD of main it should work for you now.

We’ve got a couple other fixes we want to get into main before publishing a 1.0.1 release.

Same problem. Can’t sleep, the customer is rushing the work. I’m really looking forward to news about the fix.

Hi, The same issue here.

Ok, I’ll have to dig into it a bit later; we’re all en route to Summit at the moment. Will try take a look later today. Apologies for the delay.

@elviva404 Please keep me updated. Want to make sure you’re able to get this working!

Hi @AnthonyMDev it works now 💃🏾.

After upgrading to 1.0.0 i also encounter issue during the codegen phase:

ApolloCodegenLib/resource_bundle_accessor.swift:11: Fatal error: could not load resource bundle: from /Users/XXXXXXXX/ios/Pods/Apollo/Apollo_ApolloCodegenLib.bundle or /Users/XXX/Library/Caches/CocoaPods/Pods/Release/Apollo/1.0.0-95555/.build/arm64-apple-macosx/release/Apollo_ApolloCodegenLib.bundle

A quick thought - I wonder if it’s related to not using the --path command, which means it’s looking for the config file in the location of the CLI binary?

init didn’t fail though so it seems it was able to write the config file to that location though.

Try this as a workaround - use an external path for both init and generate. See if that works because it then wouldn’t be looking in the .Pods folder.

Thanks @calvincestari , unfortunately adding the --path to both init and generate resulted in the same error. I used a path deliberately not in the Pods folder.

@calvincestari

Screenshot 2022-10-14 at 15 43 21

It works on GraphiQL.

This issue has been closed as the specific cause of it has been corrected. The fix for this will be released in 1.0.1 very soon and is available right now on the main branch.

If you are having other problems with codegen, please submit a new issue!

Thanks to everyone for all the info as we work this out. We unfortunately did not get enough users during the Beta period to find these edge cases. So we are working quickly to resolve all of them now and will release a patch version ASAP!

Thanks for the update @AnthonyMDev - appreciate you and @calvincestari replying to us all and sorting these issues so quickly.

This issue has been closed as the specific cause of it has been corrected. The fix for this will be released in 1.0.1 very soon and is available right now on the main branch.

If you are having other problems with codegen, please submit a new issue!

Thanks to everyone for all the info as we work this out. We unfortunately did not get enough users during the Beta period to find these edge cases. So we are working quickly to resolve all of them now and will release a patch version ASAP!

Looks like my generate errors were self-inflicted, and due to one or two paths being wrong, such that it was not finding any graphQL files, and passing an empty array; maybe a more accurate error than ‘Precondition failed: Expected JavaScript array but found: undefined’ would be in order. But despite all the trouble, have got it all migrated and working again.

@SilverTab You can run the plugin commands using Xcode like this. https://blog.eidinger.info/xcode-integration-of-swift-package-plugins-in-xcode-14

I’ll update the docs to add this next week!

@SilverTab your make build error seems pretty odd; PackageDescription is a Swift Package Manager defined object so as long as the dependencies are fetched correctly it should all be there. Could you try resetting the package cache or dependencies and see if that helps resolve that error - swift package reset

Thanks for pointing me in the right direction… I was able to get it to build by first doing: % sudo xcode-select --reset

@calvincestari Hi there! Had a quick question regarding the errors @indama is hitting, as my team just hit the same problem. Is there a reason why ApolloCodeGenLib used to work for iOS?

We set up an iOS project a few weeks ago and followed the documentation at the time that recommended using ApolloCodeGenLib over deprecated bash scripts for automatic code generation. I don’t recall the docs mentioning macOS only anywhere, and it was working great for us on iOS until this week. We’re trying to transition to the new Codegen CLI now.

@sammcode I think there might be some confusion between ApolloCodegenLib and the new apollo-ios-cli. ApolloCodegenLib has always only been designed to run on macOS because code generation does not need to be executed on mobile devices. You can view the ApolloCodegen class from 0.53.0 and it has the same macOS-only designation.

v1.0 moves away from recommending that code generation be executed with each build, which is what the bash scripting help before recommended. Note that you can still take that same approach but using the new apollo-ios-cli instead of the legacy Apollo tooling CLI; very similar names I’m sorry.

I am getting similar issue when trying to run cli (get via Pods):

m1entus@MacBook-Pro geneva-ios % ./Pods/Apollo/apollo-ios-cli generate
ApolloCodegenLib/resource_bundle_accessor.swift:11: Fatal error: could not load resource bundle: from /Users/m1entus/ios/Pods/Apollo/Apollo_ApolloCodegenLib.bundle or /Users/m1entus/Library/Caches/CocoaPods/Pods/Release/Apollo/1.0.0-95555/.build/x86_64-apple-macosx/release/Apollo_ApolloCodegenLib.bundle
zsh: illegal hardware instruction  ./Pods/Apollo/apollo-ios-cli generate

We are also using Xcode to manage Swift packages, so we were also a bit confused on how to set that up. Specifically, it was unclear how we can edit the apollo-codegen-configuration.json file in Xcode. We made a little bit of progress with the SPM solution, but we are still running into issues (will go into detail below).

Possible workaround for editing configuration file when using SPM

A workaround I tried with the SPM solution was opening the apollo-codegen-configuration.json file in a TextEditor rather than Xcode, which at least allowed us to edit the file.

Schema fetching errors

But when attempting to fetch the schema, we’re still running into errors.

We’ve tried with two working graphql endpoints and have gotten the same error with both of them. We are unclear as to if there is a problem with our configuration, or if there is a bug of some sort here.

The main error we keep getting is:

Error: A server with the specified hostname could not be found.

It also looks like its having an issue reading from a cache, as we get this error as well:

2022-10-04 14:37:29.452 apollo-ios-cli[31890:14972703] NetworkStorageDB:_openDBReadConnections: failed to open read connection to DB @ /Users/samuel.mcgarry/Library/Caches/apollo-ios-cli/Cache.db.  Error=14. Cause=unable to open database file

I tried installing via Package manager. When I add ApolloCodegenLib I have some errors. Screenshot at Oct 04 16-32-27

same problem here, struggling for 4 days and couldn’t solve it please help