Skip to content

Commit 739f7c4

Browse files
authored
Fix resource bundle lookup (#3463) (#3467)
* Fix resource bundle lookup * Add test check that `Bundle.main` is executed in the compiled binary * Ensure trailing slash for bundle path * Rely on Foundation.URL for path separators
1 parent 65e03e5 commit 739f7c4

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import PackageLoading
1717
import Foundation
1818
import SPMBuildCore
1919

20-
extension AbsolutePath {
21-
fileprivate var asSwiftStringLiteralConstant: String {
22-
return self.pathString.unicodeScalars
23-
.reduce("", { $0 + $1.escaped(asASCII: false) })
24-
}
20+
extension String {
21+
fileprivate var asSwiftStringLiteralConstant: String {
22+
return unicodeScalars.reduce("", { $0 + $1.escaped(asASCII: false) })
23+
}
2524
}
2625

2726
extension BuildParameters {
@@ -662,17 +661,13 @@ public final class SwiftTargetBuildDescription {
662661
guard let bundlePath = self.bundlePath else { return }
663662

664663
let stream = BufferedOutputByteStream()
665-
666-
let mainPath: AbsolutePath =
667-
AbsolutePath(Bundle.main.bundlePath).appending(component: bundlePath.basename)
668-
669664
stream <<< """
670665
import class Foundation.Bundle
671666
672667
extension Foundation.Bundle {
673668
static var module: Bundle = {
674-
let mainPath = "\(mainPath.asSwiftStringLiteralConstant)"
675-
let buildPath = "\(bundlePath.asSwiftStringLiteralConstant)"
669+
let mainPath = Bundle.main.bundleURL.appendingPathComponent("\(bundlePath.basename.asSwiftStringLiteralConstant)").path
670+
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"
676671
677672
let preferredBundle = Bundle(path: mainPath)
678673

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,9 @@ final class BuildPlanTests: XCTestCase {
24642464
let resourceAccessor = fooTarget.sources.first{ $0.basename == "resource_bundle_accessor.swift" }!
24652465
let contents = try fs.readFileContents(resourceAccessor).cString
24662466
XCTAssertTrue(contents.contains("extension Foundation.Bundle"), contents)
2467+
// Assert that `Bundle.main` is executed in the compiled binary (and not during compilation)
2468+
// See https://bugs.swift.org/browse/SR-14555 and https://github.com/apple/swift-package-manager/pull/2972/files#r623861646
2469+
XCTAssertTrue(contents.contains("let mainPath = Bundle.main."), contents)
24672470

24682471
let barTarget = try result.target(for: "Bar").swiftTarget()
24692472
XCTAssertEqual(barTarget.objects.map{ $0.pathString }, [

0 commit comments

Comments
 (0)