Skip to content

Commit 089c111

Browse files
authored
Merge pull request #1000 from compnerd/rsp
SwiftDriver: change the response file emission
2 parents 7b89381 + f434c76 commit 089c111

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Sources/SwiftDriver/Execution/ArgsResolver.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public final class ArgsResolver {
167167
}
168168

169169
private func createResponseFileIfNeeded(for job: Job, resolvedArguments: inout [String], forceResponseFiles: Bool) throws -> Bool {
170+
func quote(_ string: String) -> String {
171+
return "\"\(String(string.flatMap { ["\\", "\""].contains($0) ? "\\\($0)" : "\($0)" }))\""
172+
}
173+
170174
if forceResponseFiles ||
171175
(job.supportsResponseFiles && !commandLineFitsWithinSystemLimits(path: resolvedArguments[0], args: resolvedArguments)) {
172176
assert(!forceResponseFiles || job.supportsResponseFiles,
@@ -176,8 +180,11 @@ public final class ArgsResolver {
176180

177181
// FIXME: Need a way to support this for distributed build systems...
178182
if let absPath = responseFilePath.absolutePath {
183+
// Adopt the same technique as clang -
184+
// Wrap all arguments in double quotes to ensure that both Unix and
185+
// Windows tools understand the response file.
179186
try fileSystem.writeFileContents(absPath) {
180-
$0 <<< resolvedArguments[1...].map{ $0.spm_shellEscaped() }.joined(separator: "\n")
187+
$0 <<< resolvedArguments[1...].map { quote($0) }.joined(separator: "\n")
181188
}
182189
resolvedArguments = [resolvedArguments[0], "@\(absPath.pathString)"]
183190
}

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,9 @@ final class SwiftDriverTests: XCTestCase {
12051205
XCTAssertEqual(resolvedArgs[1].first, "@")
12061206
let responseFilePath = try AbsolutePath(validating: String(resolvedArgs[1].dropFirst()))
12071207
let contents = try localFileSystem.readFileContents(responseFilePath).description
1208-
XCTAssertTrue(contents.hasPrefix("-frontend\n-interpret\nfoo.swift"))
1209-
XCTAssertTrue(contents.contains("-D\nTEST_20000"))
1210-
XCTAssertTrue(contents.contains("-D\nTEST_1"))
1208+
XCTAssertTrue(contents.hasPrefix("\"-frontend\"\n\"-interpret\"\n\"foo.swift\""))
1209+
XCTAssertTrue(contents.contains("\"-D\"\n\"TEST_20000\""))
1210+
XCTAssertTrue(contents.contains("\"-D\"\n\"TEST_1\""))
12111211
}
12121212
// Forced response file
12131213
do {
@@ -1221,7 +1221,7 @@ final class SwiftDriverTests: XCTestCase {
12211221
XCTAssertEqual(resolvedArgs[1].first, "@")
12221222
let responseFilePath = try AbsolutePath(validating: String(resolvedArgs[1].dropFirst()))
12231223
let contents = try localFileSystem.readFileContents(responseFilePath).description
1224-
XCTAssertTrue(contents.hasPrefix("-frontend\n-interpret\nfoo.swift"))
1224+
XCTAssertTrue(contents.hasPrefix("\"-frontend\"\n\"-interpret\"\n\"foo.swift\""))
12251225
}
12261226

12271227
// No response file

0 commit comments

Comments
 (0)