Skip to content
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

Always quote path arguments when resolving command-lines for libSwiftScan queries #1335

Merged
merged 1 commit into from
Apr 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ public extension Driver {
static func itemizedJobCommand(of job: Job, useResponseFiles: ResponseFileHandling,
using resolver: ArgsResolver) throws -> [String] {
let (args, _) = try resolver.resolveArgumentList(for: job,
useResponseFiles: useResponseFiles)
useResponseFiles: useResponseFiles,
quotePaths: true)
return args
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/SwiftDriver/Jobs/PrintTargetInfoJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ extension FrontendTargetInfo {
}

extension Toolchain {
func printTargetInfoJob(target: Triple?,
targetVariant: Triple?,
sdkPath: VirtualPath? = nil,
resourceDirPath: VirtualPath? = nil,
runtimeCompatibilityVersion: String? = nil,
requiresInPlaceExecution: Bool = false,
useStaticResourceDir: Bool = false,
swiftCompilerPrefixArgs: [String]) throws -> Job {
@_spi(Testing) public func printTargetInfoJob(target: Triple?,
targetVariant: Triple?,
sdkPath: VirtualPath? = nil,
resourceDirPath: VirtualPath? = nil,
runtimeCompatibilityVersion: String? = nil,
requiresInPlaceExecution: Bool = false,
useStaticResourceDir: Bool = false,
swiftCompilerPrefixArgs: [String]) throws -> Job {
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
commandLine.append(contentsOf: [.flag("-frontend"),
.flag("-print-target-info")])
Expand Down
29 changes: 27 additions & 2 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4861,17 +4861,42 @@ final class SwiftDriverTests: XCTestCase {

// In-process query
do {
let targetInfoArgs = ["-print-target-info", "-sdk", "bar", "-resource-dir", "baz"]
let targetInfoArgs = ["-print-target-info", "-sdk", "/bar", "-resource-dir", "baz"]
let driver = try Driver(args: ["swift"] + targetInfoArgs)
let printTargetInfoJob = try driver.toolchain.printTargetInfoJob(target: nil, targetVariant: nil,
sdkPath: .absolute(driver.absoluteSDKPath!),
swiftCompilerPrefixArgs: [])
var printTargetInfoCommand = try Driver.itemizedJobCommand(of: printTargetInfoJob, useResponseFiles: .disabled, using: ArgsResolver(fileSystem: InMemoryFileSystem()))
Driver.sanitizeCommandForLibScanInvocation(&printTargetInfoCommand)
let swiftScanLibPath = try XCTUnwrap(driver.toolchain.lookupSwiftScanLib())
if localFileSystem.exists(swiftScanLibPath) {
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
if libSwiftScanInstance.canQueryTargetInfo() {
XCTAssertTrue(try driver.verifyBeingAbleToQueryTargetInfoInProcess(workingDirectory: localFileSystem.currentWorkingDirectory,
invocationCommand: targetInfoArgs))
invocationCommand: printTargetInfoCommand,
expectedSDKPath: "/bar"))
}
}
}

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

do {
Expand Down
19 changes: 14 additions & 5 deletions Tests/TestUtilities/DriverExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ extension Driver {


public func verifyBeingAbleToQueryTargetInfoInProcess(workingDirectory: AbsolutePath?,
invocationCommand: [String]) throws -> Bool {
guard try Self.queryTargetInfoInProcess(of: toolchain,
fileSystem: fileSystem,
workingDirectory: workingDirectory,
invocationCommand: invocationCommand) != nil else {
invocationCommand: [String],
expectedSDKPath: String) throws -> Bool {
guard let targetInfo = try Self.queryTargetInfoInProcess(of: toolchain,
fileSystem: fileSystem,
workingDirectory: workingDirectory,
invocationCommand: invocationCommand) else {
return false
}

guard let sdkPath = targetInfo.sdkPath else {
return false
}

if sdkPath.path.description != expectedSDKPath {
return false
}
return true
Expand Down