Skip to content

Commit 708c5d6

Browse files
authored
Fix resource bundle lookup (#3463)
* 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 79152ec commit 708c5d6

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
@@ -18,11 +18,10 @@ import Foundation
1818
import SPMBuildCore
1919
@_implementationOnly import SwiftDriver
2020

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

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

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

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)