Skip to content

Commit b4cd776

Browse files
authored
Merge pull request #1336 from artemcm/59ResolveLibScanQueryCommandLinePaths
[5.9 🍒] Always quote path arguments when resolving command-lines for libSwiftScan queries
2 parents 41ac2b1 + 82b6b42 commit b4cd776

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ public extension Driver {
416416
static func itemizedJobCommand(of job: Job, useResponseFiles: ResponseFileHandling,
417417
using resolver: ArgsResolver) throws -> [String] {
418418
let (args, _) = try resolver.resolveArgumentList(for: job,
419-
useResponseFiles: useResponseFiles)
419+
useResponseFiles: useResponseFiles,
420+
quotePaths: true)
420421
return args
421422
}
422423

Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ extension FrontendTargetInfo {
124124
}
125125

126126
extension Toolchain {
127-
func printTargetInfoJob(target: Triple?,
128-
targetVariant: Triple?,
129-
sdkPath: VirtualPath? = nil,
130-
resourceDirPath: VirtualPath? = nil,
131-
runtimeCompatibilityVersion: String? = nil,
132-
requiresInPlaceExecution: Bool = false,
133-
useStaticResourceDir: Bool = false,
134-
swiftCompilerPrefixArgs: [String]) throws -> Job {
127+
@_spi(Testing) public func printTargetInfoJob(target: Triple?,
128+
targetVariant: Triple?,
129+
sdkPath: VirtualPath? = nil,
130+
resourceDirPath: VirtualPath? = nil,
131+
runtimeCompatibilityVersion: String? = nil,
132+
requiresInPlaceExecution: Bool = false,
133+
useStaticResourceDir: Bool = false,
134+
swiftCompilerPrefixArgs: [String]) throws -> Job {
135135
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
136136
commandLine.append(contentsOf: [.flag("-frontend"),
137137
.flag("-print-target-info")])

Tests/SwiftDriverTests/SwiftDriverTests.swift

+27-2
Original file line numberDiff line numberDiff line change
@@ -4861,17 +4861,42 @@ final class SwiftDriverTests: XCTestCase {
48614861

48624862
// In-process query
48634863
do {
4864-
let targetInfoArgs = ["-print-target-info", "-sdk", "bar", "-resource-dir", "baz"]
4864+
let targetInfoArgs = ["-print-target-info", "-sdk", "/bar", "-resource-dir", "baz"]
48654865
let driver = try Driver(args: ["swift"] + targetInfoArgs)
4866+
let printTargetInfoJob = try driver.toolchain.printTargetInfoJob(target: nil, targetVariant: nil,
4867+
sdkPath: .absolute(driver.absoluteSDKPath!),
4868+
swiftCompilerPrefixArgs: [])
4869+
var printTargetInfoCommand = try Driver.itemizedJobCommand(of: printTargetInfoJob, useResponseFiles: .disabled, using: ArgsResolver(fileSystem: InMemoryFileSystem()))
4870+
Driver.sanitizeCommandForLibScanInvocation(&printTargetInfoCommand)
48664871
let swiftScanLibPath = try XCTUnwrap(driver.toolchain.lookupSwiftScanLib())
48674872
if localFileSystem.exists(swiftScanLibPath) {
48684873
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
48694874
if libSwiftScanInstance.canQueryTargetInfo() {
48704875
XCTAssertTrue(try driver.verifyBeingAbleToQueryTargetInfoInProcess(workingDirectory: localFileSystem.currentWorkingDirectory,
4871-
invocationCommand: targetInfoArgs))
4876+
invocationCommand: printTargetInfoCommand,
4877+
expectedSDKPath: "/bar"))
48724878
}
48734879
}
4880+
}
48744881

4882+
// Ensure that quoted paths are always escaped on the in-process query commands
4883+
do {
4884+
let targetInfoArgs = ["-print-target-info", "-sdk", "/tmp/foo bar", "-resource-dir", "baz"]
4885+
let driver = try Driver(args: ["swift"] + targetInfoArgs)
4886+
let printTargetInfoJob = try driver.toolchain.printTargetInfoJob(target: nil, targetVariant: nil,
4887+
sdkPath: .absolute(driver.absoluteSDKPath!),
4888+
swiftCompilerPrefixArgs: [])
4889+
var printTargetInfoCommand = try Driver.itemizedJobCommand(of: printTargetInfoJob, useResponseFiles: .disabled, using: ArgsResolver(fileSystem: InMemoryFileSystem()))
4890+
Driver.sanitizeCommandForLibScanInvocation(&printTargetInfoCommand)
4891+
let swiftScanLibPath = try XCTUnwrap(driver.toolchain.lookupSwiftScanLib())
4892+
if localFileSystem.exists(swiftScanLibPath) {
4893+
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
4894+
if libSwiftScanInstance.canQueryTargetInfo() {
4895+
XCTAssertTrue(try driver.verifyBeingAbleToQueryTargetInfoInProcess(workingDirectory: localFileSystem.currentWorkingDirectory,
4896+
invocationCommand: printTargetInfoCommand,
4897+
expectedSDKPath: "/tmp/foo bar"))
4898+
}
4899+
}
48754900
}
48764901

48774902
do {

Tests/TestUtilities/DriverExtensions.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ extension Driver {
4545

4646

4747
public func verifyBeingAbleToQueryTargetInfoInProcess(workingDirectory: AbsolutePath?,
48-
invocationCommand: [String]) throws -> Bool {
49-
guard try Self.queryTargetInfoInProcess(of: toolchain,
50-
fileSystem: fileSystem,
51-
workingDirectory: workingDirectory,
52-
invocationCommand: invocationCommand) != nil else {
48+
invocationCommand: [String],
49+
expectedSDKPath: String) throws -> Bool {
50+
guard let targetInfo = try Self.queryTargetInfoInProcess(of: toolchain,
51+
fileSystem: fileSystem,
52+
workingDirectory: workingDirectory,
53+
invocationCommand: invocationCommand) else {
54+
return false
55+
}
56+
57+
guard let sdkPath = targetInfo.sdkPath else {
58+
return false
59+
}
60+
61+
if sdkPath.path.description != expectedSDKPath {
5362
return false
5463
}
5564
return true

0 commit comments

Comments
 (0)