From 47472aee8021b9cea4783b2cff379eb318abeb4a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 6 Oct 2020 20:02:52 -0700 Subject: [PATCH] Build: quote the path string for bundle resources We need to ensure that we escape `\` for paths in strings. This enables using bundles for tests on Windows. --- Sources/Build/BuildPlan.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift index 0742c99e9ff..f8ea375864d 100644 --- a/Sources/Build/BuildPlan.swift +++ b/Sources/Build/BuildPlan.swift @@ -16,6 +16,13 @@ import PackageLoading import Foundation import SPMBuildCore +extension AbsolutePath { + fileprivate var asSwiftStringLiteralConstant: String { + return self.pathString.unicodeScalars + .reduce("", { $0 + $1.escaped(asASCII: false) }) + } +} + extension BuildParameters { /// Returns the directory to be used for module cache. public var moduleCache: AbsolutePath { @@ -576,13 +583,17 @@ 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 = Bundle.main.bundlePath + "/" + "\(bundlePath.basename)" - let buildPath = "\(bundlePath.pathString)" + let mainPath = "\(mainPath.asSwiftStringLiteralConstant)" + let buildPath = "\(bundlePath.asSwiftStringLiteralConstant)" let preferredBundle = Bundle(path: mainPath)