Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14124] SPM Places Bundle in incorrect folder when building frameworks in Xcode #56505

Open
cbaker6 opened this issue Jan 28, 2021 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@cbaker6
Copy link

cbaker6 commented Jan 28, 2021

Previous ID SR-14124
Radar None
Original Reporter @cbaker6
Type Bug

Attachment: Download

Environment

Xcode 12.4

macOS Catalina

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 369f1f32d09f19ea3c796655d5a88776

Issue Description:

When a framework has a dependency that has resources such as CoreData files, using SPM in Xcode via Project->Swift Packages places the bundle in Products->Debug-iphonesimulator instead of Products->Debug-iphonesimulator->PROJECTNAME.framework causing the framework to crash at runtime when testing.

Everything works correctly if you open "Package.swift" instead of the xcodeproj file.

The workaround is placing a run script in Build Phases->Run Script:

if [ -d "$BUILD_DIR/Debug-iphonesimulator/" ]; then
    cp -r $BUILD_DIR/Debug-iphonesimulator/*.bundle $BUILD_DIR/Debug-iphonesimulator/ParseCareKit.framework/
fi

This is important when using code integration on a framework that's building from an Xcode project. The issue was discovered in my CareKit PR [here|carekit-apple/CareKit#543 (comment).]

The problem doesn't occur when running an app in Xcode instead of Framework.

Below is file produced by SPM:

import class Foundation.Bundle

private class BundleFinder {}

extension Foundation.Bundle {
    /// Returns the resource bundle associated with the current Swift module.
    static var module: Bundle = {
        let bundleName = "CareKit_CareKitStore"

        let candidates = [
            // Bundle should be present here when the package is linked into an App.
            Bundle.main.resourceURL,

            // Bundle should be present here when the package is linked into a framework.
            Bundle(for: BundleFinder.self).resourceURL,

            // For command-line tools.
            Bundle.main.bundleURL,
        ]

        for candidate in candidates {
            let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
            if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
                return bundle
            }
        }
        fatalError("unable to find bundle named CareKit_CareKitStore")
    }()
}

And the candidate locations:

(lldb) po Bundle.main.resourceURLOptional<URL>
  ▿ some : file:///Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/
    - _url : file:///Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/(lldb) po Bundle(for: BundleFinder.self).resourceURLOptional<URL>
  ▿ some : file:///Users/coreybaker/Documents/Xcode_Projects/ParseCareKit/DerivedData/Build/Products/Debug-iphonesimulator/ParseCareKit.framework/
    - _url : file:///Users/coreybaker/Documents/Xcode_Projects/ParseCareKit/DerivedData/Build/Products/Debug-iphonesimulator/ParseCareKit.framework/(lldb) po Bundle.main.bundleURLfile:///Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/
  - _url : file:///Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/

Looking at the attached pictures shows that the bundle isn't placed in one of the candidate folders.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

1 participant