For months, CI and Xcode unit tests for a project at work have been plaguing me with a strange issue: At times and without obvious cause, some unit tests started failing to compile, saying that the classes of the app’s module could not get found – lots of Use of undeclared type and Use of unresolved identifier errors.

The types were clearly there. I was importing the module with @testable import module, keeping them accessible with the internal settings. Switching to a making them public and using a normal import, as suggested on Stack Overflow, did not make a difference.

More oddly, it all started working again without any related changes. No issue for weeks, and then it’s broken again. This time CI fails, but running in my Xcode, it’s all fine. What the…!

Through some digging, trial and error, removing things, rebuilding the tests from scratch, I finally figured out the issue:

The project is fairly complex with a hierarchy of frameworks, which have their own unit test packages. Only the unit test package for the app failed. The others always worked. Turns out, that one of the other test packages didn’t have it’s Produce Module Name set in Xcode’s build settings. That meant it was inheriting the project’s module name, which was the same name as the app’s module, which that unit test package for the app was trying to import. Apparently, occasionally the other test bundle compiles or ran first, and the problematical test package then somehow got that other test bundle’s module rather than the app’s, which is why none of the app module’s classes count be found.

Solution: Make sure your test modules have their Produce Module Name set and that it’s not the name of another module that another test bundle is trying to import.