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

Prevent keyNotFound error with unknown simulators #261

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dnicolson
Copy link

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 dnicolson requested a review from a team as a code owner January 4, 2023 07:27
@StevenSorial
Copy link
Contributor

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.

@dnicolson
Copy link
Author

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?

@StevenSorial
Copy link
Contributor

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.

@dnicolson
Copy link
Author

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

Copy link
Contributor

@MattKiazyk MattKiazyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this @dnicolson 🎉

Can we remove the force unwraps? As well as add a quick test for these? Let me know if you need some help.

installed.forEach { runtime in
let resolvedBetaNumber = downloadablesResponse.sdkToSeedMappings.first {
$0.buildUpdate == runtime.build
$0.buildUpdate == runtime.build!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor this section a bit?

Can we not use force unwrap. As a rule, I don't use them. Totally agree that it should never happen but it's bitten me in the past. Perhaps making an overloaded PrintableRuntime() to allow optionals?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how best to achieve this, feel free to make any changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my 2 cents here, personally I also try to avoid them if I can, but in this case, I would prefer a force unwrap to fail than hiding the error and providing an empty string as a default value, in that case, we would know that xcrun simctl runtime list printed something that we didn't account for, and there is probably a new state of a runtime that we should write code for. I see it as a assert/precondition and not a code smell.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MattKiazyk can you please give an update on how you would like to proceed here?

@@ -90,6 +88,9 @@ public class RuntimeInstaller {
}
}
Current.logging.log("\nNote: Bundled runtimes are indicated for the currently selected Xcode, more bundled runtimes may exist in other Xcode(s)")
if unusableCount > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this vs having the original Unknown 👍

@@ -108,18 +108,18 @@ extension DownloadableRuntime {
}

public struct InstalledRuntime: Decodable {
let build: String
let build: String?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#picky can we add a comment on why these are optional

@Bobbyphtr
Copy link

Hi, is there any updates for this PR?

@dnicolson
Copy link
Author

@MattKiazyk can you please give an update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants