简体   繁体   中英

Xcode giving “Cannot find type 'TYPE' in scope” errors with Swift Package

I have been doing server-side development for some years now using server-side Swift, using Xcode as a convenient editing tool. And now that Xcode has support for Swift packages, the situation has generally improved. I'm not targeting any traditional Apple hardware so that might be the source of my problem, but seeing as this problem has only recently started to arise, I'm reporting it in case others are also running into it.

With one of my packages: http://github.com/SyncServerII/ServerDropboxAccount.git I can no longer build it with Xcode.

My intent in this is primarily to edit and get syntax errors out-- ie, I'm using Xcode as an editor. In some cases with these server-side packages, I can run unit tests in Xcode. In some cases, I have to run the tests on my Ubuntu target platform.

Just in the past few days I can no longer build this specific package in Xcode, nor at the command line on Mac OS.

I get errors like this:

/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:140:37: Cannot find type 'APICallResult' in scope
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds.swift:16:29: Cannot find type 'AccountAPICall' in scope
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds.swift:16:45: Cannot find type 'Account' in scope
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:49:14: Value of type 'DropboxCreds' has no member 'apiCall'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:49:111: Cannot infer contextual base in reference to member 'string'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:49:147: Cannot infer contextual base in reference to member 'json'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:14: Value of type 'DropboxCreds' has no member 'apiCall'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:133: Cannot infer contextual base in reference to member 'data'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:167: Cannot infer contextual base in reference to member 'json'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:190: Unable to infer type of a closure parameter 'apiResult' in the current context
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:201: Unable to infer type of a closure parameter 'statusCode' in the current context

I get the same errors when I use swift build at the command line on Mac OS. However, when I use swift build on Ubuntu, I get no errors-- the package builds cleanly.

On Mac OS:

MacBook-Pro-4:ServerDropboxAccount chris$ swift --version
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin19.6.0

On Ubuntu:

root@7b763a0a0a3f:~/Apps/ServerDropboxAccount# swift --version
Swift version 5.3.1 (swift-5.3.1-RELEASE)
Target: x86_64-unknown-linux-gnu

This may be a fringe use case, but before I full-fledged move away from using Xcode for this, I'd like to understand why this is happening. Thoughts are appreciated. Thanks!

I'm including a tag in this question for Kitura because my server is Kitura-based. And because Kitura has been undergoing some transitions lately in moving away from IBM to being community supported.

Well, I feel pretty foolish now. I had a conditional in one of the files in a dependent library which excluded some code.

#if os(Linux) || SERVER

// Code

#endif

I'm going to leave this question present just to show the fix. I needed to add a definition for SERVER into the Package.swift for that dependent library:

        .target(
            name: "ServerAccount",
            dependencies: [
                "ServerShared",
                // For new condition feature, see https://forums.swift.org/t/package-manager-conditional-target-dependencies/31306/26
                .product(name: "Kitura", package: "Kitura", condition: .when(platforms: [.linux, .macOS])),
                .product(name: "HeliumLogger", package: "HeliumLogger", condition: .when(platforms: [.linux, .macOS])),
                .product(name: "Credentials", package: "Kitura-Credentials", condition: .when(platforms: [.linux, .macOS])),
            ],
            swiftSettings: [
                // So I can do basic development and editing with this on Mac OS. Otherwise if some dependent library uses this it will not get Account related code. See Account.swift.
                .define("SERVER", .when(platforms: [.macOS], configuration: .debug)),
            ]),

Whoops. :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM