Paul Cantrell Paul Cantrell - 3 months ago 8
Swift Question

Setting module name to be different from directory name in SwiftPM

I have a Swift library with a core module plus optional bonus modules. I would like to use the following directory layout, mapping to exported Swift package names as shown:

Taco/
Source/
Core/ → import Taco
Toppings/ → import TacoToppings
SideDishes/ → import TacoSideDishes


To my eyes, that’s a sensible-looking project layout. However, if I’m reading the docs right, this will pollute the global module namespace with unhelpful names like “Core”. It seems that SwiftPM will only export a module whose name is identical to the directory name, and thus I have to do this:

Taco/
Source/
Taco/
TacoToppings/
TacoSideDishes/


Is there a way to configure
Package.swift
to use the tidier directory layout above and still export the desired module names?

Alternatively, is it possible to make the Core, Toppings, and SideDishes modules internal to the project, and export them all to the world as one big Taco module?

Answer

There is not currently a clean way to do this, but it seems like a reasonable request. I recommend filing an enhancement request at http://bugs.swift.org for it.

There is one "hacky" way you can do this:

Create your sources in the desired internal layout:

Sources/Core
Sources/Toppings

Add additional symbolic links for the desired module names:

ln -s Core Sources/Taco
ln -s Toppings Sources/TacoToppings

Add an exclude directive to the manifest to ignore the non-desired module name:

let package = Package(
    name: "Taco",
    exclude: ["Sources/Core", "Sources/Toppings"]
)

is it possible to make the Core, Toppings, and SideDishes modules internal to the project, and export them all to the world as one big Taco module?

No, unfortunately there is no way to do this currently, and it requires substantial compiler work to be able to support.

Comments