Skip to content

Conversation

@thinkpractice
Copy link

@thinkpractice thinkpractice commented Dec 9, 2025

Hi @mattt ,

I'm sending this PR a little early, more of a heads-up, although the code and tests already build. @pedronahum and I are working to bring swift-huggingface to Linux. We made a lot of changes that we will detail below. Our main idea is bringing swift-huggingface (and hopefully swift-transformers later) to TaylorTorch. At this point in time we're still need to solve some issues on Linux, most notably:

  • MockURLProtocol.swift doesn't build on swift > 6.1 (we're currently building TaylorTorch with swift 6.3)
  • Migrating the TokenStorage classes to use gnome keyring on Ubuntu

also we need to still test the swift-huggingface on Linux, which we will hope to do some time later this week.

kind regards,

Tim

Add Linux Support

This PR adds full Linux compatibility to the swift-huggingface library.

Changes

Dependencies

  • Added swift-crypto package for cross-platform cryptographic operations (replaces Apple-only CryptoKit)

New Files

  • Sources/HuggingFace/Shared/URLSession+Linux.swift - Async wrappers for URLSession APIs (asyncData, asyncUpload, asyncDownload, asyncBytes) since Linux's FoundationNetworking lacks native async/await support
  • Sources/HuggingFace/OAuth/TokenStorage.swift - Cross-platform token storage implementations:
    • FileTokenStorage - File-based storage at ~/.cache/huggingface/token.json
    • EnvironmentTokenStorage - Reads from HF_TOKEN environment variable
    • CompositeTokenStorage - Combines multiple storage backends

Modified Files

  • Package.swift - Added swift-crypto dependency
  • Sources/HuggingFace/OAuth/OAuthClient.swift - Replaced CryptoKit with Crypto, added #if canImport(FoundationNetworking) conditionals for network calls
  • Sources/HuggingFace/OAuth/HuggingFaceAuthenticationManager.swift - Wrapped AuthenticationServices-specific code with #if canImport guards
  • Sources/HuggingFace/Hub/Pagination.swift - Converted HTTPURLResponse extension to free functions (parseNextPageURL(from:)) for Linux compatibility
  • Sources/HuggingFace/Hub/HubClient+Files.swift - Added Linux conditionals, MIME type fallback (since UniformTypeIdentifiers is unavailable), made resumeDownloadFile and DownloadProgressDelegate Apple-only
  • Sources/HuggingFace/Shared/HTTPClient.swift - Added Linux conditionals for all network operations, buffered SSE parsing for streaming responses
  • Sources/HuggingFace/Hub/File.swift - Removed unused CryptoKit import

Test Updates

  • Added #if canImport(FoundationNetworking) imports to all test files
  • PaginationTests.swift - Updated to use free function parseNextPageURL(from:)
  • HuggingFaceAuthenticationManagerTests.swift - Guarded with #if canImport(AuthenticationServices) (Apple-only)
  • MockURLProtocol.swift - Fixed Swift 6 sendability issue using nonisolated(unsafe)

Platform-Specific Features

Some features remain Apple-only due to platform limitations:

  • HuggingFaceAuthenticationManager (requires AuthenticationServices)
  • Keychain-based token storage
  • Download resume capability
  • Real-time streaming (Linux uses buffered approach)

Testing

  • swift build
  • swift build --build-tests
  • swift test ✅ (Linux)

@mattt
Copy link
Collaborator

mattt commented Dec 11, 2025

Hi @thinkpractice. Wow, this is exciting! Lemme take a some time to go over all of this. Aiming to have a review for you tomorrow.

@thinkpractice
Copy link
Author

thinkpractice commented Dec 11, 2025

@mattt thanks! Looking forward 😄 And let me know should you have any questions!

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.

3 participants