Skip to content

generate: output WIT package to the generated code directory#179

Closed
Mossaka wants to merge 6 commits intobytecodealliance:mainfrom
Mossaka:output-wit
Closed

generate: output WIT package to the generated code directory#179
Mossaka wants to merge 6 commits intobytecodealliance:mainfrom
Mossaka:output-wit

Conversation

@Mossaka
Copy link
Copy Markdown
Member

@Mossaka Mossaka commented Sep 28, 2024

This PR allows the generator to output WIT package to th generated Go directory. builds on top of #146

TODO:

@Mossaka Mossaka requested a review from ydnar as a code owner September 28, 2024 21:11
@Mossaka Mossaka marked this pull request as draft September 28, 2024 21:11
Comment thread cmd/wit-bindgen-go/cmd/generate/generate.go Outdated
@Mossaka Mossaka force-pushed the output-wit branch 3 times, most recently from f9812aa to 4e656a3 Compare October 4, 2024 01:13
Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
adds function `writeWITPackage` to output the given WIT package to the
wit directory placed inside of the generated Go code
instead of outputting the WIT package encoded as a Wasm component
Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
generate WIT files using wit.Resolve.WIT() instead of calling
`wasm-tools`

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
@Mossaka Mossaka marked this pull request as ready for review October 4, 2024 02:06
@Mossaka
Copy link
Copy Markdown
Member Author

Mossaka commented Oct 4, 2024

rebased this PR and ready for review @ydnar

if err := os.MkdirAll(witDir, cfg.outPerm); err != nil {
return err
}
witFilePath := filepath.Join(witDir, "webassembly.wit")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the name should be based on the top-level WIT package, if possible, and nested in that package's directory, e.g. $out/wasi/cli/webassembly.wit?

Copy link
Copy Markdown
Member Author

@Mossaka Mossaka Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a common practice for placing WIT files is to place it at a wit directory. In addition, a WIT package can have multiple worlds, and wit-bindgen-go generate generates all of them. Take wasi:cli as an example, it generates

.
├── go.mod
├── go.sum
├── main.go
├── main.wasm
├── wasi
│   ├── cli
│   │   ├── stderr
│   │   │   ├── empty.s
│   │   │   └── stderr.wit.go
│   │   ├── stdin
│   │   │   ├── empty.s
│   │   │   └── stdin.wit.go
│   │   └── stdout
│   │       ├── empty.s
│   │       └── stdout.wit.go
│   ├── clocks
│   │   ├── monotonic-clock
│   │   │   ├── empty.s
│   │   │   └── monotonic-clock.wit.go
│   │   └── wall-clock
│   │       ├── empty.s
│   │       └── wall-clock.wit.go
│   ├── http
│   │   ├── incoming-handler
│   │   │   ├── empty.s
│   │   │   ├── incoming-handler.exports.go
│   │   │   └── incoming-handler.wit.go
│   │   ├── outgoing-handler
│   │   │   ├── abi.go
│   │   │   ├── empty.s
│   │   │   └── outgoing-handler.wit.go
│   │   ├── proxy
│   │   │   └── proxy.wit.go
│   │   └── types
│   │       ├── abi.go
│   │       ├── empty.s
│   │       └── types.wit.go
│   ├── io
│   │   ├── error
│   │   │   ├── empty.s
│   │   │   └── error.wit.go
│   │   ├── poll
│   │   │   ├── empty.s
│   │   │   └── poll.wit.go
│   │   └── streams
│   │       ├── abi.go
│   │       ├── empty.s
│   │       └── streams.wit.go
│   └── random
│       └── random
│           ├── empty.s
│           └── random.wit.go
└── wit
    └── webassembly.wit

I think place the WIT file in the wasi/cli directory is not the best option here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple WIT files might be generated in the same out directory. So we need some way to avoid overwriting WIT files in the same directory

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, we should limit wit-bindgen-go generate to require a World name, and the WIT file can be named <out>/wit/<world-name>.wit?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wkg pattern is single file <namespace>_<package>@<version>.wit

zz ❯ wkg get wasi:http
No version specified; fetching version list...
Getting wasi:http@0.2.2...
Wrote './wasi_http@0.2.2.wit'
zz ❯ wkg get wasi:cli
No version specified; fetching version list...
Getting wasi:cli@0.2.2...
Wrote './wasi_cli@0.2.2.wit'
zz ❯ ls
wasi_cli@0.2.2.wit  wasi_http@0.2.2.wit

If possible I'd like this PR to wait until next BA meeting so we can sync on overall strategy.
I think we all agree we don't want to shell out to wasm-tools either here and in Tinygo, and some ideas floated before ( ex: go embed ) have downstream side effects to the Go ecosystem.

There is a path to combine the component compilation story & wit handling like the Rust export! macro, removing the need for interaction with the actual wit files altogether.
https://docs.rs/wit-bindgen/0.33.0/wit_bindgen/macro.generate.html#exports-the-export-macro
The 🔑 apis we're missing:

  • wit grammar parser: goes from files/oci artifacts to structs
  • wit world resolver: apply aliases, includes, and other resolution rules
  • wasm patcher: stitch wasm files & embed wit metadata

I think if we split up the work we can get there real quick.

If we do continue down the path of using wit files & shelling out, then we should align the story to other Bytecode Alliance tools as much as possible as they cover other concerns as well. In this case, following wkg file pattern means users can upload/parse/code-gen-other-languages using wasm-tools, wit-bindgen, and wkg as they are all converging on wkg's implementation.

@ydnar
Copy link
Copy Markdown
Collaborator

ydnar commented Oct 9, 2024

Added #203 which can generate tree-shaken WIT for a single world, as a precursor for this PR to emit a named WIT file inside the equivalent Go package path for the world.

@Mossaka
Copy link
Copy Markdown
Member Author

Mossaka commented Oct 11, 2024

Close since #206 implements this feature.

@Mossaka Mossaka closed this Oct 11, 2024
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