# Difficulty Accessing Resources in a Package's Bundle

**URL:** <https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598>\
**Category:** Package Manager\
**Tags:** bundle, resources, packagemanager\
**Created:** [May 3, 2024, 1:21pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598 "2024-05-03T13:21:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Treata](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/treata/32/34544_2.png) [@Treata](https://forums.swift.org/u/Treata)\
**Post date:** [May 3, 2024, 1:21pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/1 "2024-05-03T13:21:13Z")

</div>

Based on the [docs](https://developer.apple.com/documentation/xcode/bundling-resources-with-a-swift-package#Access-a-resource-in-code), I tried to retrieve the URL of some resources in my package, but it seems to be impossible to do so ...

Doesn't matter if I use `Bundle.module` or `Bundle.main` to retrieve the `URL` of a file in a target of my package. In both cases, `url(forResource: String, withExtension: String?)` returned nil.

You can try the step by step guid as explained in [docs](https://developer.apple.com/documentation/xcode/bundling-resources-with-a-swift-package#Access-a-resource-in-code) in any package that you'd like, but you'll probably bump into the same issue as I did.

---

<div class="post-metadata">

**Author:** ![Treata](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/treata/32/34544_2.png) [@Treata](https://forums.swift.org/u/Treata)\
**Post date:** [May 3, 2024, 6:09pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/2 "2024-05-03T18:09:17Z")

</div>

When debugging the issue, I found out that the modules of my [package](https://github.com/Treata11/iShader/) are not loaded:

```swift
public extension Bundle {
    static let distortionEffect: Bundle = .module
}
...
    Bundle.distortionEffect.load()
    print(Bundle.distortionEffect.isLoaded) // Always is false

```

I'm not sure if it's related to the issue that I'm facing, but it's worth noting that I couldn't get the `isLoaded` property to be true.

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [May 3, 2024, 10:11pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/3 "2024-05-03T22:11:16Z")

</div>

I've been successfully using package resources for years but there has been some hurdles, here's a couple things off the top of my head:

Pretty sure you got the basics since you're following the docs and your Bundle.module gets generated.

- Metal librairies have a different name because they get processed, I think the extension changes to metallib
- Same with CoreData models, I don't remember their extension
- When localization is involved I've had issues with locale directory placement
- When resource generation is involved I've sometimes had issues with resources being placed outside the bundle
- Localized resource generation I've never managed to get my specific case working

I think the isLoaded property is meant for dynamic libraries within the bundle, like extensions. I don't remember where I saw that though so don't quote me on that!

---

<div class="post-metadata">

**Author:** ![Treata](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/treata/32/34544_2.png) [@Treata](https://forums.swift.org/u/Treata)\
**Post date:** [May 8, 2024, 10:03am UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/4 "2024-05-08T10:03:15Z")

</div>

> [@Lancelotbronner](#):
>
> I've been successfully using package resources for years

Could you provide an example of how you have utilized package resources in your projects, either within the package itself or outside of it?

I couldn't use a single package resource with any kind of format ... I tried the `settings.plist` as explained in docs, `metallib` as you explained and even files without an extension and none of them worked.

```swift
let settingsURL = Bundle.module.url(forResource: "settings", withExtension: "plist")

```

Examples like this simply would **not** work for any resource inside of a module.

---

<div class="post-metadata">

**Author:** ![Diggory](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/diggory/32/4320_2.png) [@Diggory](https://forums.swift.org/u/Diggory)\
**Post date:** [May 8, 2024, 12:30pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/5 "2024-05-08T12:30:53Z")

</div>

Here’s an example. SwiftCSV uses sample CSV files in its tests.

You’ll notice that Xcode and SwiftPM treat resources slightly differently.

> <https://github.com/swiftcsv/SwiftCSV/blob/master/Package.swift>

> <https://github.com/swiftcsv/SwiftCSV/blob/master/SwiftCSVTests/ResourceHelper.swift>

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [May 8, 2024, 1:22pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/6 "2024-05-08T13:22:26Z")

</div>

I tried with my [swift-raylib-examples](https://github.com/Lancelotbronner/swift-raylib-examples), there's a couple of them with resources, I've added print statements for `Bundle.module.url`.

Here's one that works for me with Xcode without any special casing:

```swift
import RaylibKit
import Foundation

@main struct LogoRaylib: Applet {
	let logo: Texture
	
	init() throws {
		Window.create(800, by: 450, title: "Example - Textures - Logo Raylib")
		Application.target(fps: 60)
		
		print(Bundle.module.url(forResource: "logo", withExtension: "png"))
		logo = try Texture(at: "logo.png", bundle: .module)
	}
	
	func draw() {
		Renderer2D.texture(logo, at: Window.size / 2 - logo.size / 2)
		Renderer2D.text("this IS a texture!", at: 360, 370, size: 10, color: .gray)
	}
}

```

It successfully prints `file:///Users/lancelot/Library/Developer/Xcode/DerivedData/raylib-examples-cbnjvpbaegxsqhadrivvnvswwhbn/Build/Products/Debug/swift-raylib-examples_Textures%20-%20Logo%20Raylib.bundle/Contents/Resources/logo.png`.

I tried with your package, here's a pro-tip for debugging resources: in Xcode use `Product > Show Build Folder in Finder` then navigate to `Products/Debug` and you'll be able to see all of the generated bundles.

Something I forgot about Metal libraries, they're all pre-compiled and merged into a per-module library named `default.metallib` so individual `.metal` (or `.metallib`) files don't exist.

I fixed your color distortion test:

```swift
/// Always succeeds!
func testBundleSourceString() throws {
	let url = Bundle.colorEffect.url(forResource: "default", withExtension: "metallib")
	XCTAssertNotNil(url, "Failed to load ColorEffect metal library")
	if let url {
		print(url)
	}
}

```

You can also access the library via `makeDefaultLibrary(bundle:)` if you need it outside SwiftUI:

> **[makeDefaultLibrary(bundle:) | Apple Developer Documentation](https://developer.apple.com/documentation/metal/mtldevice/2177054-makedefaultlibrary)**
>
> Creates a Metal library instance that contains the functions in a bundle’s default Metal library.

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [May 8, 2024, 1:26pm UTC](https://forums.swift.org/t/difficulty-accessing-resources-in-a-packages-bundle/71598/7 "2024-05-08T13:26:37Z")

</div>

That work-around is no longer necessary, I've cloned your package and replaced `ResourceHelper.url` with `Bundle.module.url`, deleted `ResourceHelper.swift` and the entire test suite built and all tests succeed.
