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

Fix resource bundle lookup #3463

Merged
merged 4 commits into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 6 additions & 11 deletions Sources/Build/BuildPlan.swift
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.bundleURL.appendingPathComponent("\(bundlePath.basename.asSwiftStringLiteralConstant)").path
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"

let preferredBundle = Bundle(path: mainPath)

Expand Down
3 changes: 3 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Expand Up @@ -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."), contents)

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