Search code examples
xcodeswift-package-manager

How to add a local package dependency to a local package in Xcode? How to nest package dependencies locally in Xcode?


I have created a local package within Xcode, with 'File -> New -> Package' and named it "BaseTypeExtension" (see image). So far so good, I can use this package in my other Xcode projects as expected (by 'File -> add Package Dependencies ... -> add Local ...').

Then I created another local package (again with 'File -> New -> Package)', but this package should depend on the first package BaseTypeExtensions. But when I try to add the dependency with 'File -> add Package Dependencies ...' surprisingly the option 'add Local ...' is not available anymore.

Can somebody help?

I want to implement such a nested package dependency but only locally (a remote repository e.g. on GitHub makes no sense to me at this moment, I don't want to share code with others, I just want to locally modularize my code using Swift Package Manager.

Any hint, workaround, link, ... is appreciated. Thanks in advance.

Screenshot of first package


Solution

  • Well, I finally found a solution for nested local packages. Interesting though, I got more help from ChatGPT, than from here.

    But the real help came from this site: https://theswiftdev.com/the-swift-package-manifest-file/

    Thanks to Tibor Bödecs, who shared his knowledge about package dependencies. The key was this line of code unter the comment // ... dependency must also be handled here on target level //

    I hope it helps somebody with the same issues.

    // swift-tools-version: 5.10
    import PackageDescription
    
    // Sample for dependency on other local package
    let package = Package(
        name: "SecondPackage",
        products: [
            .library(
                name: "SecondPackage",
                targets: ["SecondPackage"]
            ),
        ],
        // relative path to first package
        dependencies: [.package(path: "../BaseTypeExtension"),],
        targets: [
            .target(
                name: "SecondPackage",
                // ... dependency must also be handled here on target level
                dependencies: [.product(name: "BaseTypeExtension", package: "BaseTypeExtension")]
            ),
            .testTarget(
                name: "SecondPackageTests",
                dependencies: ["SecondPackage"]
            ),
        ]
    )