Search code examples
swiftlibrariesswift5swift-package-manager

Failure to Parse Manifest File in Swift Package Manager With Custom Path


I am in the process of making a library to be used in multiple swift applications. I want my library to be easily installed via Swift Package Manager. I have created a blank Swift application and attempted to use the Swift Package Manager to automatically install this library from a Github repository.

When I first attempted to download the repo with the package manager I received an error saying that I needed to specify a path to my sources folder using the path property so I added the line

path: "Sources"

to specify the directory.

This is my package.Swift file in the root directory of my project

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

let package = Package(
    name: "TestProject",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "TestProject",
            targets: ["TestProject"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    path: "Sources",
    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: "TestProject",
            dependencies: []),
        .testTarget(
            name: "TestProjectTests",
            dependencies: ["TestProject"]),
    ]
)

Since adding the path: "Sources" to my code I've now received two new errors

Type 'Any' has no member 'library'

and

Failed to parse the manifest file

I'm not sure what is causing these errors as this code (minus the line added by me) was generated by Swift.


Solution

  • You specify a path with a target not with the entire package.

    So go to your:

    .target(
        name: "TestProject",
        dependencies: []),
    

    and change it to this:

    .target(
        name: "TestProject",
        dependencies: [],
        path: "Sources"),
    

    You will potentially need to do the same for your testTarget to point at its test sources (by default it looks at Tests/ relative to the package root.