Repository navigation
sdk: explain GitHub toolchain download failures #343
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
Open
mvanhorn
wants to merge
2
commits into
xtool-org:main
Choose a base branch
from
mvanhorn:fix/137-sdk-download-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import Foundation | ||
| import Dependencies | ||
| import HTTPTypesFoundation | ||
| import XKit | ||
|
|
||
| struct SDKGitHubArtifact: Equatable, Sendable { | ||
| var name: String | ||
| var progressLabel: String | ||
| var url: URL | ||
|
|
||
| static func toolset( | ||
| arch: SDKBuilder.Arch, | ||
| version: String = SDKBuilder.darwinToolsVersion | ||
| ) -> Self { | ||
| Self( | ||
| name: "Darwin toolset", | ||
| progressLabel: "Downloading toolset", | ||
| url: URL(string: """ | ||
| https://github.com/xtool-org/darwin-tools-linux-llvm/releases/download/\ | ||
| v\(version)/toolset-\(arch.rawValue).tar.gz | ||
| """)! | ||
| ) | ||
| } | ||
|
|
||
| static func openAppleMacros( | ||
| arch: SDKBuilder.Arch, | ||
| version: String = SDKBuilder.oamVersion | ||
| ) -> Self { | ||
| Self( | ||
| name: "OpenAppleMacros", | ||
| progressLabel: "Downloading OpenAppleMacros", | ||
| url: URL(string: """ | ||
| https://github.com/xtool-org/OpenAppleMacros/releases/download/\ | ||
| v\(version)/OpenAppleMacrosServer-\(arch.rawValue) | ||
| """)! | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| enum SDKGitHubDownloadError: Error, Equatable, CustomStringConvertible, LocalizedError { | ||
| case httpStatus(SDKGitHubArtifact, Int) | ||
| case emptyBody(SDKGitHubArtifact) | ||
| case network(SDKGitHubArtifact, String) | ||
|
|
||
| var artifact: SDKGitHubArtifact { | ||
| switch self { | ||
| case .httpStatus(let artifact, _), .emptyBody(let artifact), .network(let artifact, _): | ||
| artifact | ||
| } | ||
| } | ||
|
|
||
| var isRetryable: Bool { | ||
| switch self { | ||
| case .httpStatus(_, let status): | ||
| status == 429 || (500...599).contains(status) | ||
| case .emptyBody: | ||
| false | ||
| case .network: | ||
| true | ||
| } | ||
| } | ||
|
|
||
| var description: String { | ||
| let detail: String | ||
| switch self { | ||
| case .httpStatus(_, let status): | ||
| detail = "GitHub responded with HTTP \(status)." | ||
| case .emptyBody: | ||
| detail = "GitHub returned an empty response body." | ||
| case .network(_, let message): | ||
| detail = "Network error: \(message)" | ||
| } | ||
| return """ | ||
| Failed to download the \(artifact.name) from GitHub. | ||
|
|
||
| This error is not caused by your Xcode.xip. xtool fetches the Darwin \ | ||
| toolchain separately from: | ||
| \(artifact.url) | ||
|
|
||
| \(detail) | ||
|
|
||
| Check your network connection and GitHub availability, then retry \ | ||
| `xtool sdk install`. You do not need to re-download Xcode. | ||
| """ | ||
| } | ||
|
|
||
| var errorDescription: String? { description } | ||
| } | ||
|
|
||
| struct SDKGitHubDownloader: Sendable { | ||
| var maxAttempts: Int | ||
| var retryDelay: Duration | ||
| var sleep: @Sendable (Duration) async throws -> Void | ||
|
|
||
| init( | ||
| maxAttempts: Int = 3, | ||
| retryDelay: Duration = .milliseconds(250), | ||
| sleep: @escaping @Sendable (Duration) async throws -> Void = { try await Task.sleep(for: $0) } | ||
| ) { | ||
| self.maxAttempts = maxAttempts | ||
| self.retryDelay = retryDelay | ||
| self.sleep = sleep | ||
| } | ||
|
|
||
| func body(for artifact: SDKGitHubArtifact) async throws -> Data { | ||
| precondition(maxAttempts >= 1, "maxAttempts must be at least 1") | ||
| var lastError: SDKGitHubDownloadError? | ||
| for attempt in 1...maxAttempts { | ||
| do { | ||
| return try await fetch(artifact) | ||
| } catch let error as CancellationError { | ||
| throw error | ||
| } catch let error as SDKGitHubDownloadError { | ||
| lastError = error | ||
| guard error.isRetryable, attempt < maxAttempts else { throw error } | ||
| try await sleep(retryDelay) | ||
| } | ||
| } | ||
| throw lastError! | ||
| } | ||
|
|
||
| private func fetch(_ artifact: SDKGitHubArtifact) async throws -> Data { | ||
| @Dependency(\.httpClient) var httpClient | ||
| let response: HTTPResponse | ||
| let body: HTTPBody? | ||
| do { | ||
| (response, body) = try await httpClient.send(HTTPRequest(url: artifact.url)) | ||
| } catch let error as CancellationError { | ||
| throw error | ||
| } catch { | ||
| throw SDKGitHubDownloadError.network(artifact, String(describing: error)) | ||
| } | ||
| guard response.status == .ok else { | ||
| throw SDKGitHubDownloadError.httpStatus(artifact, response.status.code) | ||
| } | ||
| guard let body else { | ||
| throw SDKGitHubDownloadError.emptyBody(artifact) | ||
| } | ||
| return try await consume(body, artifact: artifact) | ||
| } | ||
|
|
||
| private func consume(_ body: HTTPBody, artifact: SDKGitHubArtifact) async throws -> Data { | ||
| let total: Int64? = switch body.length { | ||
| case .known(let known): known | ||
| case .unknown: nil | ||
| } | ||
| var data = Data() | ||
| do { | ||
| for try await chunk in body { | ||
| data.append(contentsOf: chunk) | ||
| reportProgress(artifact, written: Int64(data.count), total: total) | ||
| } | ||
| } catch let error as CancellationError { | ||
| throw error | ||
| } catch { | ||
| throw SDKGitHubDownloadError.network(artifact, String(describing: error)) | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| private func reportProgress(_ artifact: SDKGitHubArtifact, written: Int64, total: Int64?) { | ||
| guard let total else { return } | ||
| let progress = Int(Double(written) / Double(total) * 100) | ||
| print("\r[\(artifact.progressLabel)] \(progress)%", terminator: "") | ||
| fflush(stdoutSafe) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.