Package recognized but module not?

New to Swift and Xcode, so apologies if I missed something obvious.

I added a package (library) as a dependency by editing the Package.swift file (I did this because the File -> Swift Package Manager -> Add package dependency... dialog is greyed out for me. The good news is Xcode recognizes that the package has been added (locally via path) and even gives me a warning "dependency 'Blah' is not used by any target". The bad news is whenever I try to import the module, Xcode gives me an error: "No such module as 'Blah'".

I have ensured that the one enum inside the library is marked public. Still, I get an error every time I try to import it.

When editing a package, you control the dependencies directly with the manifest (Package.swift); Xcode so far lacks the means to manipulate the manifest code. When editing an Xcode project (an .xcodeproj and related files), Xcode manages dependencies for you with its user interface instead and there is no manifest. The “Add package dependency...” menu item will always be greyed out in the former case, but not in the latter.

This is telling you exactly what you need to do. You need to tell it which target needs the dependency by editing the particular .target in the manifest to add dependencies: [.product(name: "TheRelevantProduct", package: "ThePackageItComesFrom")]

Because no target depends on the module, none of the targets you can tell Xcode to build will in turn cause it to build that module. Thus the module isn’t there during compilation.

2 Likes

Thank you!

When I added the target as a product there was a name and product field. What is the difference exactly? In my case it was the same value for both parameters. And if I continue on this way, it will likely look like that in all packages.

.product(name: "Blah", package: "Blah")

A package can have multiple products. The name parameter selects which product you want, and the package parameter tells it which package to look in to find it.

You can find the list of products to choose from by looking in the dependency’s own Package.swift and finding the products parameter. (Such as here for example.)