bytecodealliance / jco

JavaScript toolchain for working with WebAssembly Components
https://bytecodealliance.github.io/jco/
Apache License 2.0
642 stars 65 forks source link

error generating types for wasi-filesystem-0.2.2 #530

Closed ctaggart closed 1 day ago

ctaggart commented 1 day ago

I need to use the filesystem from my JavaScript component, so I'm trying to import it in wit/cowsay.wit:

world cowsay {
  import wasi:filesystem/types@0.2.2;

I run wkg wit fetch and it downloads the definitions. image

I then tried to use jco types to generate TypeScript types for wasi-filesystem, but here is the error:

~/ms/StarlingMonkey15> bunx jco --version
1.8.1

~/ms/StarlingMonkey15> bunx jco types wit/deps/wasi-filesystem-0.2.2/package.wit -o cowsay/src
(jco types) ComponentError: package not found
     --> /Users/cataggar/ms/StarlingMonkey15/wit/deps/wasi-filesystem-0.2.2/package.wit:30:7
      |
   30 |   use wasi:io/streams@0.2.2.{input-stream, output-stream, error};
      |       ^------
    at generateTypes (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/obj/js-component-bindgen-component.js:3894:11)
    at typesComponent (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/cmd/transpile.js:54:29)
    at async types (file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/cmd/transpile.js:18:17)
    at async file:///Users/cataggar/ms/StarlingMonkey15/node_modules/@bytecodealliance/jco/src/jco.js:200:9
ctaggart commented 1 day ago

Code is here: https://github.com/cataggar/StarlingMonkey15/tree/jco530

ctaggart commented 1 day ago

User error. I needed to pass the wit directory.

~/ms/StarlingMonkey15> bunx jco types --help
Usage: jco types <wit-path> -o <out-dir>

Generate types for the given WIT

Arguments:
  wit-path                    path to a WIT file or directory

Options:
  --name <name>               custom output name
  -n, --world-name <world>    WIT world to generate types for
  -o, --out-dir <out-dir>     output directory
  --tla-compat                generates types for the TLA compat output with an async $init promise export
  -I, --instantiation [mode]  type output for custom module instantiation (choices: "async", "sync", preset: "async")
  -q, --quiet                 disable output summary
  --feature <feature>         enable one specific WIT feature (repeatable) (default: [])
  --all-features              enable all features
  -h, --help                  display help for command
~/ms/StarlingMonkey15> bunx jco types wit/deps/wasi-filesystem-0.2.2/package.wit -o cowsay/src --verbose
error: unknown option '--verbose'
~/ms/StarlingMonkey15> bunx jco types wit -o cowsay/src

  Generated Type Files:

 - cowsay/src/interfaces/canvas.d.ts                  0.32 KiB
 - cowsay/src/interfaces/wasi-clocks-wall-clock.d.ts  0.19 KiB
 - cowsay/src/interfaces/wasi-filesystem-types.d.ts   20.8 KiB
 - cowsay/src/interfaces/wasi-io-error.d.ts           0.41 KiB
 - cowsay/src/interfaces/wasi-io-poll.d.ts            1.36 KiB
 - cowsay/src/interfaces/wasi-io-streams.d.ts         9.01 KiB
 - cowsay/src/wit.d.ts 
ctaggart commented 1 day ago

It would be nice if the wit directory exists, may be just default to use it. So jco types would do the equivelent of what jco types wit -o . does now.

guybedford commented 1 day ago

@ctaggart that could be a sensible default actually, basically just if no --wit arg is provided assume wit. Would you consider posting a PR?