Swift Package-generated Xcode project doesn't include resources

Hello!

We have some resources included as part of our test target like so:

Code Block
        .testTarget(
            name: "MyProjectTests",
            dependencies: ["MyProject"],
            resources:
[.process("Assets/Test_5s.mov")])


However, when running the following command:

swift package generate-xcodeproj

the generated package does not include the assets. Additionally the code to access the resources won't compile because there is no auto-generated file to provide the 'module' extension for 'Bundle'.

Code Block
Bundle.module.url(forResource: "Test_5s", withExtension: "mov")

results in a "Type 'Bundle' has no member 'module'" compilation error.

Is this a known issue, or am I approaching this incorrectly if I want to have a project for the package?

Currently using Xcode 12 beta 2

Answered by DTS Engineer in 647400022
Swift package resources, including access through Bundle.module, are available when directly using the package through the Swift Package integration in Xcode. They are not available through generated Xcode projects.
I also had problems with resources visibility in SPM package with generated project file. And I've found out that if you don't generate Xcode Project file for the package but open package folder with Xcode instead there are no errors with Bundle.module and you even can see generated code for searching your module resources by CTRL+CMD+Click on .module. Also there won't be any problems with visible folders since Xcode shows all folder files.

Do you have any particular reason for project file generation? I think in most cases open a folder will be enough.
We have SwiftUI files which will not preview without a target. In this case, opening the package directly as a folder through Xcode is not sufficient.
Hello, we have the same problem with Bundle.module not accessible when opening our xcodeproj but requires it for our CI and other convenience tools as SwiftLint. Did you guys find any solution / workaround?

Still a little bit hard to know if this possibility was planned or not as Bundle.module is generated by SPM not Xcode.
zocario - we have a demo app that uses our package and have created a workspace containing the demo project and the package. We are also using SwiftLint and have opted to run the linter when building the demo app target. We mostly work from this workspace so it is sufficient for our needs.

We have recently discovered that having dependencies on a package breaks SwiftUI previews in that package. There is a radar for this bug already. It seems that having a project should not be necessary for those previews to work, so my first reply in this thread can be ignored.

If you find a way to get this working with your CI please let us know
Thanks jeff97 for the details, I've filled a bug report (FB8089986) that you can see on open radar (I don't know why but I can't add the link it this post). We'll see - if by chance they answer - if this is a bug / known issue.. did you create a bug report on your side?
Still facing the same issue here using Xcode 12 final version. As anyone come up with a solution?
I was told that the function to generate an Xcode project predates support for resources and thus doesn’t add the building of resource bundles.
Me too.

Only swift package preview can not run code with "requestAuthorization"...
So I need a xcode project target.
Is there any update to this? I'm having the same problem
Swift package resources, including access through Bundle.module, are available when directly using the package through the Swift Package integration in Xcode. They are not available through generated Xcode projects.
Are there any plans to support this functionality in the future?

That happens because generated project do not contain resources in build phase. So generate-xcodeproj is deprecated and you need to implement copying resources on your own.

For anyone still facing this problem, the workaround I've been using is to define Bundle.module when compiling from Xcode.

import Foundation

// MARK: - BundleFinder

// when compiled from xcodeproj, this code is necessary for bundling resources
// but when compiling as a swift package, these cause compilation errors: "redefinition of `module`".
// The XCODE_COMPILE pre-processor flag is set in the xcodeproj settings and is only present
// when the code is compiled from Xcode (or xcodebuild)
#if XCODE_COMPILE
 private class BundleFinder {}

 extension Bundle {
  static var module = Bundle(for: BundleFinder.self)
 }
#endif

Creating a flag XCODE_COMPILE in the Active Compilation Conditions (for release and debug) for your xcodeproj build settings allows your project to compile when using Xcode/xcodebuild while at the same time not breaking your swift package w/ a redefinition of Bundle.module.

Swift Package-generated Xcode project doesn't include resources
 
 
Q