xcodes icon indicating copy to clipboard operation
xcodes copied to clipboard

Prevent keyNotFound error with unknown simulators

Open dnicolson opened this issue 2 years ago • 6 comments

This pull request prevents the following error when running xcodes runtimes when an "Unknown Platform Simulator" is installed:

Error: keyNotFound(CodingKeys(stringValue: "build", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "820783CB-C389-4006-B2D2-D063F3FD10A1", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"build\", intValue: nil) (\"build\").", underlyingError: nil))

If one accidentally runs xcrun simctl runtime add with an incompatible pre-iOS 16 Simulator runtime:

xcrun simctl runtime add com.apple.pkg.iPhoneSimulatorSDK15_0-15.0.1.1633542405.dmg

The following error will be displayed:

D: 94DEF0B1-1585-4D71-AB37-98AB08FDD000 <unknown platform> (Unusable - Missing Signature: Error Domain=SimDiskImageErrorDomain Code=3 "Missing Signature" UserInfo={NSLocalizedDescription=Missing Signature, unusableErrorDetail=})

The problem is that it's not immediately obvious that Xcode has still installed the runtime, but with a missing signature state: PixelSnap 2023-01-04 at 07 50 16@2x

And when xcodes runs xcrun simctl runtime list -j, there is no build key resulting in the error:

"820783CB-C389-4006-B2D2-D063F3FD10A1" : {
  "deletable" : true,
  "identifier" : "820783CB-C389-4006-B2D2-D063F3FD10A1",
  "kind" : "Disk Image",
  "path" : "\/Library\/Developer\/CoreSimulator\/Images\/Inbox\/820783CB-C389-4006-B2D2-D063F3FD10A1.dmg",
  "signatureState" : "Missing",
  "sizeBytes" : 5304795932,
  "state" : "Unusable",
  "unusableErrorDetail" : "Error Domain=SimDiskImageErrorDomain Code=3 \"Missing Signature\" UserInfo={NSLocalizedDescription=Missing Signature, unusableErrorDetail=}",
  "unusableErrorMessage" : "Missing Signature",
  "unusableSubstate" : "Missing Signature"
}

This change results in the following xcodes runtimes output:

-- Unknown --
Unknown 0 (Downloaded)

Another approach could be to filter out undecodable runtimes in the RuntimeList class, this actually resulted in more code though.

dnicolson avatar Jan 04 '23 07:01 dnicolson

Nice catch, thank you :)

Another approach could be to filter out undecodable runtimes in the RuntimeList class, this actually resulted in more code though.

I prefer that approach. it doesn't have to be a lot of code, actually, it's less code.

Remove all the installed runtimes with the state unusable, before looping over them, then you can then force-unwrap build, version, and platformIdentifier (and remove the unknown enum case).

I would also add a note at the end with how many unknown runtimes were found, with a suggestion to run xcrun simctl runtime list -j for more info.

Disclaimer: I contributed the runtimes feature but I'm not part of the team, so you might want to wait for their opinion/review.

StevenSorial avatar Jan 06 '23 05:01 StevenSorial

The amount of unknown runtimes is indicated by how many "Unknown" runtimes are listed, it should be comparable to what is in Xcode. It's not ideal that there is a 0 though, but further hacks wouldn't have been nice.

The original approach seemed to require using a custom decoder and possibly filtering out the items unable to be decoded afterward. Or do you mean keeping the optional keys so that the decoding doesn't fail?

dnicolson avatar Jan 06 '23 07:01 dnicolson

Or do you mean keeping the optional keys so that the decoding doesn't fail?

yeah, what i meant is removing the

-- Unknown --
Unknown 0 (Downloaded)

and replacing it at the end with (if more than 0):

Note: Found 1 unknown downloaded runtime(s). For more info run `xcrun simctl runtime list -j`.

you just have to add let unusableCount = installed.removeAll { $0.state == "Unusable" }.count before looping over the installed array, and then you can force-unwrap the newly optional values.

StevenSorial avatar Jan 06 '23 07:01 StevenSorial

I've made those changes, thanks for the input!

dnicolson avatar Jan 06 '23 07:01 dnicolson

Hi, is there any updates for this PR?

bobbyphtr avatar Jul 24 '24 06:07 bobbyphtr

@MattKiazyk can you please give an update?

dnicolson avatar Jul 28 '24 21:07 dnicolson