swift
swift copied to clipboard
Github: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Hello,
In the last two weeks, we are seeing Danger failing on Bitrise with the following crash Github: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
and the code generating the crash is
static func validatePRDescription(using danger: DangerDSL) {
guard let description = danger.github.pullRequest.body, !description.isEmpty else { // Crash here due to nil danger.github value
danger.warn("Please provide a summary in the Pull Request description")
return
}
}
After some examination, we saw that in DangerDSL.init(from:)
the Github object is initialized by decoding it if present (github = try container.decodeIfPresent(GitHub.self, forKey: .github)
). This means that this line can return a nil value, while the GitHub property is forced unwrapped (DangerDSL.getter:github
), which leads to a crash in our case.
Do you know why Github object fails to be initialized? Also, it wouldn't be better if CIs objects are optional, as each instance of Danger can in one CI only.
Note: The code above is part of wetransfer CI OSS and with their mock Github response everything works as expected. Maybe Github has changed their JSONs ?? 🤔
@sschizas thanks for reporting this, we're indeed experiencing the same issue and I'm trying to find the root cause!
The GitHub object should always be there if you are on GitHub, because is provided by DangerJS that will query the GitHub APIs and fail if they don't respond.
The reason to have try container.decodeIfPresent(GitHub.self, forKey: .github)
is because Danger doesn't know upfront if you are in GitHub or not, so parses the platforms only if present, however once the user uses .github
, .bitbucket
etc. (given the users know the platform they are using) that should always be present, hence the force unwrap, to avoid a not needed optionality, so for example you don't need to do something like .github?.nonOptionalProperty ?? fallback
which should not be needed.
I would be curious to see which JSON is coming back from DangerJS in that case, you can get that locally with danger-swift pr $YOUR_PR_LINK --json
I'm pretty sure this is all related to https://github.com/danger/swift/issues/513#issuecomment-1099137770