Skip to content

Commit c79be1c

Browse files
committed
[ArgsResolver] Allow overriding temporary directory
1 parent a14a5c7 commit c79be1c

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

Sources/SwiftDriver/Execution/ArgsResolver.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ public struct ArgsResolver {
2121
private let fileSystem: FileSystem
2222

2323
/// Path to the directory that will contain the temporary files.
24-
private let temporaryDirectory: AbsolutePath
24+
// FIXME: We probably need a dedicated type for this...
25+
private let temporaryDirectory: VirtualPath
2526

26-
public init(fileSystem: FileSystem) throws {
27+
public init(fileSystem: FileSystem, temporaryDirectory: VirtualPath? = nil) throws {
2728
self.pathMapping = [:]
2829
self.fileSystem = fileSystem
29-
self.temporaryDirectory = try withTemporaryDirectory(removeTreeOnDeinit: false) { path in
30-
// FIXME: TSC removes empty directories even when removeTreeOnDeinit is false. This seems like a bug.
31-
try fileSystem.writeFileContents(path.appending(component: ".keep-directory")) { $0 <<< "" }
32-
return path
30+
31+
if let temporaryDirectory = temporaryDirectory {
32+
self.temporaryDirectory = temporaryDirectory
33+
} else {
34+
let tmpDir: AbsolutePath = try withTemporaryDirectory(removeTreeOnDeinit: false) { path in
35+
// FIXME: TSC removes empty directories even when removeTreeOnDeinit is false. This seems like a bug.
36+
try fileSystem.writeFileContents(path.appending(component: ".keep-directory")) { $0 <<< "" }
37+
return path
38+
}
39+
self.temporaryDirectory = .absolute(tmpDir)
3340
}
3441
}
3542

@@ -56,7 +63,7 @@ public struct ArgsResolver {
5663
// Return the path from the temporary directory if this is a temporary file.
5764
if path.isTemporary {
5865
let actualPath = temporaryDirectory.appending(component: path.name)
59-
return actualPath.pathString
66+
return actualPath.name
6067
}
6168

6269
// If there was a path mapping, use it.
@@ -76,17 +83,25 @@ public struct ArgsResolver {
7683
"Platform does not support response files for job: \(job)")
7784
// Match the integrated driver's behavior, which uses response file names of the form "arguments-[0-9a-zA-Z].resp".
7885
let responseFilePath = temporaryDirectory.appending(component: "arguments-\(abs(job.hashValue)).resp")
79-
try fileSystem.writeFileContents(responseFilePath) {
80-
$0 <<< resolvedArguments[1...].map{ $0.spm_shellEscaped() }.joined(separator: "\n")
86+
87+
// FIXME: Need a way to support this for distributed build systems...
88+
if let absPath = responseFilePath.absolutePath {
89+
try fileSystem.writeFileContents(absPath) {
90+
$0 <<< resolvedArguments[1...].map{ $0.spm_shellEscaped() }.joined(separator: "\n")
91+
}
92+
resolvedArguments = [resolvedArguments[0], "@\(absPath.pathString)"]
8193
}
82-
resolvedArguments = [resolvedArguments[0], "@\(responseFilePath.pathString)"]
94+
8395
return true
8496
}
8597
return false
8698
}
8799

88100
/// Remove the temporary directory from disk.
89101
public func removeTemporaryDirectory() throws {
90-
try fileSystem.removeFileTree(temporaryDirectory)
102+
// Only try to remove if we have an absolute path.
103+
if let absPath = temporaryDirectory.absolutePath {
104+
try fileSystem.removeFileTree(absPath)
105+
}
91106
}
92107
}

0 commit comments

Comments
 (0)