Skip to content

Commit d96ab10

Browse files
committed
Only make input paths absolute when -working-directory is passed
1 parent 2a49cfe commit d96ab10

File tree

6 files changed

+141
-60
lines changed

6 files changed

+141
-60
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+13-10
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ public struct Driver {
329329
case .absolute(let path):
330330
return path
331331
case .relative(let path):
332-
return workingDirectory.map { AbsolutePath($0, path) }
332+
let cwd = workingDirectory ?? fileSystem.currentWorkingDirectory
333+
return cwd.map { AbsolutePath($0, path) }
333334
case .standardInput, .standardOutput, .temporary, .temporaryWithKnownContents, .fileList:
334335
fatalError("Frontend target information will never include a path of this type.")
335336
}
@@ -688,14 +689,14 @@ public struct Driver {
688689
compilerMode: compilerMode)
689690

690691
// Compute the working directory.
691-
let cwd = fileSystem.currentWorkingDirectory
692-
workingDirectory = try parsedOptions.getLastArgument(.workingDirectory).map { workingDirectoryArg in
692+
self.workingDirectory = try parsedOptions.getLastArgument(.workingDirectory).map { workingDirectoryArg in
693+
let cwd = fileSystem.currentWorkingDirectory
693694
return try cwd.map{ try AbsolutePath(validating: workingDirectoryArg.asSingle, relativeTo: $0) } ?? AbsolutePath(validating: workingDirectoryArg.asSingle)
694-
} ?? cwd
695-
696-
// Apply the working directory to the parsed options.
697-
if let workingDirectory = self.workingDirectory {
698-
try Self.applyWorkingDirectory(workingDirectory, to: &self.parsedOptions)
695+
}
696+
697+
if let specifiedWorkingDir = self.workingDirectory {
698+
// Apply the working directory to the parsed options if passed explicitly.
699+
try Self.applyWorkingDirectory(specifiedWorkingDir, to: &self.parsedOptions)
699700
}
700701

701702
let staticExecutable = parsedOptions.hasFlag(positive: .staticExecutable,
@@ -776,6 +777,8 @@ public struct Driver {
776777
}
777778

778779
if let workingDirectory = self.workingDirectory {
780+
// Input paths are prefixed with the working directory when specified,
781+
// apply the same logic to the output file map keys.
779782
self.outputFileMap = outputFileMap?.resolveRelativePaths(relativeTo: workingDirectory)
780783
} else {
781784
self.outputFileMap = outputFileMap
@@ -854,7 +857,7 @@ public struct Driver {
854857
self.buildRecordInfo = BuildRecordInfo(
855858
actualSwiftVersion: self.frontendTargetInfo.compilerVersion,
856859
compilerOutputType: compilerOutputType,
857-
workingDirectory: self.workingDirectory,
860+
workingDirectory: self.workingDirectory ?? fileSystem.currentWorkingDirectory,
858861
diagnosticEngine: diagnosticEngine,
859862
fileSystem: fileSystem,
860863
moduleOutputInfo: moduleOutputInfo,
@@ -3099,7 +3102,7 @@ extension Driver {
30993102
}
31003103

31013104
if let profileArgs = parsedOptions.getLastArgument(.profileUse)?.asMultiple,
3102-
let workingDirectory = workingDirectory {
3105+
let workingDirectory = workingDirectory ?? fileSystem.currentWorkingDirectory {
31033106
for profilingData in profileArgs {
31043107
if let path = try? AbsolutePath(validating: profilingData,
31053108
relativeTo: workingDirectory) {

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public extension Driver {
184184

185185
if supportInProcessSwiftScanQueries {
186186
var scanDiagnostics: [ScannerDiagnosticPayload] = []
187-
guard let cwd = workingDirectory else {
187+
guard let cwd = workingDirectory ?? fileSystem.currentWorkingDirectory else {
188188
throw DependencyScanningError.dependencyScanFailed("cannot determine working directory")
189189
}
190190
var command = try Self.itemizedJobCommand(of: preScanJob,
@@ -260,7 +260,7 @@ public extension Driver {
260260

261261
if supportInProcessSwiftScanQueries {
262262
var scanDiagnostics: [ScannerDiagnosticPayload] = []
263-
guard let cwd = workingDirectory else {
263+
guard let cwd = workingDirectory ?? fileSystem.currentWorkingDirectory else {
264264
throw DependencyScanningError.dependencyScanFailed("cannot determine working directory")
265265
}
266266
var command = try Self.itemizedJobCommand(of: scannerJob,
@@ -298,7 +298,7 @@ public extension Driver {
298298

299299
if supportInProcessSwiftScanQueries {
300300
var scanDiagnostics: [ScannerDiagnosticPayload] = []
301-
guard let cwd = workingDirectory else {
301+
guard let cwd = workingDirectory ?? fileSystem.currentWorkingDirectory else {
302302
throw DependencyScanningError.dependencyScanFailed("cannot determine working directory")
303303
}
304304
var command = try Self.itemizedJobCommand(of: batchScanningJob,

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ extension Driver {
401401
if let compilationDir = parsedOptions.getLastArgument(.fileCompilationDir)?.asSingle {
402402
let compDirPath = try VirtualPath.intern(path: compilationDir)
403403
try addPathArgument(VirtualPath.lookup(compDirPath), to:&commandLine, remap: jobNeedPathRemap)
404-
} else if let cwd = workingDirectory {
404+
} else if let cwd = workingDirectory ?? fileSystem.currentWorkingDirectory {
405405
let compDirPath = VirtualPath.absolute(cwd)
406406
try addPathArgument(compDirPath, to:&commandLine, remap: jobNeedPathRemap)
407407
}
@@ -837,7 +837,7 @@ extension Driver {
837837

838838
extension Driver {
839839
private func getAbsolutePathFromVirtualPath(_ path: VirtualPath) -> AbsolutePath? {
840-
guard let cwd = workingDirectory else {
840+
guard let cwd = workingDirectory ?? fileSystem.currentWorkingDirectory else {
841841
return nil
842842
}
843843
return path.resolvedRelativePath(base: cwd).absolutePath

Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ extension Driver {
186186
fileSystem: FileSystem,
187187
workingDirectory: AbsolutePath?,
188188
invocationCommand: [String]) throws -> FrontendTargetInfo {
189-
let cwd = try workingDirectory ?? fileSystem.tempDirectory
189+
let cwd = try workingDirectory ?? fileSystem.currentWorkingDirectory ?? fileSystem.tempDirectory
190190
let compilerExecutablePath = try toolchain.resolvedTool(.swiftCompiler).path
191191
let targetInfoData =
192192
try libSwiftScanInstance.queryTargetInfoJSON(workingDirectory: cwd,

Sources/SwiftDriver/Utilities/VirtualPath.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ extension VirtualPath {
389389
}
390390

391391
fileprivate func intern(virtualPath path: VirtualPath) -> VirtualPath.Handle {
392-
return self.queue.sync(flags: .barrier) {
392+
return self.queue.sync(flags: .barrier) {
393393
guard let idx = self.uniquer[path.cacheKey] else {
394394
let nextSlot = self.table.count
395395
self.uniquer[path.cacheKey] = .init(nextSlot)

0 commit comments

Comments
 (0)