Why does SPM prevent target source paths from outside the package root?

I would like to be able to define a target which looks like this:

.target(
    name: "Foo", 
    dependencies:[],  
    path: "../../src/foo"),

But I run into this error when building:

error: target 'Foo' in package 'MyPackage' is outside the package root

What is the reason for this restriction?

1 Like

In the most common use case, for the package to be reachable as a versioned dependency, its Package.swift must be at the root of a Git repository. In those circumstances, anything outside the package root is also outside the repository root and will not travel with the versioned checkout. Since it would appear to work during testing, but would never work once deployed, it is disallowed from the outset to prevent confusion.

In local arrangements that aren’t versioned anyway, you can instead make that target its own package, and then depend on it with .package(name: "Foo", path: "../..").

1 Like