Skip to content

Commit 499468d

Browse files
committed
Correct immediate mode -e arg handling
When parsing the options for an immediate-mode run, if we encounter what would otherwise be an input and the -e flag has already been specified, treat that argument *and* all subsequent arguments as arguments to the script being executed.
1 parent 818fcdd commit 499468d

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Sources/SwiftOptions/OptionParsing.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extension OptionTable {
4545
}
4646

4747
var parsedOptions = ParsedOptions()
48+
var seenDashE = false
4849
var index = arguments.startIndex
4950
while index < arguments.endIndex {
5051
// Capture the next argument.
@@ -58,10 +59,16 @@ extension OptionTable {
5859

5960
// If this is not a flag, record it as an input.
6061
if argument == "-" || argument.first! != "-" {
62+
// If we've seen -e, this argument and all remaining ones are arguments to the -e script.
63+
if driverKind == .interactive && seenDashE {
64+
parsedOptions.addOption(.DASHDASH, argument: .multiple(Array(arguments[(index-1)...])))
65+
break
66+
}
67+
6168
parsedOptions.addInput(argument)
6269

6370
// In interactive mode, synthesize a "--" argument for all args after the first input.
64-
if driverKind == .interactive && index < arguments.endIndex && !parsedOptions.lookupWithoutConsuming(.e).isEmpty {
71+
if driverKind == .interactive && index < arguments.endIndex {
6572
parsedOptions.addOption(.DASHDASH, argument: .multiple(Array(arguments[index...])))
6673
break
6774
}
@@ -90,6 +97,10 @@ extension OptionTable {
9097
currentDriverKind: driverKind)
9198
}
9299
}
100+
101+
if option == .e {
102+
seenDashE = true
103+
}
93104

94105
// Translate the argument
95106
switch option.kind {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,19 @@ final class SwiftDriverTests: XCTestCase {
302302
func testDashE() throws {
303303
let fs = localFileSystem
304304

305-
let driver1 = try Driver(args: ["swift", "-e", "print(1)", "-eprint(2)", "foo/bar.swift", "baz/quux.swift"], fileSystem: fs)
306-
XCTAssertEqual(driver1.inputFiles.count, 3)
307-
let tempFilesForDriver1 = driver1.inputFiles.filter {
308-
!["bar.swift", "quux.swift"].contains($0.file.basename)
309-
}
310-
XCTAssertEqual(tempFilesForDriver1.count, 1)
311-
XCTAssertEqual(tempFilesForDriver1[0].file.basename, "main.swift")
312-
let tempFileContentsForDriver1 = try fs.readFileContents(tempFilesForDriver1[0].file.absolutePath!)
305+
var driver1 = try Driver(args: ["swift", "-e", "print(1)", "-eprint(2)", "foo/bar.swift", "baz/quux.swift"], fileSystem: fs)
306+
XCTAssertEqual(driver1.inputFiles.count, 1)
307+
XCTAssertEqual(driver1.inputFiles[0].file.basename, "main.swift")
308+
let tempFileContentsForDriver1 = try fs.readFileContents(XCTUnwrap(driver1.inputFiles[0].file.absolutePath))
313309
XCTAssertTrue(tempFileContentsForDriver1.description.hasSuffix("\nprint(1)\nprint(2)\n"))
314310

315-
XCTAssertThrowsError(try Driver(args: ["swift", "-e", "print(1)", "baz/main.swift"], fileSystem: fs))
311+
let plannedJobs = try driver1.planBuild().removingAutolinkExtractJobs()
312+
XCTAssertEqual(plannedJobs.count, 1)
313+
XCTAssertEqual(plannedJobs[0].kind, .interpret)
314+
XCTAssertEqual(plannedJobs[0].commandLine.drop(while: { $0 != .flag("--") }),
315+
[.flag("--"), .flag("foo/bar.swift"), .flag("baz/quux.swift")])
316+
317+
XCTAssertThrowsError(try Driver(args: ["swiftc", "baz/main.swift", "-e", "print(1)"], fileSystem: fs))
316318
}
317319

318320
func testRecordedInputModificationDates() throws {

0 commit comments

Comments
 (0)