Skip to content

Response files should be named using the hash of the command line arguments only #1636

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/SwiftDriver/Execution/ArgsResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import class Foundation.NSLock
import func TSCBasic.withTemporaryDirectory
import protocol TSCBasic.FileSystem
import struct TSCBasic.AbsolutePath
import struct TSCBasic.SHA256

@_implementationOnly import Yams

Expand Down Expand Up @@ -208,7 +209,8 @@ public final class ArgsResolver {
assert(!forceResponseFiles || job.supportsResponseFiles,
"Platform does not support response files for job: \(job)")
// Match the integrated driver's behavior, which uses response file names of the form "arguments-[0-9a-zA-Z].resp".
let responseFilePath = temporaryDirectory.appending(component: "arguments-\(abs(job.hashValue)).resp")
let hash = SHA256().hash(resolvedArguments.joined(separator: " ")).hexadecimalRepresentation
let responseFilePath = temporaryDirectory.appending(component: "arguments-\(hash).resp")

// FIXME: Need a way to support this for distributed build systems...
if let absPath = responseFilePath.absolutePath {
Expand Down
21 changes: 21 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,27 @@ final class SwiftDriverTests: XCTestCase {
XCTAssertFalse(resolvedArgs.contains { $0.hasPrefix("@") })
}
}

func testResponseFileDeterministicNaming() throws {
#if !os(macOS)
throw XCTSkip("Test assumes macOS response file quoting behavior")
#endif
do {
let testJob = Job(moduleName: "Foo",
kind: .compile,
tool: .init(path: try AbsolutePath(validating: "/swiftc"), supportsResponseFiles: true),
commandLine: (1...20000).map { .flag("-DTEST_\($0)") },
inputs: [],
primaryInputs: [],
outputs: [])
let resolver = try ArgsResolver(fileSystem: localFileSystem)
let resolvedArgs: [String] = try resolver.resolveArgumentList(for: testJob)
XCTAssertTrue(resolvedArgs.count == 3)
XCTAssertEqual(resolvedArgs[2].first, "@")
let responseFilePath = try AbsolutePath(validating: String(resolvedArgs[2].dropFirst()))
XCTAssertEqual(responseFilePath.basename, "arguments-847d15e70d97df7c18033735497ca8dcc4441f461d5a9c2b764b127004524e81.resp")
}
}

func testSpecificJobsResponseFiles() throws {
// The jobs below often take large command lines (e.g., when passing a large number of Clang
Expand Down