Skip to content

WASM/WASI compilation fails due to atomic file operations #39

@leogdion

Description

@leogdion

Summary

RadiantKit fails to compile for WASM/WASI targets due to the use of atomic file operations, which are unavailable in the WASI environment because temporary files are not supported.

Environment

  • RadiantKit Version: 1.0.0-beta.5
  • Platform: WASM32-WASI
  • Swift Version: 6.0+

Error Details

File: Sources/RadiantDocs/Primitives/InitializablePackageOptions.swift:84

error: 'atomic' is unavailable: atomic writing is unavailable in WASI because temporary files are not supported
84 |     if options.contains(.atomic) { dataOptions.insert(.atomic) }
   |                                                        ^~~~~~~

Root Cause

WASI (WebAssembly System Interface) doesn't support temporary files, making atomic write operations impossible. The current implementation unconditionally attempts to use .atomic when the option is specified, causing compilation failures on WASM targets.

Impact

This prevents RadiantKit from being used in:

  • WebAssembly applications
  • Cross-platform Swift projects targeting WASM
  • Server-side Swift applications compiled to WASM

Proposed Solution

Add platform-specific conditional compilation to gracefully handle WASI platforms:

// Sources/RadiantDocs/Primitives/InitializablePackageOptions.swift
public init(options: InitializablePackageOptions) {
  var dataOptions: Data.WritingOptions = []
  
  #if !arch(wasm32)
    if options.contains(.atomic) { 
      dataOptions.insert(.atomic) 
    }
  #endif
  
  if options.contains(.withoutOverwriting) { 
    dataOptions.insert(.withoutOverwriting) 
  }
  
  if options.contains(.noFileProtection) { 
    dataOptions.insert(.noFileProtection) 
  }
  
  self = dataOptions
}

Alternative Approaches

  1. Runtime detection - Check platform capabilities at runtime (more overhead)
  2. Conditional dependency - Make RadiantKit an optional dependency for WASM builds (limits functionality)
  3. WASM-specific fallback - Implement a non-atomic write path for WASM (maintains functionality with reduced safety guarantees)

Testing

After implementing the fix, the following command should succeed:

swift build --triple wasm32-unknown-wasi --configuration debug

Additional Context

This issue was discovered while adding cross-platform support to BushelKit, which depends on RadiantKit. We're working to ensure compatibility across macOS, Linux, iOS, Android, Windows, and WASM platforms.

Would you be open to a pull request implementing this fix? I'm happy to submit one with tests if this approach seems reasonable.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions