apple / apple/swift-argument-parser

Crash with custom `Decodable` property that is not a command line option

Open
#683 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.8k
Forks
411
Avg merge
7d 13h
Merged PRs (30d)
17

Description

When a property in a ParsableCommand has a custom `Decodable` implementation, but it is not a Flag, Option or Argument, and the custom decodable implementation uses `nestedContainer`, the program will crash.

The crash is the following:
```
ArgumentParser/ArgumentDecoder.swift:135: Fatal error
[1] 77018 illegal hardware instruction [executable path] --help
```

**ArgumentParser version:** `1.5.0`
**Swift version:** Paste the output of `swift --version` here.
```
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: x86_64-apple-macosx13.0
```

### Checklist
- [x] If possible, I've reproduced the issue using the `main` branch of this package
- [x] I've searched for [existing GitHub issues](https://github.com/apple/swift-argument-parser/issues)

### Steps to Reproduce
```swift
@main
struct Main: ParsableCommand {
var property: MyCustomDecodableStruct = MyCustomDecodableStruct()
}

struct MyCustomDecodableStruct: Decodable {
enum CodingKeys: String, CodingKey {
case input
case output
}
enum InputCodingKeys: String, CodingKey {
case inputLanguage = "language"
}

enum OutputCodingKeys: String, CodingKey {
case outputLanguage = "language"
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let input = try container.nestedContainer(keyedBy: InputCodingKeys.self, forKey: .input)
let output = try container.nestedContainer(keyedBy: OutputCodingKeys.self, forKey: .output)

/* rest of the implementation */
}
}
```

### Expected behavior
This property should be ignored

### Actual behavior
```
ArgumentParser/ArgumentDecoder.swift:135: Fatal error
[1] 77018 illegal hardware instruction [executable path] --help
```

### Sidenote

Important to note is that on Swift 5.7, this will cause the compiler to crash in debug mode, but not in release mode

Contributor guide

Open the contributing guide

Research direction

Start with ArgumentParser/ArgumentDecoder.swift around line 135 and reproduce the crash using the supplied ParsableCommand and custom Decodable type with --help. Trace how a non-option property is decoded, then verify that the property is ignored and the command no longer crashes, including on the stated Swift 5.7 setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.