From 188a2125ce50202cccc82fdff71a5ed90ec771fd Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Fri, 30 Apr 2021 15:20:46 +0200 Subject: [PATCH 1/4] Fix resource bundle lookup --- Sources/Build/BuildPlan.swift | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift index 9cd5ce4e8ed..6d6bb74dba5 100644 --- a/Sources/Build/BuildPlan.swift +++ b/Sources/Build/BuildPlan.swift @@ -18,11 +18,10 @@ import Foundation import SPMBuildCore @_implementationOnly import SwiftDriver -extension AbsolutePath { - fileprivate var asSwiftStringLiteralConstant: String { - return self.pathString.unicodeScalars - .reduce("", { $0 + $1.escaped(asASCII: false) }) - } +extension String { + fileprivate var asSwiftStringLiteralConstant: String { + return unicodeScalars.reduce("", { $0 + $1.escaped(asASCII: false) }) + } } extension BuildParameters { @@ -663,17 +662,13 @@ public final class SwiftTargetBuildDescription { guard let bundlePath = self.bundlePath else { return } let stream = BufferedOutputByteStream() - - let mainPath: AbsolutePath = - AbsolutePath(Bundle.main.bundlePath).appending(component: bundlePath.basename) - stream <<< """ import class Foundation.Bundle extension Foundation.Bundle { static var module: Bundle = { - let mainPath = "\(mainPath.asSwiftStringLiteralConstant)" - let buildPath = "\(bundlePath.asSwiftStringLiteralConstant)" + let mainPath = Bundle.main.bundlePath + "\(bundlePath.basename.asSwiftStringLiteralConstant)" + let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)" let preferredBundle = Bundle(path: mainPath) From c476125294267b60bbebef029dd3df06f9c24495 Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Fri, 30 Apr 2021 15:35:30 +0200 Subject: [PATCH 2/4] Add test check that `Bundle.main` is executed in the compiled binary --- Tests/BuildTests/BuildPlanTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/BuildTests/BuildPlanTests.swift b/Tests/BuildTests/BuildPlanTests.swift index 3510c46d12d..78d7da559a6 100644 --- a/Tests/BuildTests/BuildPlanTests.swift +++ b/Tests/BuildTests/BuildPlanTests.swift @@ -2464,6 +2464,9 @@ final class BuildPlanTests: XCTestCase { let resourceAccessor = fooTarget.sources.first{ $0.basename == "resource_bundle_accessor.swift" }! let contents = try fs.readFileContents(resourceAccessor).cString XCTAssertTrue(contents.contains("extension Foundation.Bundle"), contents) + // Assert that `Bundle.main` is executed in the compiled binary (and not during compilation) + // See https://bugs.swift.org/browse/SR-14555 and https://github.com/apple/swift-package-manager/pull/2972/files#r623861646 + XCTAssertTrue(contents.contains("let mainPath = Bundle.main.bundlePath +"), contents) let barTarget = try result.target(for: "Bar").swiftTarget() XCTAssertEqual(barTarget.objects.map{ $0.pathString }, [ From ea58843edaaf215f3452425bf89cdde5511265f1 Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Fri, 30 Apr 2021 18:13:10 +0200 Subject: [PATCH 3/4] Ensure trailing slash for bundle path --- Sources/Build/BuildPlan.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift index 6d6bb74dba5..583309e3808 100644 --- a/Sources/Build/BuildPlan.swift +++ b/Sources/Build/BuildPlan.swift @@ -667,7 +667,7 @@ public final class SwiftTargetBuildDescription { extension Foundation.Bundle { static var module: Bundle = { - let mainPath = Bundle.main.bundlePath + "\(bundlePath.basename.asSwiftStringLiteralConstant)" + let mainPath = Bundle.main.bundlePath + "/" + "\(bundlePath.basename.asSwiftStringLiteralConstant)" let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)" let preferredBundle = Bundle(path: mainPath) From 8e008ba2a9744682e53189a3b9bbb4e14dba813b Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Fri, 30 Apr 2021 18:19:13 +0200 Subject: [PATCH 4/4] Rely on Foundation.URL for path separators --- Sources/Build/BuildPlan.swift | 2 +- Tests/BuildTests/BuildPlanTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift index 583309e3808..ea2389271a0 100644 --- a/Sources/Build/BuildPlan.swift +++ b/Sources/Build/BuildPlan.swift @@ -667,7 +667,7 @@ public final class SwiftTargetBuildDescription { extension Foundation.Bundle { static var module: Bundle = { - let mainPath = Bundle.main.bundlePath + "/" + "\(bundlePath.basename.asSwiftStringLiteralConstant)" + let mainPath = Bundle.main.bundleURL.appendingPathComponent("\(bundlePath.basename.asSwiftStringLiteralConstant)").path let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)" let preferredBundle = Bundle(path: mainPath) diff --git a/Tests/BuildTests/BuildPlanTests.swift b/Tests/BuildTests/BuildPlanTests.swift index 78d7da559a6..b4f375f76fe 100644 --- a/Tests/BuildTests/BuildPlanTests.swift +++ b/Tests/BuildTests/BuildPlanTests.swift @@ -2466,7 +2466,7 @@ final class BuildPlanTests: XCTestCase { XCTAssertTrue(contents.contains("extension Foundation.Bundle"), contents) // Assert that `Bundle.main` is executed in the compiled binary (and not during compilation) // See https://bugs.swift.org/browse/SR-14555 and https://github.com/apple/swift-package-manager/pull/2972/files#r623861646 - XCTAssertTrue(contents.contains("let mainPath = Bundle.main.bundlePath +"), contents) + XCTAssertTrue(contents.contains("let mainPath = Bundle.main."), contents) let barTarget = try result.target(for: "Bar").swiftTarget() XCTAssertEqual(barTarget.objects.map{ $0.pathString }, [