square / okio

A modern I/O library for Android, Java, and Kotlin Multiplatform.
https://square.github.io/okio/
Apache License 2.0
8.81k stars 1.18k forks source link

Question: How do I access FileSystem in wasmJs? #1463

Open stepango opened 7 months ago

stepango commented 7 months ago

I'm using following multiplatform configuration for wasm target

wasmJs {
    binaries.executable()
    nodejs()
}

trying to add nodefilesystem dep

sourceSets {
    val wasmJsMain by getting {
        dependencies {
            implementation("com.squareup.okio:okio-nodefilesystem:3.9.0")
        }
    }
 }

But getting following error

 Could not determine the dependencies of task ':kotlinNpmInstall'.
> Could not resolve all dependencies for configuration ':llama2:wasmJsNpmAggregated'.
   > Could not resolve com.squareup.okio:okio-nodefilesystem:3.9.0.
     Required by:
         project :llama2
      > No matching variant of com.squareup.okio:okio-nodefilesystem:3.9.0 was found. The consumer was configured to find a library for use during 'kotlin-runtime', preferably optimized for non-jvm, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'wasm', attribute 'org.jetbrains.kotlin.js.public.package.json' with value 'public-package-json', attribute 'org.jetbrains.kotlin.wasm.target' with value 'js' but:
          - Variant 'commonFakeApiElements-published' capability com.squareup.okio:okio-nodefilesystem:3.9.0:
              - Incompatible because this component declares a component for use during 'kotlin-api', as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a component for use during 'kotlin-runtime', as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'wasm'
              - Other compatible attributes:
                  - Doesn't say anything about its component category (required a library)
                  - Doesn't say anything about its target Java environment (preferred optimized for non-jvm)
                  - Doesn't say anything about org.jetbrains.kotlin.js.public.package.json (required 'public-package-json')
                  - Doesn't say anything about org.jetbrains.kotlin.wasm.target (required 'js')
          - Variant 'jsApiElements-published' capability com.squareup.okio:okio-nodefilesystem:3.9.0 declares a library:
              - Incompatible because this component declares a component for use during 'kotlin-api', as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'js' and the consumer needed a component for use during 'kotlin-runtime', as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'wasm'
              - Other compatible attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for non-jvm)
                  - Doesn't say anything about org.jetbrains.kotlin.js.public.package.json (required 'public-package-json')
                  - Doesn't say anything about org.jetbrains.kotlin.wasm.target (required 'js')
          - Variant 'jsRuntimeElements-published' capability com.squareup.okio:okio-nodefilesystem:3.9.0 declares a library for use during 'kotlin-runtime':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'js' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'wasm'
              - Other compatible attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for non-jvm)
                  - Doesn't say anything about org.jetbrains.kotlin.js.public.package.json (required 'public-package-json')
                  - Doesn't say anything about org.jetbrains.kotlin.wasm.target (required 'js')
          - Variant 'jsSourcesElements-published' capability com.squareup.okio:okio-nodefilesystem:3.9.0 declares a component for use during 'kotlin-runtime':
              - Incompatible because this component declares documentation, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'js' and the consumer needed a library, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'wasm'
              - Other compatible attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for non-jvm)
                  - Doesn't say anything about org.jetbrains.kotlin.js.public.package.json (required 'public-package-json')
                  - Doesn't say anything about org.jetbrains.kotlin.wasm.target (required 'js')

Which kind of makes sense.

Is there any option for wasmJs to read from real fs the same way it works for js targets?

swankjesse commented 7 months ago

Try wasmWasi ?

swankjesse commented 7 months ago

We expose the Node FileSystem in Kotlin/JS only. For WASM we have the WASI FileSystem, which is needs its directories to be specified.

JakeWharton commented 7 months ago

Or if you're in a browser you have to use the JS file system API directly and can't use Okio: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API. In that case you can wrap your file system interactions in a higher-level suspending interface and switch between Okio and native JS implementations.