Importing Code into Package.swift

Is there a way to import local code into Package.swift?

i.e.

// swift-tools-version:5.3

import PackageDescription
import Local Module/Swift File

2 Likes

anything? I'd love to be able to centralize code and helpers for packages.

A manifest can import, but there is no where to tell it where to find any modules Swift doesn’t know about automatically. The imports that work are those from the Swift toolchain (Foundation, Dispatch, XCTest), and those intrinsic to the platform (Darwin, Glibc, WinSDK, etc.).

You’ll have to write your centralized helpers as separate tools which modify the package manifest before it gets checked into source control.

So it's a no go on importing swift files via relative path to the Package.swift?

Can I write this up as a feature request? I love that the configuration is code, but it would be great to be able to centralize things, especially in the case of a mono-repo where we have many packages and having custom modules to help manage them is a big benefit.

Swift itself doesn’t have the syntax for that yet. The most recent thread about that I can find is here.

Certainly.

To make the above proposal also reliable for use in a manifest with a relative path, you would need to codify some additional specifications about how SwiftPM treats a package repository. For example, at the moment SwiftPM is allowed to fetch the manifest on its own without the rest of the repository while it figures out which version of the rest it actually needs. Something would need to be done to ensure the relative path doesn’t end up pointing into a void.

1 Like

Do you know if there is a place where you can stick a custom module that would make it available to the package manifest currently?

Aside from modifying the toolchain itself, there is no such place.

If we are talking commandline SwiftPM, there is the -Xmanifest option (see here ) to pass additional flags to the build of the manifest itself which you could use to pass additional search paths. This option is mostly for bringing up new platforms, but you could use it in this case as well.

1 Like

Where can I add a feature request for SPM?