apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.88k stars 723 forks source link

Swift Scripting: error: unknown package 'Apollo' in dependencies of target 'Codegen' #1102

Closed alexandrethsilva closed 4 years ago

alexandrethsilva commented 4 years ago

Hi @designatednerd! First of all, thanks for the great effort on this script. It's really great to have someone looking into how to improve the existing process and making it better.

I'm not sure what may be up as I'm relatively new to Swift and Xcode development in general, but there's a problem when doing the setup described in the docs that is really cryptic as I can't find much about it somewhere else.

As for the setup, I'm currently on Xcode Version 11.4 (11E146) and therefore swift-tools 5.2.

When getting to the step 4 in the process (Update the dependencies section to grab the Apollo iOS library), I get the following message on the sidebar:

Showing All Messages: dependency 'Apollo' is not used by any target

Okay, makes sense, since I didn't add the target dependency yet. So I go ahead and do it. But then, immediately after adding it, I get the following:

unknown package 'Apollo' in dependencies of target 'Codegen'

My Package.swift currently looks like this:

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Codegen",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/apollographql/apollo-ios.git",
        from: "0.22.0")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(name: "Codegen",
            dependencies: [
            .product(name: "ApolloCodegenLib", package: "Apollo")
            ]),
        .testTarget(
            name: "CodegenTests",
            dependencies: ["Codegen"]),
    ]
)

As a sidenote, if I comment the dependency out, Xcode downloads the package from github and adds the deps to the sidebar, like here:

image

But when I add it:

image

I'm afraid I'm missing something rather simple, but then again, I couldn't get the process to work on Xcode 11.3.1 either as it was showing a similar error. I thought updating it would give me better chances, but no luck.

Do you think you have any tips on this?

Thanks again for the help! Cheers

alexandrethsilva commented 4 years ago

As a sidenote, I've tried recreating the issue by adding some other package (e.g. SwifterSwift) as I reckon it should probably work the same for any package and it didn't error. 👀

image

designatednerd commented 4 years ago

OK so I can see from the screenshots you fixed my first thought, which is that you need to be on 0.23.0 or higher, which is when the swift codegen stuff was released.

It won't work with 11.3.1 unless you're explicitly using the Swift 5.2 toolchain, since the swift-tools-version that comes with that is 5.1.

As far as I can tell, you've got things set up here mostly the same way I've got them in the SwiftScripts project's Package.swift, the only difference is that you're using the repo from a remote rather than from local. Since that's working fine, I'm not super-sure why yours isn't working.

I would try the obvious solutions (clean build, Derived Data Dance (quit xcode, delete derived data, reopen xcode, rebuild)). If none of that works let me know, I'll poke it some more.

ordazgustavo commented 4 years ago

Hi @designatednerd, I'm having the same problem, I tried the suggested "Derived Data Dance" but it still shows the same error :(

designatednerd commented 4 years ago

@ordazgustavo 1) To confirm, you are also using Xcode 11.4/Swift 5.2, correct? 2) (also for @alexandrethsilva) Do you have the same issue if you pull off master instead of 0.24.0? Hoping to kick a new version out the door tomorrow with an update to the CLI, but would be useful to know if this problem still exists on master.

ordazgustavo commented 4 years ago

@designatednerd That is correct, I'm using Xcode 11.4 and Swift 5.2

image

image

alexandrethsilva commented 4 years ago

Hi @designatednerd, sorry for the late feedback and thanks for the quick reply on your side!

I tried doing the cleanups you suggested, but no luck. When pulling master I still get the same, even when doing it right after the cleanup.

image

On a sidenote, when I was on Xcode 11.3.1 I was indeed trying it with swift-tools 5.1, but was stuck with a very similar error (slightly different wording) as I mentioned. 😞

alexandrethsilva commented 4 years ago

@designatednerd, @ordazgustavo it seems I figured it out after trying something rather obvious. 😬🎉

Apparently the problem is that the Package Name is not being automatically identified by the SPM and you have to add it manually. So, this worked:

image

I tried a number of combinations successfully after that, so all of these ran without problems for me:

.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", from: "0.23.0"),
.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", .exact("0.23.0")),
.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", from: "0.24.0"),
.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", .exact("0.24.0")),
.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", .branch("master")),

Sidenote for the unaware: .exact("0.22.0") doesn't work, but that's expected as you mentioned the first release with the feature is 0.23.0, so this would fail:

.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", .exact("0.22.0")),

But this works, as it ends up pulling 0.24.0 instead:

.package(name: "Apollo", url: "https://github.com/apollographql/apollo-ios.git", from: "0.22.0"),

Quick question to @designatednerd: would you like me to open a PR updating the docs?

Cheers and thanks again!